Utf8View.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <AK/Utf8View.h>
  2. #include <AK/LogStream.h>
  3. namespace AK {
  4. Utf8View::Utf8View(const String& string)
  5. : m_string(string)
  6. {
  7. }
  8. Utf8View::Utf8View(const StringView& string)
  9. : m_string(string)
  10. {
  11. }
  12. Utf8View::Utf8View(const char* string)
  13. : m_string(string)
  14. {
  15. }
  16. const unsigned char* Utf8View::begin_ptr() const
  17. {
  18. return (const unsigned char*)m_string.characters_without_null_termination();
  19. }
  20. const unsigned char* Utf8View::end_ptr() const
  21. {
  22. return begin_ptr() + m_string.length();
  23. }
  24. Utf8CodepointIterator Utf8View::begin() const
  25. {
  26. return { begin_ptr(), m_string.length() };
  27. }
  28. Utf8CodepointIterator Utf8View::end() const
  29. {
  30. return { end_ptr(), 0 };
  31. }
  32. int Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const
  33. {
  34. ASSERT(it.m_ptr >= begin_ptr());
  35. ASSERT(it.m_ptr <= end_ptr());
  36. return it.m_ptr - begin_ptr();
  37. }
  38. Utf8View Utf8View::substring_view(int byte_offset, int byte_length) const
  39. {
  40. StringView string = m_string.substring_view(byte_offset, byte_length);
  41. return Utf8View { string };
  42. }
  43. static inline bool decode_first_byte(
  44. unsigned char byte,
  45. int& out_codepoint_length_in_bytes,
  46. u32& out_value)
  47. {
  48. if ((byte & 128) == 0) {
  49. out_value = byte;
  50. out_codepoint_length_in_bytes = 1;
  51. return true;
  52. }
  53. if ((byte & 64) == 0) {
  54. return false;
  55. }
  56. if ((byte & 32) == 0) {
  57. out_value = byte & 31;
  58. out_codepoint_length_in_bytes = 2;
  59. return true;
  60. }
  61. if ((byte & 16) == 0) {
  62. out_value = byte & 15;
  63. out_codepoint_length_in_bytes = 3;
  64. return true;
  65. }
  66. if ((byte & 8) == 0) {
  67. out_value = byte & 7;
  68. out_codepoint_length_in_bytes = 4;
  69. return true;
  70. }
  71. return false;
  72. }
  73. bool Utf8View::validate() const
  74. {
  75. for (auto ptr = begin_ptr(); ptr < end_ptr(); ptr++) {
  76. int codepoint_length_in_bytes;
  77. u32 value;
  78. bool first_byte_makes_sense = decode_first_byte(*ptr, codepoint_length_in_bytes, value);
  79. if (!first_byte_makes_sense)
  80. return false;
  81. for (int i = 1; i < codepoint_length_in_bytes; i++) {
  82. ptr++;
  83. if (ptr >= end_ptr())
  84. return false;
  85. if (*ptr >> 6 != 2)
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. Utf8CodepointIterator::Utf8CodepointIterator(const unsigned char* ptr, int length)
  92. : m_ptr(ptr)
  93. , m_length(length)
  94. {
  95. }
  96. bool Utf8CodepointIterator::operator==(const Utf8CodepointIterator& other) const
  97. {
  98. return m_ptr == other.m_ptr && m_length == other.m_length;
  99. }
  100. bool Utf8CodepointIterator::operator!=(const Utf8CodepointIterator& other) const
  101. {
  102. return !(*this == other);
  103. }
  104. Utf8CodepointIterator& Utf8CodepointIterator::operator++()
  105. {
  106. do {
  107. ASSERT(m_length > 0);
  108. m_length--;
  109. m_ptr++;
  110. } while (m_ptr[0] >> 6 == 2);
  111. return *this;
  112. }
  113. u32 Utf8CodepointIterator::operator*() const
  114. {
  115. ASSERT(m_length > 0);
  116. u32 codepoint_value_so_far;
  117. int codepoint_length_in_bytes;
  118. bool first_byte_makes_sense = decode_first_byte(m_ptr[0], codepoint_length_in_bytes, codepoint_value_so_far);
  119. if (!first_byte_makes_sense) {
  120. dbg() << "First byte doesn't make sense, bytes = " << (const char*)m_ptr;
  121. }
  122. ASSERT(first_byte_makes_sense);
  123. if (codepoint_length_in_bytes > m_length) {
  124. dbg() << "Not enough bytes (need " << codepoint_length_in_bytes << ", have " << m_length << "), first byte is: " << m_ptr[0] << " " << (const char*)m_ptr;
  125. }
  126. ASSERT(codepoint_length_in_bytes <= m_length);
  127. for (int offset = 1; offset < codepoint_length_in_bytes; offset++) {
  128. ASSERT(m_ptr[offset] >> 6 == 2);
  129. codepoint_value_so_far <<= 6;
  130. codepoint_value_so_far |= m_ptr[offset] & 63;
  131. }
  132. return codepoint_value_so_far;
  133. }
  134. }