mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 13:30:31 +00:00
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:
parent
bacbc376a0
commit
f045a877b4
Notes:
sideshowbarker
2024-07-16 23:52:10 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f045a877b4 Pull-request: https://github.com/SerenityOS/serenity/pull/22496 Reviewed-by: https://github.com/LucasChollet
1 changed files with 23 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue