From f045a877b4b0e1adbcf072b8484137fee6dc56e0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 29 Dec 2023 17:27:58 +0100 Subject: [PATCH] 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. --- AK/StringBuilder.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 60e0d3d332e..70d4ec10fdc 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -200,7 +200,29 @@ ErrorOr 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(code_point)); + } else if (code_point <= 0x07ff) { + (void)will_append(2); + m_buffer.append(static_cast((((code_point >> 6) & 0x1f) | 0xc0))); + m_buffer.append(static_cast((((code_point >> 0) & 0x3f) | 0x80))); + } else if (code_point <= 0xffff) { + (void)will_append(3); + m_buffer.append(static_cast((((code_point >> 12) & 0x0f) | 0xe0))); + m_buffer.append(static_cast((((code_point >> 6) & 0x3f) | 0x80))); + m_buffer.append(static_cast((((code_point >> 0) & 0x3f) | 0x80))); + } else if (code_point <= 0x10ffff) { + (void)will_append(4); + m_buffer.append(static_cast((((code_point >> 18) & 0x07) | 0xf0))); + m_buffer.append(static_cast((((code_point >> 12) & 0x3f) | 0x80))); + m_buffer.append(static_cast((((code_point >> 6) & 0x3f) | 0x80))); + m_buffer.append(static_cast((((code_point >> 0) & 0x3f) | 0x80))); + } else { + (void)will_append(3); + m_buffer.append(0xef); + m_buffer.append(0xbf); + m_buffer.append(0xbd); + } } #ifndef KERNEL