Skip to content

Commit

Permalink
InputText: amends: replace IMSTB_TEXTEDIT_GETPREVCHARINDEX code with …
Browse files Browse the repository at this point in the history
…ImTextFindPreviousUtf8Codepoint(). (7925)
  • Loading branch information
ocornut committed Sep 5, 2024
1 parent c0cd48a commit e91acc2
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3955,42 +3955,10 @@ static int IMSTB_TEXTEDIT_GETNEXTCHARINDEX_IMPL(ImGuiInputTextState* obj, int id
return idx + ImTextCharFromUtf8(&c, obj->TextA.Data + idx, obj->TextA.Data + obj->TextA.Size);
}

static int CountLeadingHighBits(unsigned char b)
{
for (int i = 0; i < (int)sizeof(b) * 8; i++)
{
bool set = (b >> (7 - i)) & 1;
if (!set)
return i;
}

return sizeof(b) * 8;
}

static int IMSTB_TEXTEDIT_GETPREVCHARINDEX_IMPL(ImGuiInputTextState* obj, int idx)
{
// Backwards check/count for UTF-8 multi byte sequence
int num_seq_bytes = 0;
for (int i = idx - 1; i >= 0; i -= 1)
{
bool is_seq_byte = (obj->TextA.Data[i] & 0x80) == 0x80 && (obj->TextA.Data[i] & 0x40) == 0;
num_seq_bytes += is_seq_byte;
if (!is_seq_byte)
{
if (num_seq_bytes > 0)
{
char initial_byte = obj->TextA.Data[i];
char num_leading_bits = (char)CountLeadingHighBits(initial_byte);
bool is_multi_byte_seq = num_leading_bits == num_seq_bytes + 1;
if (is_multi_byte_seq)
{
return idx - (num_seq_bytes + 1);
}
}
break;
}
}
return idx - 1;
const char* p = ImTextFindPreviousUtf8Codepoint(obj->TextA.Data, obj->TextA.Data + idx);
return (int)(p - obj->TextA.Data);
}

static void STB_TEXTEDIT_DELETECHARS(ImGuiInputTextState* obj, int pos, int n)
Expand Down

0 comments on commit e91acc2

Please sign in to comment.