AK+LibTextCodec: Stop using Utf16View endianness override

This is preparation for removing the endianness override, since it was
only used by a single client: LibTextCodec.

While here, add helpers and make use of simdutf for fast conversion.
This commit is contained in:
Andreas Kling 2025-04-15 17:49:09 +02:00 committed by Andreas Kling
commit 0e9480b944
Notes: github-actions[bot] 2025-04-16 08:06:08 +00:00
7 changed files with 56 additions and 53 deletions

View file

@ -61,6 +61,36 @@ ErrorOr<String> String::from_utf8(StringView view)
return result;
}
ErrorOr<String> String::from_utf16_le(ReadonlyBytes bytes)
{
if (!validate_utf16_le(bytes))
return Error::from_string_literal("String::from_utf16_le: Input was not valid UTF-16LE");
if (bytes.is_empty())
return String {};
char16_t const* utf16_data = reinterpret_cast<char16_t const*>(bytes.data());
size_t utf16_length = bytes.size() / 2;
size_t max_utf8_length = simdutf::utf8_length_from_utf16(utf16_data, utf16_length);
Vector<u8> buffer;
buffer.resize(max_utf8_length);
auto utf8_length = simdutf::convert_utf16le_to_utf8(utf16_data, utf16_length, reinterpret_cast<char*>(buffer.data()));
return String::from_utf8_without_validation(ReadonlyBytes { buffer.data(), utf8_length });
}
ErrorOr<String> String::from_utf16_be(ReadonlyBytes bytes)
{
if (!validate_utf16_be(bytes))
return Error::from_string_literal("String::from_utf16_be: Input was not valid UTF-16BE");
if (bytes.is_empty())
return String {};
char16_t const* utf16_data = reinterpret_cast<char16_t const*>(bytes.data());
size_t utf16_length = bytes.size() / 2;
size_t max_utf8_length = simdutf::utf8_length_from_utf16(utf16_data, utf16_length);
Vector<u8> buffer;
buffer.resize(max_utf8_length);
auto utf8_length = simdutf::convert_utf16be_to_utf8(utf16_data, utf16_length, reinterpret_cast<char*>(buffer.data()));
return String::from_utf8_without_validation(ReadonlyBytes { buffer.data(), utf8_length });
}
ErrorOr<String> String::from_utf16(Utf16View const& utf16)
{
if (!utf16.validate())