Utf8View.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #define AK_DONT_REPLACE_STD
  8. #include <AK/Assertions.h>
  9. #include <AK/Debug.h>
  10. #include <AK/Format.h>
  11. #include <AK/Utf8View.h>
  12. #include <simdutf.h>
  13. namespace AK {
  14. Utf8CodePointIterator Utf8View::iterator_at_byte_offset(size_t byte_offset) const
  15. {
  16. size_t current_offset = 0;
  17. for (auto iterator = begin(); !iterator.done(); ++iterator) {
  18. if (current_offset >= byte_offset)
  19. return iterator;
  20. current_offset += iterator.underlying_code_point_length_in_bytes();
  21. }
  22. return end();
  23. }
  24. Utf8CodePointIterator Utf8View::iterator_at_byte_offset_without_validation(size_t byte_offset) const
  25. {
  26. return Utf8CodePointIterator { reinterpret_cast<u8 const*>(m_string.characters_without_null_termination()) + byte_offset, m_string.length() - byte_offset };
  27. }
  28. size_t Utf8View::byte_offset_of(Utf8CodePointIterator const& it) const
  29. {
  30. VERIFY(it.m_ptr >= begin_ptr());
  31. VERIFY(it.m_ptr <= end_ptr());
  32. return it.m_ptr - begin_ptr();
  33. }
  34. size_t Utf8View::byte_offset_of(size_t code_point_offset) const
  35. {
  36. size_t byte_offset = 0;
  37. for (auto it = begin(); !it.done(); ++it) {
  38. if (code_point_offset == 0)
  39. return byte_offset;
  40. byte_offset += it.underlying_code_point_length_in_bytes();
  41. --code_point_offset;
  42. }
  43. return byte_offset;
  44. }
  45. Utf8View Utf8View::unicode_substring_view(size_t code_point_offset, size_t code_point_length) const
  46. {
  47. if (code_point_length == 0)
  48. return {};
  49. size_t code_point_index = 0, offset_in_bytes = 0;
  50. for (auto iterator = begin(); !iterator.done(); ++iterator) {
  51. if (code_point_index == code_point_offset)
  52. offset_in_bytes = byte_offset_of(iterator);
  53. if (code_point_index == code_point_offset + code_point_length - 1) {
  54. size_t length_in_bytes = byte_offset_of(++iterator) - offset_in_bytes;
  55. return substring_view(offset_in_bytes, length_in_bytes);
  56. }
  57. ++code_point_index;
  58. }
  59. VERIFY_NOT_REACHED();
  60. }
  61. size_t Utf8View::calculate_length() const
  62. {
  63. // FIXME: simdutf's code point length method assumes valid UTF-8, whereas Utf8View uses U+FFFD as a replacement
  64. // for invalid code points. If we change Utf8View to only accept valid encodings as an invariant, we can
  65. // remove this branch.
  66. if (validate()) [[likely]]
  67. return simdutf::count_utf8(m_string.characters_without_null_termination(), m_string.length());
  68. size_t length = 0;
  69. for (size_t i = 0; i < m_string.length(); ++length) {
  70. auto [byte_length, code_point, is_valid] = decode_leading_byte(static_cast<u8>(m_string[i]));
  71. // Similar to Utf8CodePointIterator::operator++, if the byte is invalid, try the next byte.
  72. i += is_valid ? byte_length : 1;
  73. }
  74. return length;
  75. }
  76. bool Utf8View::starts_with(Utf8View const& start) const
  77. {
  78. if (start.is_empty())
  79. return true;
  80. if (is_empty())
  81. return false;
  82. if (start.length() > length())
  83. return false;
  84. if (begin_ptr() == start.begin_ptr())
  85. return true;
  86. for (auto k = begin(), l = start.begin(); l != start.end(); ++k, ++l) {
  87. if (*k != *l)
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool Utf8View::contains(u32 needle) const
  93. {
  94. for (u32 code_point : *this) {
  95. if (code_point == needle)
  96. return true;
  97. }
  98. return false;
  99. }
  100. Utf8View Utf8View::trim(Utf8View const& characters, TrimMode mode) const
  101. {
  102. size_t substring_start = 0;
  103. size_t substring_length = byte_length();
  104. if (mode == TrimMode::Left || mode == TrimMode::Both) {
  105. for (auto code_point = begin(); code_point != end(); ++code_point) {
  106. if (substring_length == 0)
  107. return {};
  108. if (!characters.contains(*code_point))
  109. break;
  110. substring_start += code_point.underlying_code_point_length_in_bytes();
  111. substring_length -= code_point.underlying_code_point_length_in_bytes();
  112. }
  113. }
  114. if (mode == TrimMode::Right || mode == TrimMode::Both) {
  115. size_t seen_whitespace_length = 0;
  116. for (auto code_point = begin(); code_point != end(); ++code_point) {
  117. if (characters.contains(*code_point))
  118. seen_whitespace_length += code_point.underlying_code_point_length_in_bytes();
  119. else
  120. seen_whitespace_length = 0;
  121. }
  122. if (seen_whitespace_length >= substring_length)
  123. return {};
  124. substring_length -= seen_whitespace_length;
  125. }
  126. return substring_view(substring_start, substring_length);
  127. }
  128. bool Utf8View::validate(size_t& valid_bytes, AllowSurrogates allow_surrogates) const
  129. {
  130. auto result = simdutf::validate_utf8_with_errors(m_string.characters_without_null_termination(), m_string.length());
  131. valid_bytes = result.count;
  132. if (result.error == simdutf::SURROGATE && allow_surrogates == AllowSurrogates::Yes) {
  133. valid_bytes += 3; // All surrogates have a UTF-8 byte length of 3.
  134. size_t substring_valid_bytes = 0;
  135. auto is_valid = substring_view(valid_bytes).validate(substring_valid_bytes, allow_surrogates);
  136. valid_bytes += substring_valid_bytes;
  137. return is_valid;
  138. }
  139. return result.error == simdutf::SUCCESS;
  140. }
  141. Utf8CodePointIterator& Utf8CodePointIterator::operator++()
  142. {
  143. VERIFY(m_length > 0);
  144. // OPTIMIZATION: Fast path for ASCII characters.
  145. if (*m_ptr <= 0x7F) {
  146. m_ptr += 1;
  147. m_length -= 1;
  148. return *this;
  149. }
  150. size_t code_point_length_in_bytes = underlying_code_point_length_in_bytes();
  151. if (code_point_length_in_bytes > m_length) {
  152. // We don't have enough data for the next code point. Skip one character and try again.
  153. // The rest of the code will output replacement characters as needed for any eventual extension bytes we might encounter afterwards.
  154. dbgln_if(UTF8_DEBUG, "Expected code point size {} is too big for the remaining length {}. Moving forward one byte.", code_point_length_in_bytes, m_length);
  155. m_ptr += 1;
  156. m_length -= 1;
  157. return *this;
  158. }
  159. m_ptr += code_point_length_in_bytes;
  160. m_length -= code_point_length_in_bytes;
  161. return *this;
  162. }
  163. size_t Utf8CodePointIterator::underlying_code_point_length_in_bytes() const
  164. {
  165. VERIFY(m_length > 0);
  166. auto [code_point_length_in_bytes, value, first_byte_makes_sense] = Utf8View::decode_leading_byte(*m_ptr);
  167. // If any of these tests fail, we will output a replacement character for this byte and treat it as a code point of size 1.
  168. if (!first_byte_makes_sense)
  169. return 1;
  170. if (code_point_length_in_bytes > m_length)
  171. return 1;
  172. for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
  173. if (m_ptr[offset] >> 6 != 2)
  174. return 1;
  175. }
  176. return code_point_length_in_bytes;
  177. }
  178. ReadonlyBytes Utf8CodePointIterator::underlying_code_point_bytes() const
  179. {
  180. return { m_ptr, underlying_code_point_length_in_bytes() };
  181. }
  182. u32 Utf8CodePointIterator::operator*() const
  183. {
  184. VERIFY(m_length > 0);
  185. // OPTIMIZATION: Fast path for ASCII characters.
  186. if (*m_ptr <= 0x7F)
  187. return *m_ptr;
  188. auto [code_point_length_in_bytes, code_point_value_so_far, first_byte_makes_sense] = Utf8View::decode_leading_byte(*m_ptr);
  189. if (!first_byte_makes_sense) {
  190. // The first byte of the code point doesn't make sense: output a replacement character
  191. dbgln_if(UTF8_DEBUG, "First byte doesn't make sense: {:#02x}.", m_ptr[0]);
  192. return 0xFFFD;
  193. }
  194. if (code_point_length_in_bytes > m_length) {
  195. // There is not enough data left for the full code point: output a replacement character
  196. dbgln_if(UTF8_DEBUG, "Not enough bytes (need {}, have {}), first byte is: {:#02x}.", code_point_length_in_bytes, m_length, m_ptr[0]);
  197. return 0xFFFD;
  198. }
  199. for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
  200. if (m_ptr[offset] >> 6 != 2) {
  201. // One of the extension bytes of the code point doesn't make sense: output a replacement character
  202. dbgln_if(UTF8_DEBUG, "Extension byte {:#02x} in {} position after first byte {:#02x} doesn't make sense.", m_ptr[offset], offset, m_ptr[0]);
  203. return 0xFFFD;
  204. }
  205. code_point_value_so_far <<= 6;
  206. code_point_value_so_far |= m_ptr[offset] & 63;
  207. }
  208. if (code_point_value_so_far > 0x10FFFF) {
  209. dbgln_if(UTF8_DEBUG, "Multi-byte sequence is otherwise valid, but code point {:#x} is not permissible.", code_point_value_so_far);
  210. return 0xFFFD;
  211. }
  212. return code_point_value_so_far;
  213. }
  214. Optional<u32> Utf8CodePointIterator::peek(size_t offset) const
  215. {
  216. if (offset == 0) {
  217. if (this->done())
  218. return {};
  219. return this->operator*();
  220. }
  221. auto new_iterator = *this;
  222. for (size_t index = 0; index < offset; ++index) {
  223. ++new_iterator;
  224. if (new_iterator.done())
  225. return {};
  226. }
  227. return *new_iterator;
  228. }
  229. ErrorOr<void> Formatter<Utf8View>::format(FormatBuilder& builder, Utf8View const& string)
  230. {
  231. return Formatter<StringView>::format(builder, string.as_string());
  232. }
  233. }