Utf16View.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/StringBuilder.h>
  8. #include <AK/StringView.h>
  9. #include <AK/Utf16View.h>
  10. #include <AK/Utf32View.h>
  11. #include <AK/Utf8View.h>
  12. namespace AK {
  13. static constexpr u16 high_surrogate_min = 0xd800;
  14. static constexpr u16 high_surrogate_max = 0xdbff;
  15. static constexpr u16 low_surrogate_min = 0xdc00;
  16. static constexpr u16 low_surrogate_max = 0xdfff;
  17. static constexpr u32 replacement_code_point = 0xfffd;
  18. static constexpr u32 first_supplementary_plane_code_point = 0x10000;
  19. template<typename UtfViewType>
  20. static Vector<u16> to_utf16_impl(UtfViewType const& view) requires(IsSame<UtfViewType, Utf8View> || IsSame<UtfViewType, Utf32View>)
  21. {
  22. Vector<u16> utf16_data;
  23. for (auto code_point : view) {
  24. if (code_point < first_supplementary_plane_code_point) {
  25. utf16_data.append(static_cast<u16>(code_point));
  26. } else {
  27. code_point -= first_supplementary_plane_code_point;
  28. utf16_data.append(static_cast<u16>(high_surrogate_min | (code_point >> 10)));
  29. utf16_data.append(static_cast<u16>(low_surrogate_min | (code_point & 0x3ff)));
  30. }
  31. }
  32. return utf16_data;
  33. }
  34. Vector<u16> utf8_to_utf16(StringView const& utf8_view)
  35. {
  36. return to_utf16_impl(Utf8View { utf8_view });
  37. }
  38. Vector<u16> utf8_to_utf16(Utf8View const& utf8_view)
  39. {
  40. return to_utf16_impl(utf8_view);
  41. }
  42. Vector<u16> utf32_to_utf16(Utf32View const& utf32_view)
  43. {
  44. return to_utf16_impl(utf32_view);
  45. }
  46. bool Utf16View::is_high_surrogate(u16 code_unit)
  47. {
  48. return (code_unit >= high_surrogate_min) && (code_unit <= high_surrogate_max);
  49. }
  50. bool Utf16View::is_low_surrogate(u16 code_unit)
  51. {
  52. return (code_unit >= low_surrogate_min) && (code_unit <= low_surrogate_max);
  53. }
  54. u32 Utf16View::decode_surrogate_pair(u16 high_surrogate, u16 low_surrogate)
  55. {
  56. VERIFY(is_high_surrogate(high_surrogate));
  57. VERIFY(is_low_surrogate(low_surrogate));
  58. return ((high_surrogate - high_surrogate_min) << 10) + (low_surrogate - low_surrogate_min) + first_supplementary_plane_code_point;
  59. }
  60. String Utf16View::to_utf8(AllowInvalidCodeUnits allow_invalid_code_units) const
  61. {
  62. StringBuilder builder;
  63. if (allow_invalid_code_units == AllowInvalidCodeUnits::Yes) {
  64. for (auto const* ptr = begin_ptr(); ptr < end_ptr(); ++ptr) {
  65. if (is_high_surrogate(*ptr)) {
  66. auto const* next = ptr + 1;
  67. if ((next < end_ptr()) && is_low_surrogate(*next)) {
  68. auto code_point = decode_surrogate_pair(*ptr, *next);
  69. builder.append_code_point(code_point);
  70. ++ptr;
  71. continue;
  72. }
  73. }
  74. builder.append_code_point(static_cast<u32>(*ptr));
  75. }
  76. } else {
  77. for (auto code_point : *this)
  78. builder.append_code_point(code_point);
  79. }
  80. return builder.build();
  81. }
  82. size_t Utf16View::length_in_code_points() const
  83. {
  84. if (!m_length_in_code_points.has_value())
  85. m_length_in_code_points = calculate_length_in_code_points();
  86. return *m_length_in_code_points;
  87. }
  88. u16 Utf16View::code_unit_at(size_t index) const
  89. {
  90. VERIFY(index < length_in_code_units());
  91. return m_code_units[index];
  92. }
  93. size_t Utf16View::code_point_offset_of(size_t code_unit_offset) const
  94. {
  95. size_t code_point_offset = 0;
  96. for (auto it = begin(); it != end(); ++it) {
  97. if (code_unit_offset == 0)
  98. return code_point_offset;
  99. code_unit_offset -= it.length_in_code_units();
  100. ++code_point_offset;
  101. }
  102. return code_point_offset;
  103. }
  104. size_t Utf16View::code_unit_offset_of(size_t code_point_offset) const
  105. {
  106. size_t code_unit_offset = 0;
  107. for (auto it = begin(); it != end(); ++it) {
  108. if (code_point_offset == 0)
  109. return code_unit_offset;
  110. code_unit_offset += it.length_in_code_units();
  111. --code_point_offset;
  112. }
  113. return code_unit_offset;
  114. }
  115. Utf16View Utf16View::substring_view(size_t code_unit_offset, size_t code_unit_length) const
  116. {
  117. VERIFY(!Checked<size_t>::addition_would_overflow(code_unit_offset, code_unit_length));
  118. VERIFY(code_unit_offset + code_unit_length <= length_in_code_units());
  119. return Utf16View { m_code_units.slice(code_unit_offset, code_unit_length) };
  120. }
  121. Utf16View Utf16View::unicode_substring_view(size_t code_point_offset, size_t code_point_length) const
  122. {
  123. if (code_point_length == 0)
  124. return {};
  125. auto code_unit_offset_of = [&](Utf16CodePointIterator const& it) { return it.m_ptr - begin_ptr(); };
  126. size_t code_point_index = 0;
  127. size_t code_unit_offset = 0;
  128. for (auto it = begin(); it != end(); ++it) {
  129. if (code_point_index == code_point_offset)
  130. code_unit_offset = code_unit_offset_of(it);
  131. if (code_point_index == (code_point_offset + code_point_length - 1)) {
  132. size_t code_unit_length = code_unit_offset_of(++it) - code_unit_offset;
  133. return substring_view(code_unit_offset, code_unit_length);
  134. }
  135. ++code_point_index;
  136. }
  137. VERIFY_NOT_REACHED();
  138. }
  139. bool Utf16View::validate(size_t& valid_code_units) const
  140. {
  141. valid_code_units = 0;
  142. for (auto const* ptr = begin_ptr(); ptr < end_ptr(); ++ptr) {
  143. if (is_high_surrogate(*ptr)) {
  144. if ((++ptr >= end_ptr()) || !is_low_surrogate(*ptr))
  145. return false;
  146. ++valid_code_units;
  147. } else if (is_low_surrogate(*ptr)) {
  148. return false;
  149. }
  150. ++valid_code_units;
  151. }
  152. return true;
  153. }
  154. size_t Utf16View::calculate_length_in_code_points() const
  155. {
  156. size_t code_points = 0;
  157. for ([[maybe_unused]] auto code_point : *this)
  158. ++code_points;
  159. return code_points;
  160. }
  161. bool Utf16View::operator==(Utf16View const& other) const
  162. {
  163. if (length_in_code_units() == 0)
  164. return other.length_in_code_units() == 0;
  165. if (length_in_code_units() != other.length_in_code_units())
  166. return false;
  167. for (size_t i = 0; i < length_in_code_units(); ++i) {
  168. if (m_code_units[i] != other.m_code_units[i])
  169. return false;
  170. }
  171. return true;
  172. }
  173. bool Utf16View::equals_ignoring_case(Utf16View const& other) const
  174. {
  175. if (length_in_code_units() == 0)
  176. return other.length_in_code_units() == 0;
  177. if (length_in_code_units() != other.length_in_code_units())
  178. return false;
  179. for (size_t i = 0; i < length_in_code_units(); ++i) {
  180. // FIXME: Handle non-ASCII case insensitive comparisons.
  181. if (to_ascii_lowercase(m_code_units[i]) != to_ascii_lowercase(other.m_code_units[i]))
  182. return false;
  183. }
  184. return true;
  185. }
  186. Utf16CodePointIterator& Utf16CodePointIterator::operator++()
  187. {
  188. size_t code_units = length_in_code_units();
  189. if (code_units > m_remaining_code_units) {
  190. // If there aren't enough code units remaining, skip to the end.
  191. m_ptr += m_remaining_code_units;
  192. m_remaining_code_units = 0;
  193. } else {
  194. m_ptr += code_units;
  195. m_remaining_code_units -= code_units;
  196. }
  197. return *this;
  198. }
  199. u32 Utf16CodePointIterator::operator*() const
  200. {
  201. VERIFY(m_remaining_code_units > 0);
  202. if (Utf16View::is_high_surrogate(*m_ptr)) {
  203. if ((m_remaining_code_units > 1) && Utf16View::is_low_surrogate(*(m_ptr + 1)))
  204. return Utf16View::decode_surrogate_pair(*m_ptr, *(m_ptr + 1));
  205. return replacement_code_point;
  206. } else if (Utf16View::is_low_surrogate(*m_ptr)) {
  207. return replacement_code_point;
  208. }
  209. return static_cast<u32>(*m_ptr);
  210. }
  211. size_t Utf16CodePointIterator::length_in_code_units() const
  212. {
  213. VERIFY(m_remaining_code_units > 0);
  214. if (Utf16View::is_high_surrogate(*m_ptr)) {
  215. if ((m_remaining_code_units > 1) && Utf16View::is_low_surrogate(*(m_ptr + 1)))
  216. return 2;
  217. }
  218. // If this return is reached, either the encoded code point is a valid single code unit, or that
  219. // code point is invalid (e.g. began with a low surrogate, or a low surrogate did not follow a
  220. // high surrogate). In the latter case, a single replacement code unit will be used.
  221. return 1;
  222. }
  223. }