diff --git a/src/3rdparty/squirrel/squirrel/sqlexer.cpp b/src/3rdparty/squirrel/squirrel/sqlexer.cpp index 96e43f4af7..89eae0026e 100644 --- a/src/3rdparty/squirrel/squirrel/sqlexer.cpp +++ b/src/3rdparty/squirrel/squirrel/sqlexer.cpp @@ -11,7 +11,7 @@ #include "sqcompiler.h" #include "sqlexer.h" -#include "../../../string_func.h" +#include "../../../core/utf8.hpp" #include "../../../safeguards.h" @@ -28,8 +28,7 @@ SQLexer::~SQLexer() void SQLexer::APPEND_CHAR(char32_t c) { - char buf[4]; - size_t chars = Utf8Encode(buf, c); + auto [buf, chars] = EncodeUtf8(c); for (size_t i = 0; i < chars; i++) { _longstr.push_back(buf[i]); } diff --git a/src/newgrf/newgrf_act0_globalvar.cpp b/src/newgrf/newgrf_act0_globalvar.cpp index 3f765b79fd..d1e4f30ead 100644 --- a/src/newgrf/newgrf_act0_globalvar.cpp +++ b/src/newgrf/newgrf_act0_globalvar.cpp @@ -14,6 +14,7 @@ #include "../language.h" #include "../rev.h" #include "../string_func.h" +#include "../core/utf8.hpp" #include "../newgrf.h" #include "../newgrf_badge.h" #include "../newgrf_badge_type.h" @@ -295,9 +296,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint first, uint last, int prop, Byt * safe as OpenTTD's strings gender/cases are usually in ASCII which * is just a subset of UTF8, or they need the bigger UTF8 characters * such as Cyrillic. Thus we will simply assume they're all UTF8. */ - char32_t c; - size_t len = Utf8Decode(&c, name.data()); - if (len <= name.size() && c == NFO_UTF8_IDENTIFIER) name = name.substr(len); + auto [len, c] = DecodeUtf8(name); + if (c == NFO_UTF8_IDENTIFIER) name.remove_prefix(len); LanguageMap::Mapping map; map.newgrf_id = newgrf_id; diff --git a/src/textbuf.cpp b/src/textbuf.cpp index 9b4573d0da..111be15df0 100644 --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -127,12 +127,10 @@ void Textbuf::DeleteAll() */ bool Textbuf::InsertChar(char32_t key) { - uint16_t len = (uint16_t)Utf8CharLen(key); + auto [src, len] = EncodeUtf8(key); if (this->buf.size() + len < this->max_bytes && this->chars + 1 <= this->max_chars) { /* Make space in the string, then overwrite it with the Utf8 encoded character. */ - auto pos = this->buf.begin() + this->caretpos; - pos = this->buf.insert(pos, len, '\0'); - Utf8Encode(pos, key); + this->buf.insert(this->caretpos, src, len); this->chars++; this->caretpos += len; diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 0863719a88..799f05c960 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -17,6 +17,7 @@ #include "../core/math_func.hpp" #include "../core/mem_func.hpp" #include "../core/geometry_func.hpp" +#include "../core/utf8.hpp" #include "../fileio_func.h" #include "../framerate_type.h" #include "../window_func.h" @@ -497,9 +498,8 @@ bool VideoDriver_SDL_Base::PollEvent() uint keycode = ConvertSdlKeycodeIntoMy(kc); if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) { - char32_t character; - Utf8Decode(&character, ev.text.text); - HandleKeypress(keycode, character); + auto [len, c] = DecodeUtf8(ev.text.text); + if (len > 0) HandleKeypress(keycode, c); } else { HandleTextInput(ev.text.text); }