1
0
Fork 0

Codechange: Replace last usage of Utf8CharLen with Utf8View.

pull/13985/head
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

@ -106,23 +106,6 @@ inline char32_t Utf8Consume(Titr &s)
return c; return c;
} }
/**
* Return the length of a UTF-8 encoded character.
* @param c Unicode character.
* @return Length of UTF-8 encoding for character.
*/
inline int8_t Utf8CharLen(char32_t c)
{
if (c < 0x80) return 1;
if (c < 0x800) return 2;
if (c < 0x10000) return 3;
if (c < 0x110000) return 4;
/* Invalid valid, we encode as a '?' */
return 1;
}
/** /**
* Return the length of an UTF-8 encoded value based on a single char. This * Return the length of an UTF-8 encoded value based on a single char. This
* char should be the first byte of the UTF-8 encoding. If not, or encoding * char should be the first byte of the UTF-8 encoding. If not, or encoding

View File

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