From 82ed98d832c06427acb8a49abce6557b68acfaed Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 5 Sep 2024 19:56:12 +0200 Subject: [PATCH] InputText: amends: replace IMSTB_TEXTEDIT_GETPREVCHARINDEX code with ImTextFindPreviousUtf8Codepoint(). (7925) --- imgui_widgets.cpp | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index fbc740278220..3ca740afb55d 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -3964,42 +3964,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)