Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with UTF-8 display and navigation #303

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/base/base_strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ utf8_decode(U8 *str, U64 max){
}break;
case 2:
{
if (2 < max)
if (1 < max)
{
U8 cont_byte = str[1];
if (utf8_class[cont_byte >> 3] == 0)
Expand Down
31 changes: 29 additions & 2 deletions src/ui/ui_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,35 @@ ui_single_line_txt_op_from_event(Arena *arena, UI_Event *event, String8 string,
default:{}break;
case UI_EventDeltaUnit_Char:
{
// TODO(rjf): this should account for multi-byte characters in UTF-8... for now, just assume ASCII and
// no-op
if (delta.x > 0)
{
UnicodeDecode decode = utf8_decode(string.str + cursor.column - 1, string.size);
delta.x = decode.inc;
}
else
{
//Backwards check/count for UTF-8 multi byte sequence
U32 numSeqBytes = 0;
for (S64 idx = cursor.column - 2; idx >= 0; idx -= 1)
{
B32 isSeqByte = ExtractBit(string.str[idx], 7) == 1 && ExtractBit(string.str[idx], 6) == 0;
numSeqBytes += isSeqByte;
if (!isSeqByte)
{
if (numSeqBytes > 0)
{
U32 initialByte = ~(string.str[idx]) << 24;
U64 numLeadingBits = clz32(initialByte);
B32 isMultiByteSeq = numLeadingBits == numSeqBytes + 1;
if (isMultiByteSeq)
{
delta.x = -(numSeqBytes + 1);
}
}
break;
}
}
}
}break;
case UI_EventDeltaUnit_Word:
{
Expand Down