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>
|
2025-06-27 12:30:25 -04:00
|
|
|
#include <AK/Utf16String.h>
|
2021-07-19 09:02:13 -04:00
|
|
|
#include <AK/Utf16View.h>
|
|
|
|
#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 {
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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-12 19:29:41 -04:00
|
|
|
if (has_ascii_storage())
|
|
|
|
return String::from_utf8_without_validation(bytes());
|
|
|
|
|
2025-06-26 19:52:09 -04:00
|
|
|
if (allow_lonely_surrogates == AllowLonelySurrogates::No) {
|
2025-08-08 16:32:26 -04:00
|
|
|
if (!validate())
|
|
|
|
return Error::from_string_literal("Input was not valid UTF-16");
|
2025-06-12 19:29:41 -04:00
|
|
|
|
2025-08-08 16:32:26 -04:00
|
|
|
String result;
|
2025-06-12 19:29:41 -04:00
|
|
|
auto utf8_length = simdutf::utf8_length_from_utf16(m_string.utf16, length_in_code_units());
|
2025-06-26 12:52:23 -04:00
|
|
|
|
|
|
|
TRY(result.replace_with_new_string(Badge<Utf16View> {}, utf8_length, [&](Bytes buffer) -> ErrorOr<void> {
|
2025-06-12 19:29:41 -04:00
|
|
|
[[maybe_unused]] auto result = simdutf::convert_utf16_to_utf8(m_string.utf16, length_in_code_units(), reinterpret_cast<char*>(buffer.data()));
|
2025-06-26 12:52:23 -04:00
|
|
|
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-27 12:30:25 -04:00
|
|
|
Utf16String Utf16View::to_ascii_lowercase() const
|
|
|
|
{
|
|
|
|
StringBuilder builder(StringBuilder::Mode::UTF16, length_in_code_units());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length_in_code_units(); ++i)
|
|
|
|
builder.append_code_unit(AK::to_ascii_lowercase(code_unit_at(i)));
|
|
|
|
|
|
|
|
return builder.to_utf16_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
Utf16String Utf16View::to_ascii_uppercase() const
|
|
|
|
{
|
|
|
|
StringBuilder builder(StringBuilder::Mode::UTF16, length_in_code_units());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length_in_code_units(); ++i)
|
|
|
|
builder.append_code_unit(AK::to_ascii_uppercase(code_unit_at(i)));
|
|
|
|
|
|
|
|
return builder.to_utf16_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
Utf16String Utf16View::to_ascii_titlecase() const
|
|
|
|
{
|
|
|
|
StringBuilder builder(StringBuilder::Mode::UTF16, length_in_code_units());
|
|
|
|
bool next_is_upper = true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length_in_code_units(); ++i) {
|
|
|
|
auto code_unit = code_unit_at(i);
|
|
|
|
|
|
|
|
if (next_is_upper)
|
|
|
|
builder.append_code_unit(AK::to_ascii_uppercase(code_unit));
|
|
|
|
else
|
|
|
|
builder.append_code_unit(AK::to_ascii_lowercase(code_unit));
|
|
|
|
|
|
|
|
next_is_upper = code_unit == u' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_utf16_string();
|
|
|
|
}
|
|
|
|
|
2025-08-06 07:17:20 -04:00
|
|
|
Utf16String Utf16View::replace(char16_t needle, Utf16View const& replacement, ReplaceMode replace_mode) const
|
|
|
|
{
|
|
|
|
return replace({ &needle, 1 }, replacement, replace_mode);
|
|
|
|
}
|
|
|
|
|
2025-06-27 12:30:25 -04:00
|
|
|
Utf16String Utf16View::replace(Utf16View const& needle, Utf16View const& replacement, ReplaceMode replace_mode) const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
StringBuilder builder(StringBuilder::Mode::UTF16, length_in_code_units());
|
|
|
|
auto remaining = *this;
|
|
|
|
|
|
|
|
do {
|
|
|
|
auto index = remaining.find_code_unit_offset(needle);
|
|
|
|
if (!index.has_value())
|
|
|
|
break;
|
|
|
|
|
|
|
|
builder.append(remaining.substring_view(0, *index));
|
|
|
|
builder.append(replacement);
|
|
|
|
|
|
|
|
remaining = remaining.substring_view(*index + needle.length_in_code_units());
|
|
|
|
index = remaining.find_code_unit_offset(needle);
|
|
|
|
} while (replace_mode == ReplaceMode::All && !remaining.is_empty());
|
|
|
|
|
|
|
|
builder.append(remaining);
|
|
|
|
return builder.to_utf16_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
Utf16String Utf16View::escape_html_entities() const
|
|
|
|
{
|
|
|
|
StringBuilder builder(StringBuilder::Mode::UTF16, length_in_code_units());
|
|
|
|
|
|
|
|
for (auto code_point : *this) {
|
|
|
|
if (code_point == '<')
|
|
|
|
builder.append(u"<"sv);
|
|
|
|
else if (code_point == '>')
|
|
|
|
builder.append(u">"sv);
|
|
|
|
else if (code_point == '&')
|
|
|
|
builder.append(u"&"sv);
|
|
|
|
else if (code_point == '"')
|
|
|
|
builder.append(u"""sv);
|
|
|
|
else
|
|
|
|
builder.append_code_point(code_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_utf16_string();
|
|
|
|
}
|
|
|
|
|
2025-06-26 12:52:23 -04:00
|
|
|
bool Utf16View::is_ascii() const
|
2021-08-01 18:56:52 -04:00
|
|
|
{
|
2025-06-12 19:29:41 -04:00
|
|
|
if (has_ascii_storage())
|
|
|
|
return true;
|
|
|
|
|
2025-07-09 13:54:56 -04:00
|
|
|
// FIXME: Petition simdutf to implement an ASCII validator for UTF-16.
|
2025-06-12 19:29:41 -04:00
|
|
|
return all_of(utf16_span(), AK::is_ascii);
|
2021-08-01 18:56:52 -04:00
|
|
|
}
|
|
|
|
|
2025-08-08 16:32:26 -04:00
|
|
|
bool Utf16View::validate() const
|
|
|
|
{
|
|
|
|
if (has_ascii_storage())
|
|
|
|
return true;
|
|
|
|
return simdutf::validate_utf16(m_string.utf16, length_in_code_units());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Utf16View::validate(size_t& valid_code_units) const
|
2021-07-20 22:25:48 -04:00
|
|
|
{
|
2025-06-12 19:29:41 -04:00
|
|
|
if (has_ascii_storage()) {
|
|
|
|
valid_code_units = length_in_code_units();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2025-08-08 16:32:26 -04:00
|
|
|
auto result = simdutf::validate_utf16_with_errors(m_string.utf16, length_in_code_units());
|
|
|
|
valid_code_units = result.count;
|
2021-07-20 22:25:48 -04:00
|
|
|
|
2025-08-08 16:32:26 -04:00
|
|
|
return result.error == simdutf::SUCCESS;
|
2021-07-20 22:25:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Utf16View::code_unit_offset_of(size_t code_point_offset) const
|
|
|
|
{
|
2025-07-22 09:46:15 -04:00
|
|
|
VERIFY(code_point_offset <= length_in_code_points());
|
|
|
|
|
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-07-22 09:46:15 -04:00
|
|
|
VERIFY(code_unit_offset <= length_in_code_units());
|
|
|
|
|
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-07-22 07:38:26 -04:00
|
|
|
for (auto it = begin(); it != end();) {
|
|
|
|
// We know the view is using UTF-16 storage because ASCII storage would have returned early above.
|
|
|
|
if ((++it).m_iterator.utf16 > m_string.utf16 + code_unit_offset)
|
|
|
|
break;
|
2025-06-26 12:52:23 -04:00
|
|
|
++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) {
|
2025-06-12 19:29:41 -04:00
|
|
|
if (has_ascii_storage())
|
|
|
|
return it.m_iterator.ascii - m_string.ascii;
|
|
|
|
return it.m_iterator.utf16 - m_string.utf16;
|
2025-06-26 12:52:23 -04:00
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2025-08-11 07:57:58 -04:00
|
|
|
Optional<size_t> Utf16View::find_code_unit_offset(char16_t needle, size_t start_offset) const
|
|
|
|
{
|
|
|
|
if (start_offset >= length_in_code_units())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (has_ascii_storage()) {
|
|
|
|
if (!AK::is_ascii(needle))
|
|
|
|
return {};
|
|
|
|
return StringUtils::find(bytes(), static_cast<char>(needle), start_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const* start = m_string.utf16 + start_offset;
|
|
|
|
auto const* end = m_string.utf16 + length_in_code_units();
|
|
|
|
|
|
|
|
auto const* result = simdutf::find(start, end, needle);
|
|
|
|
if (result == end)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return result - start + start_offset;
|
|
|
|
}
|
|
|
|
|
2025-07-26 10:09:06 -04:00
|
|
|
Vector<Utf16View> Utf16View::split_view(char16_t separator, SplitBehavior split_behavior) const
|
|
|
|
{
|
2025-08-26 11:00:23 +00:00
|
|
|
Utf16View separator_view { &separator, 1 };
|
|
|
|
return split_view(separator_view, split_behavior);
|
2025-07-26 10:09:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Utf16View> Utf16View::split_view(Utf16View const& separator, SplitBehavior split_behavior) const
|
|
|
|
{
|
|
|
|
Vector<Utf16View> parts;
|
|
|
|
|
|
|
|
for_each_split_view(separator, split_behavior, [&](auto const& part) {
|
|
|
|
parts.append(part);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
size_t Utf16View::calculate_length_in_code_points() const
|
|
|
|
{
|
2025-06-12 19:29:41 -04:00
|
|
|
ASSERT(!has_ascii_storage());
|
|
|
|
|
2025-06-26 19:52:09 -04:00
|
|
|
// simdutf's code point length method assumes valid UTF-16, whereas we allow lonely surrogates.
|
2025-08-08 16:32:26 -04:00
|
|
|
if (validate()) [[likely]]
|
2025-06-12 19:29:41 -04:00
|
|
|
return simdutf::count_utf16(m_string.utf16, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|