StringView.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@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/ByteBuffer.h>
  27. #include <AK/String.h>
  28. #include <AK/StringView.h>
  29. #include <AK/Vector.h>
  30. namespace AK {
  31. StringView::StringView(const String& string)
  32. : m_impl(string.impl())
  33. , m_characters(string.characters())
  34. , m_length(string.length())
  35. {
  36. }
  37. StringView::StringView(const ByteBuffer& buffer)
  38. : m_characters((const char*)buffer.data())
  39. , m_length((size_t)buffer.size())
  40. {
  41. }
  42. Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
  43. {
  44. if (is_empty())
  45. return {};
  46. Vector<StringView> v;
  47. size_t substart = 0;
  48. for (size_t i = 0; i < length(); ++i) {
  49. char ch = characters_without_null_termination()[i];
  50. if (ch == separator) {
  51. size_t sublen = i - substart;
  52. if (sublen != 0 || keep_empty)
  53. v.append(substring_view(substart, sublen));
  54. substart = i + 1;
  55. }
  56. }
  57. size_t taillen = length() - substart;
  58. if (taillen != 0 || keep_empty)
  59. v.append(substring_view(substart, taillen));
  60. return v;
  61. }
  62. Vector<StringView> StringView::lines(bool consider_cr) const
  63. {
  64. if (is_empty())
  65. return {};
  66. if (!consider_cr)
  67. return split_view('\n', true);
  68. Vector<StringView> v;
  69. size_t substart = 0;
  70. bool last_ch_was_cr = false;
  71. bool split_view = false;
  72. for (size_t i = 0; i < length(); ++i) {
  73. char ch = characters_without_null_termination()[i];
  74. if (ch == '\n') {
  75. split_view = true;
  76. if (last_ch_was_cr) {
  77. substart = i + 1;
  78. split_view = false;
  79. last_ch_was_cr = false;
  80. }
  81. }
  82. if (ch == '\r') {
  83. split_view = true;
  84. last_ch_was_cr = true;
  85. }
  86. if (split_view) {
  87. size_t sublen = i - substart;
  88. v.append(substring_view(substart, sublen));
  89. substart = i + 1;
  90. }
  91. split_view = false;
  92. }
  93. size_t taillen = length() - substart;
  94. if (taillen != 0)
  95. v.append(substring_view(substart, taillen));
  96. return v;
  97. }
  98. bool StringView::starts_with(const StringView& str) const
  99. {
  100. if (str.is_empty())
  101. return true;
  102. if (is_empty())
  103. return false;
  104. if (str.length() > length())
  105. return false;
  106. if (characters_without_null_termination() == str.characters_without_null_termination())
  107. return true;
  108. return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
  109. }
  110. bool StringView::ends_with(const StringView& str) const
  111. {
  112. if (str.is_empty())
  113. return true;
  114. if (is_empty())
  115. return false;
  116. if (str.length() > length())
  117. return false;
  118. return !memcmp(characters_without_null_termination() + length() - str.length(), str.characters_without_null_termination(), str.length());
  119. }
  120. StringView StringView::substring_view(size_t start, size_t length) const
  121. {
  122. ASSERT(start + length <= m_length);
  123. return { m_characters + start, length };
  124. }
  125. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  126. {
  127. const char* remaining_characters = substring.characters_without_null_termination();
  128. ASSERT(remaining_characters >= m_characters);
  129. ASSERT(remaining_characters <= m_characters + m_length);
  130. size_t remaining_length = m_length - (remaining_characters - m_characters);
  131. return { remaining_characters, remaining_length };
  132. }
  133. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  134. {
  135. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  136. ASSERT(remaining_characters >= m_characters);
  137. ASSERT(remaining_characters <= m_characters + m_length);
  138. size_t remaining_length = m_length - (remaining_characters - m_characters);
  139. return { remaining_characters, remaining_length };
  140. }
  141. int StringView::to_int(bool& ok) const
  142. {
  143. bool negative = false;
  144. int value = 0;
  145. size_t i = 0;
  146. if (is_empty()) {
  147. ok = false;
  148. return 0;
  149. }
  150. if (characters_without_null_termination()[0] == '-') {
  151. i++;
  152. negative = true;
  153. }
  154. for (; i < length(); i++) {
  155. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  156. ok = false;
  157. return 0;
  158. }
  159. value = value * 10;
  160. value += characters_without_null_termination()[i] - '0';
  161. }
  162. ok = true;
  163. return negative ? -value : value;
  164. }
  165. unsigned StringView::to_uint(bool& ok) const
  166. {
  167. unsigned value = 0;
  168. for (size_t i = 0; i < length(); ++i) {
  169. if (characters_without_null_termination()[i] < '0' || characters_without_null_termination()[i] > '9') {
  170. ok = false;
  171. return 0;
  172. }
  173. value = value * 10;
  174. value += characters_without_null_termination()[i] - '0';
  175. }
  176. ok = true;
  177. return value;
  178. }
  179. unsigned StringView::hash() const
  180. {
  181. if (is_empty())
  182. return 0;
  183. if (m_impl)
  184. return m_impl->hash();
  185. return string_hash(characters_without_null_termination(), length());
  186. }
  187. }