StringView.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/FlyString.h>
  28. #include <AK/Memory.h>
  29. #include <AK/String.h>
  30. #include <AK/StringView.h>
  31. #include <AK/Vector.h>
  32. namespace AK {
  33. StringView::StringView(const String& string)
  34. : m_impl(string.impl())
  35. , m_characters(string.characters())
  36. , m_length(string.length())
  37. {
  38. }
  39. StringView::StringView(const FlyString& string)
  40. : m_impl(string.impl())
  41. , m_characters(string.characters())
  42. , m_length(string.length())
  43. {
  44. }
  45. StringView::StringView(const ByteBuffer& buffer)
  46. : m_characters((const char*)buffer.data())
  47. , m_length(buffer.size())
  48. {
  49. }
  50. Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
  51. {
  52. if (is_empty())
  53. return {};
  54. Vector<StringView> v;
  55. size_t substart = 0;
  56. for (size_t i = 0; i < length(); ++i) {
  57. char ch = characters_without_null_termination()[i];
  58. if (ch == separator) {
  59. size_t sublen = i - substart;
  60. if (sublen != 0 || keep_empty)
  61. v.append(substring_view(substart, sublen));
  62. substart = i + 1;
  63. }
  64. }
  65. size_t taillen = length() - substart;
  66. if (taillen != 0 || keep_empty)
  67. v.append(substring_view(substart, taillen));
  68. return v;
  69. }
  70. Vector<StringView> StringView::lines(bool consider_cr) const
  71. {
  72. if (is_empty())
  73. return {};
  74. if (!consider_cr)
  75. return split_view('\n', true);
  76. Vector<StringView> v;
  77. size_t substart = 0;
  78. bool last_ch_was_cr = false;
  79. bool split_view = false;
  80. for (size_t i = 0; i < length(); ++i) {
  81. char ch = characters_without_null_termination()[i];
  82. if (ch == '\n') {
  83. split_view = true;
  84. if (last_ch_was_cr) {
  85. substart = i + 1;
  86. split_view = false;
  87. last_ch_was_cr = false;
  88. }
  89. }
  90. if (ch == '\r') {
  91. split_view = true;
  92. last_ch_was_cr = true;
  93. }
  94. if (split_view) {
  95. size_t sublen = i - substart;
  96. v.append(substring_view(substart, sublen));
  97. substart = i + 1;
  98. }
  99. split_view = false;
  100. }
  101. size_t taillen = length() - substart;
  102. if (taillen != 0)
  103. v.append(substring_view(substart, taillen));
  104. return v;
  105. }
  106. bool StringView::starts_with(char ch) const
  107. {
  108. if (is_empty())
  109. return false;
  110. return ch == characters_without_null_termination()[0];
  111. }
  112. bool StringView::starts_with(const StringView& str) const
  113. {
  114. if (str.is_empty())
  115. return true;
  116. if (is_empty())
  117. return false;
  118. if (str.length() > length())
  119. return false;
  120. if (characters_without_null_termination() == str.characters_without_null_termination())
  121. return true;
  122. return !memcmp(characters_without_null_termination(), str.characters_without_null_termination(), str.length());
  123. }
  124. bool StringView::ends_with(char ch) const
  125. {
  126. if (is_empty())
  127. return false;
  128. return ch == characters_without_null_termination()[length() - 1];
  129. }
  130. bool StringView::ends_with(const StringView& str) const
  131. {
  132. if (str.is_empty())
  133. return true;
  134. if (is_empty())
  135. return false;
  136. if (str.length() > length())
  137. return false;
  138. return !memcmp(characters_without_null_termination() + length() - str.length(), str.characters_without_null_termination(), str.length());
  139. }
  140. bool StringView::matches(const StringView& mask, CaseSensitivity case_sensitivity) const
  141. {
  142. return StringUtils::matches(*this, mask, case_sensitivity);
  143. }
  144. bool StringView::contains(char needle) const
  145. {
  146. for (char current : *this) {
  147. if (current == needle)
  148. return true;
  149. }
  150. return false;
  151. }
  152. StringView StringView::substring_view(size_t start, size_t length) const
  153. {
  154. ASSERT(start + length <= m_length);
  155. return { m_characters + start, length };
  156. }
  157. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  158. {
  159. const char* remaining_characters = substring.characters_without_null_termination();
  160. ASSERT(remaining_characters >= m_characters);
  161. ASSERT(remaining_characters <= m_characters + m_length);
  162. size_t remaining_length = m_length - (remaining_characters - m_characters);
  163. return { remaining_characters, remaining_length };
  164. }
  165. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  166. {
  167. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  168. ASSERT(remaining_characters >= m_characters);
  169. ASSERT(remaining_characters <= m_characters + m_length);
  170. size_t remaining_length = m_length - (remaining_characters - m_characters);
  171. return { remaining_characters, remaining_length };
  172. }
  173. int StringView::to_int(bool& ok) const
  174. {
  175. return StringUtils::convert_to_int(*this, ok);
  176. }
  177. unsigned StringView::to_uint(bool& ok) const
  178. {
  179. return StringUtils::convert_to_uint(*this, ok);
  180. }
  181. unsigned StringView::hash() const
  182. {
  183. if (is_empty())
  184. return 0;
  185. if (m_impl)
  186. return m_impl->hash();
  187. return string_hash(characters_without_null_termination(), length());
  188. }
  189. bool StringView::operator==(const String& string) const
  190. {
  191. if (string.is_null())
  192. return !m_characters;
  193. if (!m_characters)
  194. return false;
  195. if (m_length != string.length())
  196. return false;
  197. if (m_characters == string.characters())
  198. return true;
  199. return !__builtin_memcmp(m_characters, string.characters(), m_length);
  200. }
  201. Optional<size_t> StringView::find_first_of(char c) const
  202. {
  203. for (size_t pos = 0; pos < m_length; ++pos) {
  204. if (m_characters[pos] == c)
  205. return pos;
  206. }
  207. return {};
  208. }
  209. Optional<size_t> StringView::find_first_of(const StringView& view) const
  210. {
  211. for (size_t pos = 0; pos < m_length; ++pos) {
  212. char c = m_characters[pos];
  213. for (char view_char : view) {
  214. if (c == view_char)
  215. return pos;
  216. }
  217. }
  218. return {};
  219. }
  220. Optional<size_t> StringView::find_last_of(char c) const
  221. {
  222. for (size_t pos = m_length; --pos >0;) {
  223. if (m_characters[pos] == c)
  224. return pos;
  225. }
  226. return {};
  227. }
  228. Optional<size_t> StringView::find_last_of(const StringView& view) const
  229. {
  230. for (size_t pos = m_length - 1; --pos > 0;) {
  231. char c = m_characters[pos];
  232. for (char view_char : view) {
  233. if (c == view_char)
  234. return pos;
  235. }
  236. }
  237. return {};
  238. }
  239. String StringView::to_string() const { return String { *this }; }
  240. }