2021-07-19 13:02:13 +00:00
|
|
|
/*
|
2023-01-06 16:00:24 +00:00
|
|
|
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
|
2021-07-19 13:02:13 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-01-06 18:19:34 +00:00
|
|
|
#include <AK/Error.h>
|
2021-08-09 16:29:42 +00:00
|
|
|
#include <AK/Format.h>
|
2021-07-19 13:02:13 +00:00
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
#include <AK/Span.h>
|
2023-01-08 23:56:53 +00:00
|
|
|
#include <AK/String.h>
|
2021-07-19 13:02:13 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-01-06 16:00:24 +00:00
|
|
|
using Utf16Data = Vector<u16, 1>;
|
|
|
|
|
2023-01-06 18:19:34 +00:00
|
|
|
ErrorOr<Utf16Data> utf8_to_utf16(StringView);
|
|
|
|
ErrorOr<Utf16Data> utf8_to_utf16(Utf8View const&);
|
|
|
|
ErrorOr<Utf16Data> utf32_to_utf16(Utf32View const&);
|
|
|
|
ErrorOr<void> code_point_to_utf16(Utf16Data&, u32);
|
2021-07-19 13:02:13 +00: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;
|
|
|
|
|
|
|
|
size_t length_in_code_units() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Utf16CodePointIterator(u16 const* ptr, size_t length)
|
|
|
|
: m_ptr(ptr)
|
|
|
|
, m_remaining_code_units(length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 const* m_ptr { nullptr };
|
|
|
|
size_t m_remaining_code_units { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
class Utf16View {
|
|
|
|
public:
|
|
|
|
static bool is_high_surrogate(u16);
|
|
|
|
static bool is_low_surrogate(u16);
|
|
|
|
static u32 decode_surrogate_pair(u16 high_surrogate, u16 low_surrogate);
|
|
|
|
|
|
|
|
Utf16View() = default;
|
|
|
|
~Utf16View() = default;
|
|
|
|
|
|
|
|
explicit Utf16View(Span<u16 const> code_units)
|
|
|
|
: m_code_units(code_units)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-02 15:49:02 +00:00
|
|
|
bool operator==(Utf16View const& other) const { return m_code_units == other.m_code_units; }
|
2021-07-19 13:02:13 +00:00
|
|
|
|
|
|
|
enum class AllowInvalidCodeUnits {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2023-01-08 23:46:02 +00:00
|
|
|
ErrorOr<DeprecatedString> to_deprecated_string(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
|
2023-01-08 23:56:53 +00:00
|
|
|
ErrorOr<String> to_utf8(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
|
2021-07-19 13:02:13 +00:00
|
|
|
|
2021-07-21 02:25:48 +00:00
|
|
|
bool is_null() const { return m_code_units.is_null(); }
|
2021-07-19 13:02:13 +00:00
|
|
|
bool is_empty() const { return m_code_units.is_empty(); }
|
|
|
|
size_t length_in_code_units() const { return m_code_units.size(); }
|
|
|
|
size_t length_in_code_points() const;
|
|
|
|
|
|
|
|
Utf16CodePointIterator begin() const { return { begin_ptr(), m_code_units.size() }; }
|
|
|
|
Utf16CodePointIterator end() const { return { end_ptr(), 0 }; }
|
|
|
|
|
|
|
|
u16 const* data() const { return m_code_units.data(); }
|
|
|
|
u16 code_unit_at(size_t index) const;
|
2021-08-01 22:56:52 +00:00
|
|
|
u32 code_point_at(size_t index) const;
|
2021-07-19 13:02:13 +00:00
|
|
|
|
2021-07-21 02:25:48 +00: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 16:13:32 +00:00
|
|
|
size_t code_unit_offset_of(Utf16CodePointIterator const&) const;
|
2021-07-21 02:25:48 +00:00
|
|
|
|
2021-07-19 13:02:13 +00: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-21 02:25:48 +00: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); }
|
|
|
|
|
2021-07-19 13:02:13 +00:00
|
|
|
bool validate(size_t& valid_code_units) const;
|
|
|
|
bool validate() const
|
|
|
|
{
|
|
|
|
size_t valid_code_units;
|
|
|
|
return validate(valid_code_units);
|
|
|
|
}
|
|
|
|
|
2021-07-21 20:38:12 +00:00
|
|
|
bool equals_ignoring_case(Utf16View const&) const;
|
|
|
|
|
2021-07-19 13:02:13 +00: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;
|
|
|
|
|
|
|
|
Span<u16 const> m_code_units;
|
|
|
|
mutable Optional<size_t> m_length_in_code_points;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-09 16:29:42 +00:00
|
|
|
template<>
|
|
|
|
struct AK::Formatter<AK::Utf16View> : Formatter<FormatString> {
|
2021-11-16 00:15:21 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, AK::Utf16View const& value)
|
2021-08-09 16:29:42 +00:00
|
|
|
{
|
2021-11-16 00:15:21 +00:00
|
|
|
return builder.builder().try_append(value);
|
2021-08-09 16:29:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-01-06 16:00:24 +00:00
|
|
|
using AK::Utf16Data;
|
2021-07-19 13:02:13 +00:00
|
|
|
using AK::Utf16View;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|