AK: Add StringBuilder::append_codepoint(u32)

Also, implement append(Utf32View) using it.
This commit is contained in:
Andreas Kling 2020-06-04 21:04:31 +02:00
parent 0bc92c259d
commit d4bbc00901
Notes: sideshowbarker 2024-07-19 05:49:56 +09:00
2 changed files with 25 additions and 19 deletions

View File

@ -113,29 +113,34 @@ void StringBuilder::clear()
m_length = 0;
}
void StringBuilder::append_codepoint(u32 codepoint)
{
if (codepoint <= 0x7f) {
append((char)codepoint);
} else if (codepoint <= 0x07ff) {
append((char)(((codepoint >> 6) & 0x1f) | 0xc0));
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
} else if (codepoint <= 0xffff) {
append((char)(((codepoint >> 12) & 0x0f) | 0xe0));
append((char)(((codepoint >> 6) & 0x3f) | 0x80));
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
} else if (codepoint <= 0x10ffff) {
append((char)(((codepoint >> 18) & 0x07) | 0xf0));
append((char)(((codepoint >> 12) & 0x3f) | 0x80));
append((char)(((codepoint >> 6) & 0x3f) | 0x80));
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
} else {
append(0xef);
append(0xbf);
append(0xbd);
}
}
void StringBuilder::append(const Utf32View& utf32_view)
{
for (size_t i = 0; i < utf32_view.length(); ++i) {
auto codepoint = utf32_view.codepoints()[i];
if (codepoint <= 0x7f) {
append((char)codepoint);
} else if (codepoint <= 0x07ff) {
append((char)(((codepoint >> 6) & 0x1f) | 0xc0));
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
} else if (codepoint <= 0xffff) {
append((char)(((codepoint >> 12) & 0x0f) | 0xe0));
append((char)(((codepoint >> 6) & 0x3f) | 0x80));
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
} else if (codepoint <= 0x10ffff) {
append((char)(((codepoint >> 18) & 0x07) | 0xf0));
append((char)(((codepoint >> 12) & 0x3f) | 0x80));
append((char)(((codepoint >> 6) & 0x3f) | 0x80));
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
} else {
append(0xef);
append(0xbf);
append(0xbd);
}
append_codepoint(codepoint);
}
}

View File

@ -42,6 +42,7 @@ public:
void append(const StringView&);
void append(const Utf32View&);
void append(char);
void append_codepoint(u32);
void append(const char*, size_t);
void appendf(const char*, ...);
void appendvf(const char*, va_list);