StringView.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <AK/String.h>
  2. #include <AK/StringView.h>
  3. namespace AK {
  4. StringView::StringView(const String& string)
  5. : m_impl(string.impl())
  6. , m_characters(string.characters())
  7. , m_length(string.length())
  8. {
  9. }
  10. StringView::StringView(const ByteBuffer& buffer)
  11. : m_characters((const char*)buffer.data())
  12. , m_length((size_t)buffer.size())
  13. {
  14. }
  15. Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
  16. {
  17. if (is_empty())
  18. return {};
  19. Vector<StringView> v;
  20. size_t substart = 0;
  21. for (size_t i = 0; i < length(); ++i) {
  22. char ch = characters_without_null_termination()[i];
  23. if (ch == separator) {
  24. size_t sublen = i - substart;
  25. if (sublen != 0 || keep_empty)
  26. v.append(substring_view(substart, sublen));
  27. substart = i + 1;
  28. }
  29. }
  30. size_t taillen = length() - substart;
  31. if (taillen != 0 || keep_empty)
  32. v.append(substring_view(substart, taillen));
  33. if (characters_without_null_termination()[length() - 1] == separator && keep_empty)
  34. v.append(String::empty());
  35. return v;
  36. }
  37. Vector<StringView> StringView::lines(bool consider_cr) const
  38. {
  39. if (is_empty())
  40. return {};
  41. if (!consider_cr)
  42. return split_view('\n', true);
  43. Vector<StringView> v;
  44. size_t substart = 0;
  45. bool last_ch_was_cr = false;
  46. bool split_view = false;
  47. for (size_t i = 0; i < length(); ++i) {
  48. char ch = characters_without_null_termination()[i];
  49. if (ch == '\n') {
  50. split_view = true;
  51. if (last_ch_was_cr) {
  52. substart = i + 1;
  53. split_view = false;
  54. last_ch_was_cr = false;
  55. }
  56. }
  57. if (ch == '\r') {
  58. split_view = true;
  59. last_ch_was_cr = true;
  60. }
  61. if (split_view) {
  62. size_t sublen = i - substart;
  63. v.append(substring_view(substart, sublen));
  64. substart = i + 1;
  65. }
  66. split_view = false;
  67. }
  68. size_t taillen = length() - substart;
  69. if (taillen != 0)
  70. v.append(substring_view(substart, taillen));
  71. return v;
  72. }
  73. bool StringView::starts_with(const StringView& str) const
  74. {
  75. if (str.is_empty())
  76. return true;
  77. if (is_empty())
  78. return false;
  79. if (str.length() > length())
  80. return false;
  81. if (characters_without_null_termination() == str.characters_without_null_termination())
  82. return true;
  83. return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
  84. }
  85. bool StringView::ends_with(const StringView& str) const
  86. {
  87. if (str.is_empty())
  88. return true;
  89. if (is_empty())
  90. return false;
  91. if (str.length() > length())
  92. return false;
  93. return !memcmp(characters_without_null_termination() + length() - str.length(), str.characters_without_null_termination(), str.length());
  94. }
  95. StringView StringView::substring_view(size_t start, size_t length) const
  96. {
  97. ASSERT(start + length <= m_length);
  98. return { m_characters + start, length };
  99. }
  100. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  101. {
  102. const char* remaining_characters = substring.characters_without_null_termination();
  103. ASSERT(remaining_characters >= m_characters);
  104. ASSERT(remaining_characters <= m_characters + m_length);
  105. size_t remaining_length = m_length - (remaining_characters - m_characters);
  106. return { remaining_characters, remaining_length };
  107. }
  108. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  109. {
  110. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  111. ASSERT(remaining_characters >= m_characters);
  112. ASSERT(remaining_characters <= m_characters + m_length);
  113. size_t remaining_length = m_length - (remaining_characters - m_characters);
  114. return { remaining_characters, remaining_length };
  115. }
  116. int StringView::to_int(bool& ok) const
  117. {
  118. bool negative = false;
  119. int value = 0;
  120. size_t i = 0;
  121. if (is_empty()) {
  122. ok = false;
  123. return 0;
  124. }
  125. if (characters_without_null_termination()[0] == '-') {
  126. i++;
  127. negative = true;
  128. }
  129. for (; i < length(); i++) {
  130. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  131. ok = false;
  132. return 0;
  133. }
  134. value = value * 10;
  135. value += characters_without_null_termination()[i] - '0';
  136. }
  137. ok = true;
  138. return negative ? -value : value;
  139. }
  140. unsigned StringView::to_uint(bool& ok) const
  141. {
  142. unsigned value = 0;
  143. for (size_t i = 0; i < length(); ++i) {
  144. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  145. ok = false;
  146. return 0;
  147. }
  148. value = value * 10;
  149. value += characters_without_null_termination()[i] - '0';
  150. }
  151. ok = true;
  152. return value;
  153. }
  154. unsigned StringView::hash() const
  155. {
  156. if (is_empty())
  157. return 0;
  158. if (m_impl)
  159. return m_impl->hash();
  160. return string_hash(characters_without_null_termination(), length());
  161. }
  162. }