Utf16View.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Format.h>
  8. #include <AK/Forward.h>
  9. #include <AK/Optional.h>
  10. #include <AK/Span.h>
  11. #include <AK/String.h>
  12. #include <AK/Types.h>
  13. #include <AK/Vector.h>
  14. namespace AK {
  15. Vector<u16, 1> utf8_to_utf16(StringView);
  16. Vector<u16, 1> utf8_to_utf16(Utf8View const&);
  17. Vector<u16, 1> utf32_to_utf16(Utf32View const&);
  18. void code_point_to_utf16(Vector<u16, 1>&, u32);
  19. class Utf16View;
  20. class Utf16CodePointIterator {
  21. friend class Utf16View;
  22. public:
  23. Utf16CodePointIterator() = default;
  24. ~Utf16CodePointIterator() = default;
  25. bool operator==(Utf16CodePointIterator const& other) const
  26. {
  27. return (m_ptr == other.m_ptr) && (m_remaining_code_units == other.m_remaining_code_units);
  28. }
  29. bool operator!=(Utf16CodePointIterator const& other) const
  30. {
  31. return !(*this == other);
  32. }
  33. Utf16CodePointIterator& operator++();
  34. u32 operator*() const;
  35. size_t length_in_code_units() const;
  36. private:
  37. Utf16CodePointIterator(u16 const* ptr, size_t length)
  38. : m_ptr(ptr)
  39. , m_remaining_code_units(length)
  40. {
  41. }
  42. u16 const* m_ptr { nullptr };
  43. size_t m_remaining_code_units { 0 };
  44. };
  45. class Utf16View {
  46. public:
  47. static bool is_high_surrogate(u16);
  48. static bool is_low_surrogate(u16);
  49. static u32 decode_surrogate_pair(u16 high_surrogate, u16 low_surrogate);
  50. Utf16View() = default;
  51. ~Utf16View() = default;
  52. explicit Utf16View(Span<u16 const> code_units)
  53. : m_code_units(code_units)
  54. {
  55. }
  56. bool operator==(Utf16View const& other) const { return m_code_units == other.m_code_units; }
  57. enum class AllowInvalidCodeUnits {
  58. Yes,
  59. No,
  60. };
  61. String to_utf8(AllowInvalidCodeUnits = AllowInvalidCodeUnits::No) const;
  62. bool is_null() const { return m_code_units.is_null(); }
  63. bool is_empty() const { return m_code_units.is_empty(); }
  64. size_t length_in_code_units() const { return m_code_units.size(); }
  65. size_t length_in_code_points() const;
  66. Utf16CodePointIterator begin() const { return { begin_ptr(), m_code_units.size() }; }
  67. Utf16CodePointIterator end() const { return { end_ptr(), 0 }; }
  68. u16 const* data() const { return m_code_units.data(); }
  69. u16 code_unit_at(size_t index) const;
  70. u32 code_point_at(size_t index) const;
  71. size_t code_point_offset_of(size_t code_unit_offset) const;
  72. size_t code_unit_offset_of(size_t code_point_offset) const;
  73. Utf16View substring_view(size_t code_unit_offset, size_t code_unit_length) const;
  74. Utf16View substring_view(size_t code_unit_offset) const { return substring_view(code_unit_offset, length_in_code_units() - code_unit_offset); }
  75. Utf16View unicode_substring_view(size_t code_point_offset, size_t code_point_length) const;
  76. Utf16View unicode_substring_view(size_t code_point_offset) const { return unicode_substring_view(code_point_offset, length_in_code_points() - code_point_offset); }
  77. bool validate(size_t& valid_code_units) const;
  78. bool validate() const
  79. {
  80. size_t valid_code_units;
  81. return validate(valid_code_units);
  82. }
  83. bool equals_ignoring_case(Utf16View const&) const;
  84. private:
  85. u16 const* begin_ptr() const { return m_code_units.data(); }
  86. u16 const* end_ptr() const { return begin_ptr() + m_code_units.size(); }
  87. size_t calculate_length_in_code_points() const;
  88. Span<u16 const> m_code_units;
  89. mutable Optional<size_t> m_length_in_code_points;
  90. };
  91. }
  92. template<>
  93. struct AK::Formatter<AK::Utf16View> : Formatter<FormatString> {
  94. ErrorOr<void> format(FormatBuilder& builder, AK::Utf16View const& value)
  95. {
  96. return builder.builder().try_append(value);
  97. }
  98. };
  99. using AK::Utf16View;