1
0
Fork 0

Codechange: Use EncodeUtf8 and DecodeUtf8 directly, when dealing with a single character.

pull/13960/head
frosch 2025-04-02 16:18:41 +02:00 committed by frosch
parent f640daee4c
commit 20805ba84b
4 changed files with 10 additions and 13 deletions

View File

@ -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]);
}

View File

@ -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;

View File

@ -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;

View File

@ -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);
}