Skip to content

Commit 20805ba

Browse files
committed
Codechange: Use EncodeUtf8 and DecodeUtf8 directly, when dealing with a single character.
1 parent f640dae commit 20805ba

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

src/3rdparty/squirrel/squirrel/sqlexer.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "sqcompiler.h"
1212
#include "sqlexer.h"
1313

14-
#include "../../../string_func.h"
14+
#include "../../../core/utf8.hpp"
1515

1616
#include "../../../safeguards.h"
1717

@@ -28,8 +28,7 @@ SQLexer::~SQLexer()
2828

2929
void SQLexer::APPEND_CHAR(char32_t c)
3030
{
31-
char buf[4];
32-
size_t chars = Utf8Encode(buf, c);
31+
auto [buf, chars] = EncodeUtf8(c);
3332
for (size_t i = 0; i < chars; i++) {
3433
_longstr.push_back(buf[i]);
3534
}

src/newgrf/newgrf_act0_globalvar.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../language.h"
1515
#include "../rev.h"
1616
#include "../string_func.h"
17+
#include "../core/utf8.hpp"
1718
#include "../newgrf.h"
1819
#include "../newgrf_badge.h"
1920
#include "../newgrf_badge_type.h"
@@ -295,9 +296,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint first, uint last, int prop, Byt
295296
* safe as OpenTTD's strings gender/cases are usually in ASCII which
296297
* is just a subset of UTF8, or they need the bigger UTF8 characters
297298
* such as Cyrillic. Thus we will simply assume they're all UTF8. */
298-
char32_t c;
299-
size_t len = Utf8Decode(&c, name.data());
300-
if (len <= name.size() && c == NFO_UTF8_IDENTIFIER) name = name.substr(len);
299+
auto [len, c] = DecodeUtf8(name);
300+
if (c == NFO_UTF8_IDENTIFIER) name.remove_prefix(len);
301301

302302
LanguageMap::Mapping map;
303303
map.newgrf_id = newgrf_id;

src/textbuf.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,10 @@ void Textbuf::DeleteAll()
127127
*/
128128
bool Textbuf::InsertChar(char32_t key)
129129
{
130-
uint16_t len = (uint16_t)Utf8CharLen(key);
130+
auto [src, len] = EncodeUtf8(key);
131131
if (this->buf.size() + len < this->max_bytes && this->chars + 1 <= this->max_chars) {
132132
/* Make space in the string, then overwrite it with the Utf8 encoded character. */
133-
auto pos = this->buf.begin() + this->caretpos;
134-
pos = this->buf.insert(pos, len, '\0');
135-
Utf8Encode(pos, key);
133+
this->buf.insert(this->caretpos, src, len);
136134
this->chars++;
137135
this->caretpos += len;
138136

src/video/sdl2_v.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../core/math_func.hpp"
1818
#include "../core/mem_func.hpp"
1919
#include "../core/geometry_func.hpp"
20+
#include "../core/utf8.hpp"
2021
#include "../fileio_func.h"
2122
#include "../framerate_type.h"
2223
#include "../window_func.h"
@@ -497,9 +498,8 @@ bool VideoDriver_SDL_Base::PollEvent()
497498
uint keycode = ConvertSdlKeycodeIntoMy(kc);
498499

499500
if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
500-
char32_t character;
501-
Utf8Decode(&character, ev.text.text);
502-
HandleKeypress(keycode, character);
501+
auto [len, c] = DecodeUtf8(ev.text.text);
502+
if (len > 0) HandleKeypress(keycode, c);
503503
} else {
504504
HandleTextInput(ev.text.text);
505505
}

0 commit comments

Comments
 (0)