diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 587cc908f4eb..c67df493d7fe 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -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)