diff --git a/Source/DiabloUI/diabloui.cpp b/Source/DiabloUI/diabloui.cpp index 31fc886cd..e4b1197c1 100644 --- a/Source/DiabloUI/diabloui.cpp +++ b/Source/DiabloUI/diabloui.cpp @@ -27,6 +27,7 @@ #include "utils/sdl_wrap.h" #include "utils/stubs.h" #include "utils/language.h" +#include "utils/utf8.h" #ifdef __SWITCH__ // for virtual keyboard on Switch @@ -330,10 +331,7 @@ void UiFocusNavigation(SDL_Event *event) #endif case SDLK_BACKSPACE: case SDLK_LEFT: { - int nameLen = strlen(UiTextInput); - if (nameLen > 0) { - UiTextInput[nameLen - 1] = '\0'; - } + UiTextInput[FindLastUtf8Symbols(UiTextInput)] = '\0'; return; } default: diff --git a/Source/control.cpp b/Source/control.cpp index 5f401d8bb..73fea7a86 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -38,6 +38,7 @@ #include "utils/language.h" #include "utils/sdl_geometry.h" #include "utils/stdcompat/optional.hpp" +#include "utils/utf8.h" #include "options.h" #ifdef _DEBUG @@ -1802,9 +1803,7 @@ bool control_presskeys(int vkey) } else if (vkey == DVL_VK_RETURN) { ControlPressEnter(); } else if (vkey == DVL_VK_BACK) { - std::size_t len = strlen(TalkMessage); - if (len > 0) - TalkMessage[len - 1] = '\0'; + TalkMessage[FindLastUtf8Symbols(TalkMessage)] = '\0'; } else if (vkey == DVL_VK_DOWN) { ControlUpDown(1); } else if (vkey == DVL_VK_UP) { diff --git a/Source/utils/utf8.h b/Source/utils/utf8.h index 06b8573f4..d4e0c5115 100644 --- a/Source/utils/utf8.h +++ b/Source/utils/utf8.h @@ -65,3 +65,21 @@ inline const char *utf8_decode(const char *buf, uint32_t *c, int *e) return reinterpret_cast(next); } + +inline int FindLastUtf8Symbols(const char *text) +{ + std::string textBuffer(text); + textBuffer.resize(textBuffer.size() + 4); // Buffer must be padded before calling utf8_decode() + const char *textData = textBuffer.data(); + const char *previousPosition = textData; + + uint32_t next; + int error; + for (; *textData != '\0'; previousPosition = textData) { + textData = utf8_decode(textData, &next, &error); + if (*textData == '\0') + return previousPosition - textBuffer.data(); + } + + return 0; +}