From 408165d2f43fa788a38abb751b89297f1cc0e05c Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Thu, 12 Jun 2025 12:09:42 +0200 Subject: [PATCH] AK: Return early in `utf8_to_utf16()` for empty strings No need to validate an empty string. --- AK/Utf16View.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/AK/Utf16View.cpp b/AK/Utf16View.cpp index f2862b2e1e1..e2008cee3dd 100644 --- a/AK/Utf16View.cpp +++ b/AK/Utf16View.cpp @@ -58,11 +58,12 @@ ErrorOr utf8_to_utf16(StringView utf8_view, Endianness en ErrorOr 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. if (!utf8_view.validate(Utf8View::AllowSurrogates::No)) [[unlikely]] return to_utf16_slow(utf8_view, endianness); - if (utf8_view.is_empty()) - return Utf16ConversionResult { Utf16Data {}, 0 }; auto const* data = reinterpret_cast(utf8_view.bytes()); auto length = utf8_view.byte_length();