StringView.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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::split_view(const StringView& separator, bool keep_empty) const
  71. {
  72. ASSERT(!separator.is_empty());
  73. if (is_empty())
  74. return {};
  75. StringView view { *this };
  76. Vector<StringView> parts;
  77. auto maybe_separator_index = find_first_of(separator);
  78. while (maybe_separator_index.has_value()) {
  79. auto separator_index = maybe_separator_index.value();
  80. auto part_with_separator = view.substring_view(0, separator_index + separator.length());
  81. if (keep_empty || separator_index > 0)
  82. parts.append(part_with_separator.substring_view(0, separator_index));
  83. view = view.substring_view_starting_after_substring(part_with_separator);
  84. maybe_separator_index = view.find_first_of(separator);
  85. }
  86. if (keep_empty || !view.is_empty())
  87. parts.append(view);
  88. return parts;
  89. }
  90. Vector<StringView> StringView::lines(bool consider_cr) const
  91. {
  92. if (is_empty())
  93. return {};
  94. if (!consider_cr)
  95. return split_view('\n', true);
  96. Vector<StringView> v;
  97. size_t substart = 0;
  98. bool last_ch_was_cr = false;
  99. bool split_view = false;
  100. for (size_t i = 0; i < length(); ++i) {
  101. char ch = characters_without_null_termination()[i];
  102. if (ch == '\n') {
  103. split_view = true;
  104. if (last_ch_was_cr) {
  105. substart = i + 1;
  106. split_view = false;
  107. last_ch_was_cr = false;
  108. }
  109. }
  110. if (ch == '\r') {
  111. split_view = true;
  112. last_ch_was_cr = true;
  113. }
  114. if (split_view) {
  115. size_t sublen = i - substart;
  116. v.append(substring_view(substart, sublen));
  117. substart = i + 1;
  118. }
  119. split_view = false;
  120. }
  121. size_t taillen = length() - substart;
  122. if (taillen != 0)
  123. v.append(substring_view(substart, taillen));
  124. return v;
  125. }
  126. bool StringView::starts_with(char ch) const
  127. {
  128. if (is_empty())
  129. return false;
  130. return ch == characters_without_null_termination()[0];
  131. }
  132. bool StringView::starts_with(const StringView& str, CaseSensitivity case_sensitivity) const
  133. {
  134. return StringUtils::starts_with(*this, str, case_sensitivity);
  135. }
  136. bool StringView::ends_with(char ch) const
  137. {
  138. if (is_empty())
  139. return false;
  140. return ch == characters_without_null_termination()[length() - 1];
  141. }
  142. bool StringView::ends_with(const StringView& str, CaseSensitivity case_sensitivity) const
  143. {
  144. return StringUtils::ends_with(*this, str, case_sensitivity);
  145. }
  146. bool StringView::matches(const StringView& mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
  147. {
  148. return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
  149. }
  150. bool StringView::matches(const StringView& mask, CaseSensitivity case_sensitivity) const
  151. {
  152. return StringUtils::matches(*this, mask, case_sensitivity);
  153. }
  154. bool StringView::contains(char needle) const
  155. {
  156. for (char current : *this) {
  157. if (current == needle)
  158. return true;
  159. }
  160. return false;
  161. }
  162. bool StringView::contains(const StringView& needle, CaseSensitivity case_sensitivity) const
  163. {
  164. return StringUtils::contains(*this, needle, case_sensitivity);
  165. }
  166. bool StringView::equals_ignoring_case(const StringView& other) const
  167. {
  168. return StringUtils::equals_ignoring_case(*this, other);
  169. }
  170. StringView StringView::substring_view(size_t start, size_t length) const
  171. {
  172. ASSERT(start + length <= m_length);
  173. return { m_characters + start, length };
  174. }
  175. StringView StringView::substring_view(size_t start) const
  176. {
  177. ASSERT(start <= m_length);
  178. return { m_characters + start, length() - start };
  179. }
  180. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  181. {
  182. const char* remaining_characters = substring.characters_without_null_termination();
  183. ASSERT(remaining_characters >= m_characters);
  184. ASSERT(remaining_characters <= m_characters + m_length);
  185. size_t remaining_length = m_length - (remaining_characters - m_characters);
  186. return { remaining_characters, remaining_length };
  187. }
  188. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  189. {
  190. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  191. ASSERT(remaining_characters >= m_characters);
  192. ASSERT(remaining_characters <= m_characters + m_length);
  193. size_t remaining_length = m_length - (remaining_characters - m_characters);
  194. return { remaining_characters, remaining_length };
  195. }
  196. Optional<int> StringView::to_int() const
  197. {
  198. return StringUtils::convert_to_int(*this);
  199. }
  200. Optional<unsigned> StringView::to_uint() const
  201. {
  202. return StringUtils::convert_to_uint(*this);
  203. }
  204. unsigned StringView::hash() const
  205. {
  206. if (is_empty())
  207. return 0;
  208. if (m_impl)
  209. return m_impl->hash();
  210. return string_hash(characters_without_null_termination(), length());
  211. }
  212. bool StringView::operator==(const String& string) const
  213. {
  214. if (string.is_null())
  215. return !m_characters;
  216. if (!m_characters)
  217. return false;
  218. if (m_length != string.length())
  219. return false;
  220. if (m_characters == string.characters())
  221. return true;
  222. return !__builtin_memcmp(m_characters, string.characters(), m_length);
  223. }
  224. Optional<size_t> StringView::find_first_of(char c) const
  225. {
  226. for (size_t pos = 0; pos < m_length; ++pos) {
  227. if (m_characters[pos] == c)
  228. return pos;
  229. }
  230. return {};
  231. }
  232. Optional<size_t> StringView::find_first_of(const StringView& view) const
  233. {
  234. for (size_t pos = 0; pos < m_length; ++pos) {
  235. char c = m_characters[pos];
  236. for (char view_char : view) {
  237. if (c == view_char)
  238. return pos;
  239. }
  240. }
  241. return {};
  242. }
  243. Optional<size_t> StringView::find_last_of(char c) const
  244. {
  245. for (size_t pos = m_length; --pos > 0;) {
  246. if (m_characters[pos] == c)
  247. return pos;
  248. }
  249. return {};
  250. }
  251. Optional<size_t> StringView::find_last_of(const StringView& view) const
  252. {
  253. for (size_t pos = m_length - 1; --pos > 0;) {
  254. char c = m_characters[pos];
  255. for (char view_char : view) {
  256. if (c == view_char)
  257. return pos;
  258. }
  259. }
  260. return {};
  261. }
  262. String StringView::to_string() const { return String { *this }; }
  263. }