mirror of
https://github.com/zebrajr/ladybird.git
synced 2025-12-06 00:19:53 +01:00
AK: Avoid redundant ASCII validation in StringBuilder::append(Utf16View)
If the UTF-16 data comes from ASCII-only storage, we can skip the ASCII validation and save ourselves the effort.
This commit is contained in:
parent
b50ff02da4
commit
bb93107a7d
|
|
@ -159,6 +159,32 @@ ErrorOr<void> StringBuilder::try_append(StringView string)
|
|||
return {};
|
||||
}
|
||||
|
||||
void StringBuilder::append_ascii_without_validation(ReadonlyBytes string)
|
||||
{
|
||||
MUST(try_append_ascii_without_validation(string));
|
||||
}
|
||||
|
||||
ErrorOr<void> StringBuilder::try_append_ascii_without_validation(ReadonlyBytes string)
|
||||
{
|
||||
if (string.is_empty())
|
||||
return {};
|
||||
|
||||
if (m_mode == Mode::UTF8 || m_utf16_builder_is_ascii) {
|
||||
TRY(m_buffer.try_append(string));
|
||||
} else {
|
||||
if (m_mode == Mode::UTF16) {
|
||||
TRY(ensure_storage_is_utf16());
|
||||
TRY(will_append(string.size() * 2));
|
||||
} else {
|
||||
TRY(will_append(string.size()));
|
||||
}
|
||||
for (auto code_point : Utf8View { string })
|
||||
TRY(try_append_code_point(code_point));
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> StringBuilder::try_append(char ch)
|
||||
{
|
||||
if (m_mode == Mode::UTF8 || (m_utf16_builder_is_ascii && is_ascii(ch))) {
|
||||
|
|
@ -428,7 +454,7 @@ ErrorOr<void> StringBuilder::try_append(Utf16View const& utf16_view)
|
|||
if (utf16_view.is_empty())
|
||||
return {};
|
||||
if (utf16_view.has_ascii_storage())
|
||||
return try_append(utf16_view.bytes());
|
||||
return try_append_ascii_without_validation(utf16_view.bytes());
|
||||
|
||||
auto append_as_utf8 = m_mode == Mode::UTF8 || (m_utf16_builder_is_ascii && utf16_view.is_ascii());
|
||||
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ public:
|
|||
ErrorOr<void> try_append_repeated(StringView, size_t);
|
||||
ErrorOr<void> try_append_repeated(Utf16View const&, size_t);
|
||||
ErrorOr<void> try_append_escaped_for_json(StringView);
|
||||
ErrorOr<void> try_append_ascii_without_validation(ReadonlyBytes);
|
||||
|
||||
template<typename... Parameters>
|
||||
ErrorOr<void> try_appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
||||
|
|
@ -68,6 +69,7 @@ public:
|
|||
void append_repeated(Utf16View const&, size_t);
|
||||
void append_escaped_for_json(StringView);
|
||||
void append_as_lowercase(char);
|
||||
void append_ascii_without_validation(ReadonlyBytes);
|
||||
|
||||
template<typename... Parameters>
|
||||
void appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user