Utf8View.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <AK/Assertions.h>
  8. #include <AK/Format.h>
  9. #include <AK/Utf8View.h>
  10. namespace AK {
  11. Utf8View::Utf8View(const String& string)
  12. : m_string(string)
  13. {
  14. }
  15. Utf8View::Utf8View(const StringView& string)
  16. : m_string(string)
  17. {
  18. }
  19. Utf8View::Utf8View(const char* string)
  20. : m_string(string)
  21. {
  22. }
  23. const unsigned char* Utf8View::begin_ptr() const
  24. {
  25. return (const unsigned char*)m_string.characters_without_null_termination();
  26. }
  27. const unsigned char* Utf8View::end_ptr() const
  28. {
  29. return begin_ptr() + m_string.length();
  30. }
  31. Utf8CodepointIterator Utf8View::begin() const
  32. {
  33. return { begin_ptr(), m_string.length() };
  34. }
  35. Utf8CodepointIterator Utf8View::end() const
  36. {
  37. return { end_ptr(), 0 };
  38. }
  39. size_t Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const
  40. {
  41. VERIFY(it.m_ptr >= begin_ptr());
  42. VERIFY(it.m_ptr <= end_ptr());
  43. return it.m_ptr - begin_ptr();
  44. }
  45. Utf8View Utf8View::substring_view(size_t byte_offset, size_t byte_length) const
  46. {
  47. StringView string = m_string.substring_view(byte_offset, byte_length);
  48. return Utf8View { string };
  49. }
  50. Utf8View Utf8View::unicode_substring_view(size_t codepoint_offset, size_t codepoint_length) const
  51. {
  52. if (codepoint_length == 0)
  53. return {};
  54. size_t codepoint_index = 0, offset_in_bytes = 0;
  55. for (auto iterator = begin(); !iterator.done(); ++iterator) {
  56. if (codepoint_index == codepoint_offset)
  57. offset_in_bytes = byte_offset_of(iterator);
  58. if (codepoint_index == codepoint_offset + codepoint_length - 1) {
  59. size_t length_in_bytes = byte_offset_of(++iterator) - offset_in_bytes;
  60. return substring_view(offset_in_bytes, length_in_bytes);
  61. }
  62. ++codepoint_index;
  63. }
  64. VERIFY_NOT_REACHED();
  65. }
  66. static inline bool decode_first_byte(
  67. unsigned char byte,
  68. size_t& out_code_point_length_in_bytes,
  69. u32& out_value)
  70. {
  71. if ((byte & 128) == 0) {
  72. out_value = byte;
  73. out_code_point_length_in_bytes = 1;
  74. return true;
  75. }
  76. if ((byte & 64) == 0) {
  77. return false;
  78. }
  79. if ((byte & 32) == 0) {
  80. out_value = byte & 31;
  81. out_code_point_length_in_bytes = 2;
  82. return true;
  83. }
  84. if ((byte & 16) == 0) {
  85. out_value = byte & 15;
  86. out_code_point_length_in_bytes = 3;
  87. return true;
  88. }
  89. if ((byte & 8) == 0) {
  90. out_value = byte & 7;
  91. out_code_point_length_in_bytes = 4;
  92. return true;
  93. }
  94. return false;
  95. }
  96. bool Utf8View::validate(size_t& valid_bytes) const
  97. {
  98. valid_bytes = 0;
  99. for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
  100. size_t code_point_length_in_bytes;
  101. u32 value;
  102. bool first_byte_makes_sense = decode_first_byte(*ptr, code_point_length_in_bytes, value);
  103. if (!first_byte_makes_sense)
  104. return false;
  105. for (size_t i = 1; i < code_point_length_in_bytes; i++) {
  106. ptr++;
  107. if (ptr >= end_ptr())
  108. return false;
  109. if (*ptr >> 6 != 2)
  110. return false;
  111. }
  112. valid_bytes += code_point_length_in_bytes;
  113. }
  114. return true;
  115. }
  116. size_t Utf8View::calculate_length() const
  117. {
  118. size_t length = 0;
  119. for ([[maybe_unused]] auto code_point : *this) {
  120. ++length;
  121. }
  122. return length;
  123. }
  124. bool Utf8View::starts_with(const Utf8View& start) const
  125. {
  126. if (start.is_empty())
  127. return true;
  128. if (is_empty())
  129. return false;
  130. if (start.length() > length())
  131. return false;
  132. if (begin_ptr() == start.begin_ptr())
  133. return true;
  134. for (auto k = begin(), l = start.begin(); l != start.end(); ++k, ++l) {
  135. if (*k != *l)
  136. return false;
  137. }
  138. return true;
  139. }
  140. Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, size_t length)
  141. : m_ptr(ptr)
  142. , m_length(length)
  143. {
  144. }
  145. bool Utf8CodepointIterator::operator==(const Utf8CodepointIterator& other) const
  146. {
  147. return m_ptr == other.m_ptr && m_length == other.m_length;
  148. }
  149. bool Utf8CodepointIterator::operator!=(const Utf8CodepointIterator& other) const
  150. {
  151. return !(*this == other);
  152. }
  153. Utf8CodepointIterator& Utf8CodepointIterator::operator++()
  154. {
  155. VERIFY(m_length > 0);
  156. size_t code_point_length_in_bytes = 0;
  157. u32 value;
  158. bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
  159. VERIFY(first_byte_makes_sense);
  160. VERIFY(code_point_length_in_bytes <= m_length);
  161. m_ptr += code_point_length_in_bytes;
  162. m_length -= code_point_length_in_bytes;
  163. return *this;
  164. }
  165. size_t Utf8CodepointIterator::code_point_length_in_bytes() const
  166. {
  167. VERIFY(m_length > 0);
  168. size_t code_point_length_in_bytes = 0;
  169. u32 value;
  170. bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
  171. VERIFY(first_byte_makes_sense);
  172. return code_point_length_in_bytes;
  173. }
  174. u32 Utf8CodepointIterator::operator*() const
  175. {
  176. VERIFY(m_length > 0);
  177. u32 code_point_value_so_far = 0;
  178. size_t code_point_length_in_bytes = 0;
  179. bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far);
  180. if (!first_byte_makes_sense)
  181. dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, m_length });
  182. VERIFY(first_byte_makes_sense);
  183. if (code_point_length_in_bytes > m_length)
  184. dbgln("Not enough bytes (need {}, have {}), first byte is: {:#02x}, '{}'", code_point_length_in_bytes, m_length, m_ptr[0], (const char*)m_ptr);
  185. VERIFY(code_point_length_in_bytes <= m_length);
  186. for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
  187. VERIFY(m_ptr[offset] >> 6 == 2);
  188. code_point_value_so_far <<= 6;
  189. code_point_value_so_far |= m_ptr[offset] & 63;
  190. }
  191. return code_point_value_so_far;
  192. }
  193. }