2021-07-19 09:02:13 -04:00
|
|
|
/*
|
2025-06-12 17:39:05 -04:00
|
|
|
* Copyright (c) 2021-2025, Tim Flynn <trflynn89@ladybird.org>
|
2021-07-19 09:02:13 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-07-21 16:38:12 -04:00
|
|
|
#include <AK/CharacterTypes.h>
|
2023-01-06 13:19:34 -05:00
|
|
|
#include <AK/Concepts.h>
|
2021-07-19 09:02:13 -04:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Utf16View.h>
|
2021-07-20 22:25:48 -04:00
|
|
|
#include <AK/Utf32View.h>
|
2021-07-19 09:02:13 -04:00
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
|
2024-07-16 13:02:51 -04:00
|
|
|
#include <simdutf.h>
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
namespace AK {
|
|
|
|
|
2023-01-06 13:19:34 -05:00
|
|
|
template<OneOf<Utf8View, Utf32View> UtfViewType>
|
2025-06-19 07:34:10 -04:00
|
|
|
static ErrorOr<Utf16ConversionResult> to_utf16_slow(UtfViewType const& view)
|
2021-07-19 09:02:13 -04:00
|
|
|
{
|
2023-01-06 11:00:24 -05:00
|
|
|
Utf16Data utf16_data;
|
2023-01-06 13:19:34 -05:00
|
|
|
TRY(utf16_data.try_ensure_capacity(view.length()));
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
size_t code_point_count = 0;
|
|
|
|
for (auto code_point : view) {
|
2025-06-12 17:39:05 -04:00
|
|
|
TRY(UnicodeUtils::try_code_point_to_utf16(code_point, [&](auto code_unit) -> ErrorOr<void> {
|
2025-06-19 07:34:10 -04:00
|
|
|
TRY(utf16_data.try_append(code_unit));
|
2025-06-12 17:39:05 -04:00
|
|
|
return {};
|
|
|
|
}));
|
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
code_point_count++;
|
|
|
|
}
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
return Utf16ConversionResult { move(utf16_data), code_point_count };
|
2021-07-19 09:02:13 -04:00
|
|
|
}
|
|
|
|
|
2025-06-19 07:34:10 -04:00
|
|
|
ErrorOr<Utf16ConversionResult> utf8_to_utf16(StringView utf8_view)
|
2021-07-20 22:25:48 -04:00
|
|
|
{
|
2025-06-19 07:34:10 -04:00
|
|
|
return utf8_to_utf16(Utf8View { utf8_view });
|
2021-07-20 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
2025-06-19 07:34:10 -04:00
|
|
|
ErrorOr<Utf16ConversionResult> utf8_to_utf16(Utf8View const& utf8_view)
|
2021-07-20 22:25:48 -04:00
|
|
|
{
|
2025-06-12 12:09:42 +02:00
|
|
|
if (utf8_view.is_empty())
|
|
|
|
return Utf16ConversionResult { Utf16Data {}, 0 };
|
|
|
|
|
2024-07-16 16:05:46 -04:00
|
|
|
// All callers want to allow lonely surrogates, which simdutf does not permit.
|
2025-06-26 19:52:09 -04:00
|
|
|
if (!utf8_view.validate(AllowLonelySurrogates::No)) [[unlikely]]
|
2025-06-19 07:34:10 -04:00
|
|
|
return to_utf16_slow(utf8_view);
|
2024-07-16 16:05:46 -04:00
|
|
|
|
2024-07-18 11:57:01 -04:00
|
|
|
auto const* data = reinterpret_cast<char const*>(utf8_view.bytes());
|
|
|
|
auto length = utf8_view.byte_length();
|
2024-07-16 16:05:46 -04:00
|
|
|
|
2024-07-18 11:57:01 -04:00
|
|
|
Utf16Data utf16_data;
|
|
|
|
TRY(utf16_data.try_resize(simdutf::utf16_length_from_utf8(data, length)));
|
2025-04-02 17:56:49 +02:00
|
|
|
// FIXME: simdutf _could_ be telling us about this, but it doesn't -- so we have to compute it again.
|
|
|
|
auto code_point_length = simdutf::count_utf8(data, length);
|
2024-07-18 11:57:01 -04:00
|
|
|
|
2025-06-19 07:34:10 -04:00
|
|
|
[[maybe_unused]] auto result = simdutf::convert_utf8_to_utf16(data, length, reinterpret_cast<char16_t*>(utf16_data.data()));
|
2024-07-16 16:05:46 -04:00
|
|
|
ASSERT(result == utf16_data.size());
|
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
return Utf16ConversionResult { utf16_data, code_point_length };
|
2021-07-20 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
2025-06-19 07:34:10 -04:00
|
|
|
ErrorOr<Utf16ConversionResult> utf32_to_utf16(Utf32View const& utf32_view)
|
2021-07-20 22:25:48 -04:00
|
|
|
{
|
2024-07-20 07:31:19 -04:00
|
|
|
if (utf32_view.is_empty())
|
2025-04-02 17:56:49 +02:00
|
|
|
return Utf16ConversionResult { Utf16Data {}, 0 };
|
2024-07-20 07:31:19 -04:00
|
|
|
|
2024-07-18 11:57:01 -04:00
|
|
|
auto const* data = reinterpret_cast<char32_t const*>(utf32_view.code_points());
|
|
|
|
auto length = utf32_view.length();
|
2024-07-16 16:05:46 -04:00
|
|
|
|
2024-07-18 11:57:01 -04:00
|
|
|
Utf16Data utf16_data;
|
|
|
|
TRY(utf16_data.try_resize(simdutf::utf16_length_from_utf32(data, length)));
|
|
|
|
|
2025-06-19 07:34:10 -04:00
|
|
|
[[maybe_unused]] auto result = simdutf::convert_utf32_to_utf16(data, length, reinterpret_cast<char16_t*>(utf16_data.data()));
|
2024-07-16 16:05:46 -04:00
|
|
|
ASSERT(result == utf16_data.size());
|
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
return Utf16ConversionResult { utf16_data, length };
|
2021-07-20 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
bool validate_utf16_le(ReadonlyBytes bytes)
|
2024-07-30 06:19:56 -04:00
|
|
|
{
|
2025-06-26 12:52:23 -04:00
|
|
|
return simdutf::validate_utf16le(reinterpret_cast<char16_t const*>(bytes.data()), bytes.size() / 2);
|
2024-07-30 06:19:56 -04:00
|
|
|
}
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
bool validate_utf16_be(ReadonlyBytes bytes)
|
2023-01-08 18:56:53 -05:00
|
|
|
{
|
2025-06-26 12:52:23 -04:00
|
|
|
return simdutf::validate_utf16be(reinterpret_cast<char16_t const*>(bytes.data()), bytes.size() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t utf16_code_unit_length_from_utf8(StringView string)
|
|
|
|
{
|
|
|
|
return simdutf::utf16_length_from_utf8(string.characters_without_null_termination(), string.length());
|
2023-01-08 18:56:53 -05:00
|
|
|
}
|
|
|
|
|
2025-06-26 19:52:09 -04:00
|
|
|
ErrorOr<String> Utf16View::to_utf8(AllowLonelySurrogates allow_lonely_surrogates) const
|
2021-07-19 09:02:13 -04:00
|
|
|
{
|
2025-06-26 12:52:23 -04:00
|
|
|
if (is_empty())
|
|
|
|
return String {};
|
2025-06-26 19:52:09 -04:00
|
|
|
if (!validate(allow_lonely_surrogates))
|
2025-06-26 12:52:23 -04:00
|
|
|
return Error::from_string_literal("Input was not valid UTF-16");
|
|
|
|
|
2025-06-26 19:52:09 -04:00
|
|
|
if (allow_lonely_surrogates == AllowLonelySurrogates::No) {
|
2025-06-26 12:52:23 -04:00
|
|
|
String result;
|
|
|
|
auto utf8_length = simdutf::utf8_length_from_utf16(m_string, length_in_code_units());
|
|
|
|
|
|
|
|
TRY(result.replace_with_new_string(Badge<Utf16View> {}, utf8_length, [&](Bytes buffer) -> ErrorOr<void> {
|
|
|
|
[[maybe_unused]] auto result = simdutf::convert_utf16_to_utf8(m_string, length_in_code_units(), reinterpret_cast<char*>(buffer.data()));
|
|
|
|
ASSERT(result == buffer.size());
|
|
|
|
return {};
|
|
|
|
}));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-07-16 16:05:46 -04:00
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
StringBuilder builder;
|
2025-05-09 18:08:01 +02:00
|
|
|
builder.append(*this);
|
|
|
|
return builder.to_string();
|
2021-07-19 09:02:13 -04:00
|
|
|
}
|
|
|
|
|
2025-06-26 19:52:09 -04:00
|
|
|
ErrorOr<ByteString> Utf16View::to_byte_string(AllowLonelySurrogates allow_lonely_surrogates) const
|
2021-07-19 09:02:13 -04:00
|
|
|
{
|
2025-06-26 19:52:09 -04:00
|
|
|
return TRY(to_utf8(allow_lonely_surrogates)).to_byte_string();
|
2021-07-19 09:02:13 -04:00
|
|
|
}
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
bool Utf16View::is_ascii() const
|
2021-08-01 18:56:52 -04:00
|
|
|
{
|
2025-06-26 12:52:23 -04:00
|
|
|
return simdutf::validate_ascii(reinterpret_cast<char const*>(m_string), length_in_code_units() * sizeof(char16_t));
|
2021-08-01 18:56:52 -04:00
|
|
|
}
|
|
|
|
|
2025-06-26 19:52:09 -04:00
|
|
|
bool Utf16View::validate(size_t& valid_code_units, AllowLonelySurrogates allow_lonely_surrogates) const
|
2021-07-20 22:25:48 -04:00
|
|
|
{
|
2025-06-26 12:52:23 -04:00
|
|
|
auto view = *this;
|
|
|
|
valid_code_units = 0;
|
2025-04-02 17:56:49 +02:00
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
while (!view.is_empty()) {
|
|
|
|
auto result = simdutf::validate_utf16_with_errors(view.m_string, view.length_in_code_units());
|
|
|
|
valid_code_units += result.count;
|
2021-07-20 22:25:48 -04:00
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
if (result.error == simdutf::SUCCESS)
|
|
|
|
return true;
|
2025-06-26 19:52:09 -04:00
|
|
|
if (allow_lonely_surrogates == AllowLonelySurrogates::No || result.error != simdutf::SURROGATE)
|
2025-06-26 12:52:23 -04:00
|
|
|
return false;
|
2021-07-20 22:25:48 -04:00
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
view = view.substring_view(result.count + 1);
|
|
|
|
++valid_code_units;
|
2021-07-20 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
return true;
|
2021-07-20 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Utf16View::code_unit_offset_of(size_t code_point_offset) const
|
|
|
|
{
|
2025-06-11 11:35:28 +02:00
|
|
|
if (length_in_code_points() == length_in_code_units()) // Fast path: all code points are one code unit.
|
2025-04-02 17:56:49 +02:00
|
|
|
return code_point_offset;
|
|
|
|
|
2021-07-20 22:25:48 -04:00
|
|
|
size_t code_unit_offset = 0;
|
|
|
|
|
|
|
|
for (auto it = begin(); it != end(); ++it) {
|
|
|
|
if (code_point_offset == 0)
|
|
|
|
return code_unit_offset;
|
|
|
|
|
|
|
|
code_unit_offset += it.length_in_code_units();
|
|
|
|
--code_point_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return code_unit_offset;
|
|
|
|
}
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
size_t Utf16View::code_point_offset_of(size_t code_unit_offset) const
|
2022-01-31 18:13:32 +02:00
|
|
|
{
|
2025-06-26 12:52:23 -04:00
|
|
|
if (length_in_code_points() == length_in_code_units()) // Fast path: all code points are one code unit.
|
|
|
|
return code_unit_offset;
|
2022-01-31 18:13:32 +02:00
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
size_t code_point_offset = 0;
|
2022-01-31 18:13:32 +02:00
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
for (auto it = begin(); it != end(); ++it) {
|
|
|
|
if (code_unit_offset == 0)
|
|
|
|
return code_point_offset;
|
|
|
|
|
|
|
|
code_unit_offset -= it.length_in_code_units();
|
|
|
|
++code_point_offset;
|
|
|
|
}
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
return code_point_offset;
|
2021-07-19 09:02:13 -04:00
|
|
|
}
|
|
|
|
|
2021-07-20 22:25:48 -04:00
|
|
|
Utf16View Utf16View::unicode_substring_view(size_t code_point_offset, size_t code_point_length) const
|
|
|
|
{
|
|
|
|
if (code_point_length == 0)
|
|
|
|
return {};
|
|
|
|
|
2025-06-11 11:35:28 +02:00
|
|
|
if (length_in_code_points() == length_in_code_units()) // Fast path: all code points are one code unit.
|
2025-04-02 17:56:49 +02:00
|
|
|
return substring_view(code_point_offset, code_point_length);
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
auto code_unit_offset_of = [&](Utf16CodePointIterator const& it) {
|
|
|
|
return it.m_iterator - m_string;
|
|
|
|
};
|
|
|
|
|
2021-07-20 22:25:48 -04:00
|
|
|
size_t code_point_index = 0;
|
|
|
|
size_t code_unit_offset = 0;
|
|
|
|
|
|
|
|
for (auto it = begin(); it != end(); ++it) {
|
|
|
|
if (code_point_index == code_point_offset)
|
|
|
|
code_unit_offset = code_unit_offset_of(it);
|
|
|
|
|
|
|
|
if (code_point_index == (code_point_offset + code_point_length - 1)) {
|
|
|
|
size_t code_unit_length = code_unit_offset_of(++it) - code_unit_offset;
|
|
|
|
return substring_view(code_unit_offset, code_unit_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
++code_point_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
size_t Utf16View::calculate_length_in_code_points() const
|
|
|
|
{
|
2025-06-26 19:52:09 -04:00
|
|
|
// simdutf's code point length method assumes valid UTF-16, whereas we allow lonely surrogates.
|
|
|
|
if (validate(AllowLonelySurrogates::No)) [[likely]]
|
2025-06-26 12:52:23 -04:00
|
|
|
return simdutf::count_utf16(m_string, length_in_code_units());
|
2024-07-16 13:02:51 -04:00
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
size_t code_points = 0;
|
|
|
|
for ([[maybe_unused]] auto code_point : *this)
|
|
|
|
++code_points;
|
|
|
|
return code_points;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|