Utf8View.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/LogStream.h>
  28. #include <AK/Utf8View.h>
  29. namespace AK {
  30. Utf8View::Utf8View(const String& string)
  31. : m_string(string)
  32. {
  33. }
  34. Utf8View::Utf8View(const StringView& string)
  35. : m_string(string)
  36. {
  37. }
  38. Utf8View::Utf8View(const char* string)
  39. : m_string(string)
  40. {
  41. }
  42. const unsigned char* Utf8View::begin_ptr() const
  43. {
  44. return (const unsigned char*)m_string.characters_without_null_termination();
  45. }
  46. const unsigned char* Utf8View::end_ptr() const
  47. {
  48. return begin_ptr() + m_string.length();
  49. }
  50. Utf8CodepointIterator Utf8View::begin() const
  51. {
  52. return { begin_ptr(), m_string.length() };
  53. }
  54. Utf8CodepointIterator Utf8View::end() const
  55. {
  56. return { end_ptr(), 0 };
  57. }
  58. size_t Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const
  59. {
  60. VERIFY(it.m_ptr >= begin_ptr());
  61. VERIFY(it.m_ptr <= end_ptr());
  62. return it.m_ptr - begin_ptr();
  63. }
  64. Utf8View Utf8View::substring_view(int byte_offset, int byte_length) const
  65. {
  66. StringView string = m_string.substring_view(byte_offset, byte_length);
  67. return Utf8View { string };
  68. }
  69. static inline bool decode_first_byte(
  70. unsigned char byte,
  71. size_t& out_code_point_length_in_bytes,
  72. u32& out_value)
  73. {
  74. if ((byte & 128) == 0) {
  75. out_value = byte;
  76. out_code_point_length_in_bytes = 1;
  77. return true;
  78. }
  79. if ((byte & 64) == 0) {
  80. return false;
  81. }
  82. if ((byte & 32) == 0) {
  83. out_value = byte & 31;
  84. out_code_point_length_in_bytes = 2;
  85. return true;
  86. }
  87. if ((byte & 16) == 0) {
  88. out_value = byte & 15;
  89. out_code_point_length_in_bytes = 3;
  90. return true;
  91. }
  92. if ((byte & 8) == 0) {
  93. out_value = byte & 7;
  94. out_code_point_length_in_bytes = 4;
  95. return true;
  96. }
  97. return false;
  98. }
  99. bool Utf8View::validate(size_t& valid_bytes) const
  100. {
  101. valid_bytes = 0;
  102. for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
  103. size_t code_point_length_in_bytes;
  104. u32 value;
  105. bool first_byte_makes_sense = decode_first_byte(*ptr, code_point_length_in_bytes, value);
  106. if (!first_byte_makes_sense)
  107. return false;
  108. for (size_t i = 1; i < code_point_length_in_bytes; i++) {
  109. ptr++;
  110. if (ptr >= end_ptr())
  111. return false;
  112. if (*ptr >> 6 != 2)
  113. return false;
  114. }
  115. valid_bytes += code_point_length_in_bytes;
  116. }
  117. return true;
  118. }
  119. size_t Utf8View::calculate_length() const
  120. {
  121. size_t length = 0;
  122. for ([[maybe_unused]] auto code_point : *this) {
  123. ++length;
  124. }
  125. return length;
  126. }
  127. Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, size_t length)
  128. : m_ptr(ptr)
  129. , m_length(length)
  130. {
  131. }
  132. bool Utf8CodepointIterator::operator==(const Utf8CodepointIterator& other) const
  133. {
  134. return m_ptr == other.m_ptr && m_length == other.m_length;
  135. }
  136. bool Utf8CodepointIterator::operator!=(const Utf8CodepointIterator& other) const
  137. {
  138. return !(*this == other);
  139. }
  140. Utf8CodepointIterator& Utf8CodepointIterator::operator++()
  141. {
  142. VERIFY(m_length > 0);
  143. size_t code_point_length_in_bytes = 0;
  144. u32 value;
  145. bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
  146. VERIFY(first_byte_makes_sense);
  147. VERIFY(code_point_length_in_bytes <= m_length);
  148. m_ptr += code_point_length_in_bytes;
  149. m_length -= code_point_length_in_bytes;
  150. return *this;
  151. }
  152. size_t Utf8CodepointIterator::code_point_length_in_bytes() const
  153. {
  154. VERIFY(m_length > 0);
  155. size_t code_point_length_in_bytes = 0;
  156. u32 value;
  157. bool first_byte_makes_sense = decode_first_byte(*m_ptr, code_point_length_in_bytes, value);
  158. VERIFY(first_byte_makes_sense);
  159. return code_point_length_in_bytes;
  160. }
  161. u32 Utf8CodepointIterator::operator*() const
  162. {
  163. VERIFY(m_length > 0);
  164. u32 code_point_value_so_far = 0;
  165. size_t code_point_length_in_bytes = 0;
  166. bool first_byte_makes_sense = decode_first_byte(m_ptr[0], code_point_length_in_bytes, code_point_value_so_far);
  167. if (!first_byte_makes_sense)
  168. dbgln("First byte doesn't make sense, bytes: {}", StringView { (const char*)m_ptr, m_length });
  169. VERIFY(first_byte_makes_sense);
  170. if (code_point_length_in_bytes > m_length)
  171. dbgln("Not enough bytes (need {}, have {}), first byte is: {:#02x}, '{}'", code_point_length_in_bytes, m_length, m_ptr[0], (const char*)m_ptr);
  172. VERIFY(code_point_length_in_bytes <= m_length);
  173. for (size_t offset = 1; offset < code_point_length_in_bytes; offset++) {
  174. VERIFY(m_ptr[offset] >> 6 == 2);
  175. code_point_value_so_far <<= 6;
  176. code_point_value_so_far |= m_ptr[offset] & 63;
  177. }
  178. return code_point_value_so_far;
  179. }
  180. }