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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2023-01-06 13:19:34 -05:00
|
|
|
#include <AK/Error.h>
|
2021-08-09 12:29:42 -04:00
|
|
|
#include <AK/Format.h>
|
2021-07-19 09:02:13 -04:00
|
|
|
#include <AK/Forward.h>
|
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
|
#include <AK/Span.h>
|
2023-01-08 18:56:53 -05:00
|
|
|
#include <AK/String.h>
|
2025-06-19 09:18:07 -04:00
|
|
|
#include <AK/StringHash.h>
|
|
|
|
|
#include <AK/Traits.h>
|
2021-07-19 09:02:13 -04:00
|
|
|
#include <AK/Types.h>
|
2025-06-12 17:39:05 -04:00
|
|
|
#include <AK/UnicodeUtils.h>
|
2021-07-19 09:02:13 -04:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2023-01-06 11:00:24 -05:00
|
|
|
using Utf16Data = Vector<u16, 1>;
|
|
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
struct Utf16ConversionResult {
|
|
|
|
|
Utf16Data data;
|
|
|
|
|
size_t code_point_count;
|
|
|
|
|
};
|
2025-06-19 07:34:10 -04:00
|
|
|
ErrorOr<Utf16ConversionResult> utf8_to_utf16(StringView);
|
|
|
|
|
ErrorOr<Utf16ConversionResult> utf8_to_utf16(Utf8View const&);
|
|
|
|
|
ErrorOr<Utf16ConversionResult> utf32_to_utf16(Utf32View const&);
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2025-04-15 17:49:09 +02:00
|
|
|
[[nodiscard]] bool validate_utf16_le(ReadonlyBytes);
|
|
|
|
|
[[nodiscard]] bool validate_utf16_be(ReadonlyBytes);
|
|
|
|
|
|
2024-07-30 06:19:56 -04:00
|
|
|
size_t utf16_code_unit_length_from_utf8(StringView);
|
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
class Utf16View;
|
|
|
|
|
|
|
|
|
|
class Utf16CodePointIterator {
|
|
|
|
|
friend class Utf16View;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Utf16CodePointIterator() = default;
|
|
|
|
|
~Utf16CodePointIterator() = default;
|
|
|
|
|
|
|
|
|
|
bool operator==(Utf16CodePointIterator const& other) const
|
|
|
|
|
{
|
|
|
|
|
return (m_ptr == other.m_ptr) && (m_remaining_code_units == other.m_remaining_code_units);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utf16CodePointIterator& operator++();
|
|
|
|
|
u32 operator*() const;
|
|
|
|
|
|
2025-06-12 17:39:05 -04:00
|
|
|
size_t length_in_code_units() const
|
|
|
|
|
{
|
|
|
|
|
return UnicodeUtils::code_unit_length_for_code_point(**this);
|
|
|
|
|
}
|
2021-07-19 09:02:13 -04:00
|
|
|
|
|
|
|
|
private:
|
2025-04-15 17:58:42 +02:00
|
|
|
Utf16CodePointIterator(u16 const* ptr, size_t length)
|
2021-07-19 09:02:13 -04:00
|
|
|
: m_ptr(ptr)
|
|
|
|
|
, m_remaining_code_units(length)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u16 const* m_ptr { nullptr };
|
|
|
|
|
size_t m_remaining_code_units { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Utf16View {
|
|
|
|
|
public:
|
2023-11-08 10:12:34 -05:00
|
|
|
using Iterator = Utf16CodePointIterator;
|
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
Utf16View() = default;
|
|
|
|
|
~Utf16View() = default;
|
|
|
|
|
|
2025-04-15 17:58:42 +02:00
|
|
|
explicit Utf16View(ReadonlySpan<u16> code_units)
|
2021-07-19 09:02:13 -04:00
|
|
|
: m_code_units(code_units)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
Utf16View(Utf16ConversionResult&&) = delete;
|
|
|
|
|
explicit Utf16View(Utf16ConversionResult const& conversion_result)
|
|
|
|
|
: m_code_units(conversion_result.data)
|
|
|
|
|
, m_length_in_code_points(conversion_result.code_point_count)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 13:16:37 -05:00
|
|
|
template<size_t Size>
|
2025-04-15 17:58:42 +02:00
|
|
|
Utf16View(char16_t const (&code_units)[Size])
|
2024-01-03 13:16:37 -05:00
|
|
|
: m_code_units(
|
2024-04-24 06:53:44 -04:00
|
|
|
reinterpret_cast<u16 const*>(&code_units[0]),
|
|
|
|
|
code_units[Size - 1] == u'\0' ? Size - 1 : Size)
|
2024-01-03 13:16:37 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 17:49:02 +02:00
|
|
|
bool operator==(Utf16View const& other) const { return m_code_units == other.m_code_units; }
|
2021-07-19 09:02:13 -04:00
|
|
|
|
|
|
|
|
enum class AllowInvalidCodeUnits {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
ErrorOr<ByteString> to_byte_string(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
|
2023-01-08 18:56:53 -05:00
|
|
|
ErrorOr<String> to_utf8(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2025-04-02 17:56:49 +02:00
|
|
|
void unsafe_set_code_point_length(size_t length) const { m_length_in_code_points = length; }
|
|
|
|
|
|
2021-07-20 22:25:48 -04:00
|
|
|
bool is_null() const { return m_code_units.is_null(); }
|
2021-07-19 09:02:13 -04:00
|
|
|
bool is_empty() const { return m_code_units.is_empty(); }
|
2025-06-12 19:06:11 -04:00
|
|
|
bool is_ascii() const;
|
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
size_t length_in_code_units() const { return m_code_units.size(); }
|
|
|
|
|
size_t length_in_code_points() const;
|
|
|
|
|
|
2025-06-25 15:52:01 -04:00
|
|
|
Optional<size_t> length_in_code_points_if_known() const
|
|
|
|
|
{
|
|
|
|
|
if (m_length_in_code_points == NumericLimits<size_t>::max())
|
|
|
|
|
return {};
|
|
|
|
|
return m_length_in_code_points;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-19 09:18:07 -04:00
|
|
|
u32 hash() const
|
|
|
|
|
{
|
|
|
|
|
if (is_empty())
|
|
|
|
|
return 0;
|
|
|
|
|
return string_hash(reinterpret_cast<char const*>(m_code_units.data()), m_code_units.size() * sizeof(u16));
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 17:58:42 +02:00
|
|
|
Utf16CodePointIterator begin() const { return { begin_ptr(), m_code_units.size() }; }
|
|
|
|
|
Utf16CodePointIterator end() const { return { end_ptr(), 0 }; }
|
2021-07-19 09:02:13 -04:00
|
|
|
|
|
|
|
|
u16 const* data() const { return m_code_units.data(); }
|
2024-07-18 11:57:01 -04:00
|
|
|
char16_t const* char_data() const { return reinterpret_cast<char16_t const*>(data()); }
|
|
|
|
|
|
2024-10-26 11:05:31 +02:00
|
|
|
ReadonlySpan<u16> span() const { return m_code_units; }
|
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
u16 code_unit_at(size_t index) const;
|
2021-08-01 18:56:52 -04:00
|
|
|
u32 code_point_at(size_t index) const;
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2021-07-20 22:25:48 -04:00
|
|
|
size_t code_point_offset_of(size_t code_unit_offset) const;
|
|
|
|
|
size_t code_unit_offset_of(size_t code_point_offset) const;
|
2022-01-31 18:13:32 +02:00
|
|
|
size_t code_unit_offset_of(Utf16CodePointIterator const&) const;
|
2021-07-20 22:25:48 -04:00
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
Utf16View substring_view(size_t code_unit_offset, size_t code_unit_length) const;
|
|
|
|
|
Utf16View substring_view(size_t code_unit_offset) const { return substring_view(code_unit_offset, length_in_code_units() - code_unit_offset); }
|
|
|
|
|
|
2021-07-20 22:25:48 -04:00
|
|
|
Utf16View unicode_substring_view(size_t code_point_offset, size_t code_point_length) const;
|
|
|
|
|
Utf16View unicode_substring_view(size_t code_point_offset) const { return unicode_substring_view(code_point_offset, length_in_code_points() - code_point_offset); }
|
|
|
|
|
|
2025-06-10 16:04:10 +02:00
|
|
|
Optional<size_t> find_code_unit_offset(Utf16View const& needle, size_t start_offset = 0) const;
|
|
|
|
|
Optional<size_t> find_code_unit_offset_ignoring_case(Utf16View const& needle, size_t start_offset = 0) const;
|
|
|
|
|
|
2024-01-03 13:17:57 -05:00
|
|
|
bool starts_with(Utf16View const&) const;
|
2025-05-15 17:56:33 +12:00
|
|
|
bool is_code_unit_less_than(Utf16View const& other) const;
|
2024-01-03 13:17:57 -05:00
|
|
|
|
2025-06-18 11:04:18 -04:00
|
|
|
bool validate(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
|
|
|
|
|
bool validate(size_t& valid_code_units, AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
|
2021-07-19 09:02:13 -04:00
|
|
|
|
2021-07-21 16:38:12 -04:00
|
|
|
bool equals_ignoring_case(Utf16View const&) const;
|
|
|
|
|
|
2021-07-19 09:02:13 -04:00
|
|
|
private:
|
|
|
|
|
u16 const* begin_ptr() const { return m_code_units.data(); }
|
|
|
|
|
u16 const* end_ptr() const { return begin_ptr() + m_code_units.size(); }
|
|
|
|
|
|
|
|
|
|
size_t calculate_length_in_code_points() const;
|
|
|
|
|
|
2023-02-05 19:02:54 +00:00
|
|
|
ReadonlySpan<u16> m_code_units;
|
2025-04-15 18:08:26 +02:00
|
|
|
mutable size_t m_length_in_code_points { NumericLimits<size_t>::max() };
|
2021-07-19 09:02:13 -04:00
|
|
|
};
|
|
|
|
|
|
2021-08-09 12:29:42 -04:00
|
|
|
template<>
|
2025-06-19 09:18:07 -04:00
|
|
|
struct Formatter<Utf16View> : Formatter<FormatString> {
|
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Utf16View const& value)
|
2021-08-09 12:29:42 -04:00
|
|
|
{
|
2021-11-16 01:15:21 +01:00
|
|
|
return builder.builder().try_append(value);
|
2021-08-09 12:29:42 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-19 09:18:07 -04:00
|
|
|
template<>
|
|
|
|
|
struct Traits<Utf16View> : public DefaultTraits<Utf16View> {
|
|
|
|
|
using PeekType = Utf16View;
|
|
|
|
|
using ConstPeekType = Utf16View;
|
|
|
|
|
static unsigned hash(Utf16View const& s) { return s.hash(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 12:18:30 +01:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-01-06 11:00:24 -05:00
|
|
|
using AK::Utf16Data;
|
2021-07-19 09:02:13 -04:00
|
|
|
using AK::Utf16View;
|
2022-11-26 12:18:30 +01:00
|
|
|
#endif
|