Utf16View.cpp 8.3 KB

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