AK: Implement StringBuilder::append_code_point() more efficiently

Instead of do a wrappy MUST(try_append_code_point()), we now inline
the UTF-8 encoding logic. This allows us to grow the buffer by the
right increment up front, and also removes a bunch of ErrorOr ceremony
that we don't care about.
This commit is contained in:
Andreas Kling 2023-12-29 17:27:58 +01:00
parent bacbc376a0
commit f045a877b4
Notes: sideshowbarker 2024-07-16 23:52:10 +09:00

View file

@ -200,7 +200,29 @@ ErrorOr<void> StringBuilder::try_append_code_point(u32 code_point)
void StringBuilder::append_code_point(u32 code_point)
{
MUST(try_append_code_point(code_point));
if (code_point <= 0x7f) {
m_buffer.append(static_cast<char>(code_point));
} else if (code_point <= 0x07ff) {
(void)will_append(2);
m_buffer.append(static_cast<char>((((code_point >> 6) & 0x1f) | 0xc0)));
m_buffer.append(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80)));
} else if (code_point <= 0xffff) {
(void)will_append(3);
m_buffer.append(static_cast<char>((((code_point >> 12) & 0x0f) | 0xe0)));
m_buffer.append(static_cast<char>((((code_point >> 6) & 0x3f) | 0x80)));
m_buffer.append(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80)));
} else if (code_point <= 0x10ffff) {
(void)will_append(4);
m_buffer.append(static_cast<char>((((code_point >> 18) & 0x07) | 0xf0)));
m_buffer.append(static_cast<char>((((code_point >> 12) & 0x3f) | 0x80)));
m_buffer.append(static_cast<char>((((code_point >> 6) & 0x3f) | 0x80)));
m_buffer.append(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80)));
} else {
(void)will_append(3);
m_buffer.append(0xef);
m_buffer.append(0xbf);
m_buffer.append(0xbd);
}
}
#ifndef KERNEL