AK: Return early in utf8_to_utf16() for empty strings

No need to validate an empty string.
This commit is contained in:
Jelle Raaijmakers 2025-06-12 12:09:42 +02:00 committed by Jelle Raaijmakers
commit 408165d2f4
Notes: github-actions[bot] 2025-06-13 13:10:03 +00:00

View file

@ -58,11 +58,12 @@ ErrorOr<Utf16ConversionResult> utf8_to_utf16(StringView utf8_view, Endianness en
ErrorOr<Utf16ConversionResult> utf8_to_utf16(Utf8View const& utf8_view, Endianness endianness) ErrorOr<Utf16ConversionResult> utf8_to_utf16(Utf8View const& utf8_view, Endianness endianness)
{ {
if (utf8_view.is_empty())
return Utf16ConversionResult { Utf16Data {}, 0 };
// All callers want to allow lonely surrogates, which simdutf does not permit. // All callers want to allow lonely surrogates, which simdutf does not permit.
if (!utf8_view.validate(Utf8View::AllowSurrogates::No)) [[unlikely]] if (!utf8_view.validate(Utf8View::AllowSurrogates::No)) [[unlikely]]
return to_utf16_slow(utf8_view, endianness); return to_utf16_slow(utf8_view, endianness);
if (utf8_view.is_empty())
return Utf16ConversionResult { Utf16Data {}, 0 };
auto const* data = reinterpret_cast<char const*>(utf8_view.bytes()); auto const* data = reinterpret_cast<char const*>(utf8_view.bytes());
auto length = utf8_view.byte_length(); auto length = utf8_view.byte_length();