1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-17 09:22:42 +01:00

Codechange: Replace last usage of Utf8CharLen with Utf8View.

This commit is contained in:
frosch
2025-04-04 21:43:46 +02:00
committed by frosch
parent 9229956f04
commit 294f826364
2 changed files with 19 additions and 30 deletions

View File

@@ -170,24 +170,30 @@ bool Textbuf::InsertString(const char *str, bool marked, const char *caret, cons
if (str == nullptr) return false;
uint16_t bytes = 0, chars = 0;
char32_t c;
for (const char *ptr = str; (c = Utf8Consume(&ptr)) != '\0';) {
if (!IsValidChar(c, this->afilter)) break;
uint16_t chars = 0;
uint16_t bytes;
{
Utf8View view(str);
auto cur = view.begin();
const auto end = view.end();
while (cur != end) {
if (!IsValidChar(*cur, this->afilter)) break;
uint8_t len = Utf8CharLen(c);
if (this->buf.size() + bytes + len >= this->max_bytes) break;
if (this->chars + chars + 1 > this->max_chars) break;
auto next = cur;
++next;
if (this->buf.size() + next.GetByteOffset() >= this->max_bytes) break;
if (this->chars + chars + 1 > this->max_chars) break;
bytes += len;
chars++;
/* Move caret if needed. */
if (ptr == caret) this->caretpos = insertpos + bytes;
cur = next;
chars++;
}
bytes = static_cast<uint16_t>(cur.GetByteOffset());
}
if (bytes == 0) return false;
/* Move caret if needed. */
if (str <= caret && caret <= str + bytes) this->caretpos = insertpos + (caret - str);
if (marked) {
this->markpos = insertpos;
this->markend = insertpos + bytes;