StringView.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/AnyOf.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/Find.h>
  29. #include <AK/FlyString.h>
  30. #include <AK/Memory.h>
  31. #include <AK/String.h>
  32. #include <AK/StringView.h>
  33. #include <AK/Vector.h>
  34. namespace AK {
  35. StringView::StringView(const String& string)
  36. : m_impl(string.impl())
  37. , m_characters(string.characters())
  38. , m_length(string.length())
  39. {
  40. }
  41. StringView::StringView(const FlyString& string)
  42. : m_impl(string.impl())
  43. , m_characters(string.characters())
  44. , m_length(string.length())
  45. {
  46. }
  47. StringView::StringView(const ByteBuffer& buffer)
  48. : m_characters((const char*)buffer.data())
  49. , m_length(buffer.size())
  50. {
  51. }
  52. Vector<StringView> StringView::split_view(const char separator, bool keep_empty) const
  53. {
  54. if (is_empty())
  55. return {};
  56. Vector<StringView> v;
  57. size_t substart = 0;
  58. for (size_t i = 0; i < length(); ++i) {
  59. char ch = characters_without_null_termination()[i];
  60. if (ch == separator) {
  61. size_t sublen = i - substart;
  62. if (sublen != 0 || keep_empty)
  63. v.append(substring_view(substart, sublen));
  64. substart = i + 1;
  65. }
  66. }
  67. size_t taillen = length() - substart;
  68. if (taillen != 0 || keep_empty)
  69. v.append(substring_view(substart, taillen));
  70. return v;
  71. }
  72. Vector<StringView> StringView::split_view(const StringView& separator, bool keep_empty) const
  73. {
  74. VERIFY(!separator.is_empty());
  75. if (is_empty())
  76. return {};
  77. StringView view { *this };
  78. Vector<StringView> parts;
  79. auto maybe_separator_index = find(separator);
  80. while (maybe_separator_index.has_value()) {
  81. auto separator_index = maybe_separator_index.value();
  82. auto part_with_separator = view.substring_view(0, separator_index + separator.length());
  83. if (keep_empty || separator_index > 0)
  84. parts.append(part_with_separator.substring_view(0, separator_index));
  85. view = view.substring_view_starting_after_substring(part_with_separator);
  86. maybe_separator_index = view.find(separator);
  87. }
  88. if (keep_empty || !view.is_empty())
  89. parts.append(view);
  90. return parts;
  91. }
  92. Vector<StringView> StringView::lines(bool consider_cr) const
  93. {
  94. if (is_empty())
  95. return {};
  96. if (!consider_cr)
  97. return split_view('\n', true);
  98. Vector<StringView> v;
  99. size_t substart = 0;
  100. bool last_ch_was_cr = false;
  101. bool split_view = false;
  102. for (size_t i = 0; i < length(); ++i) {
  103. char ch = characters_without_null_termination()[i];
  104. if (ch == '\n') {
  105. split_view = true;
  106. if (last_ch_was_cr) {
  107. substart = i + 1;
  108. split_view = false;
  109. last_ch_was_cr = false;
  110. }
  111. }
  112. if (ch == '\r') {
  113. split_view = true;
  114. last_ch_was_cr = true;
  115. }
  116. if (split_view) {
  117. size_t sublen = i - substart;
  118. v.append(substring_view(substart, sublen));
  119. substart = i + 1;
  120. }
  121. split_view = false;
  122. }
  123. size_t taillen = length() - substart;
  124. if (taillen != 0)
  125. v.append(substring_view(substart, taillen));
  126. return v;
  127. }
  128. bool StringView::starts_with(char ch) const
  129. {
  130. if (is_empty())
  131. return false;
  132. return ch == characters_without_null_termination()[0];
  133. }
  134. bool StringView::starts_with(const StringView& str, CaseSensitivity case_sensitivity) const
  135. {
  136. return StringUtils::starts_with(*this, str, case_sensitivity);
  137. }
  138. bool StringView::ends_with(char ch) const
  139. {
  140. if (is_empty())
  141. return false;
  142. return ch == characters_without_null_termination()[length() - 1];
  143. }
  144. bool StringView::ends_with(const StringView& str, CaseSensitivity case_sensitivity) const
  145. {
  146. return StringUtils::ends_with(*this, str, case_sensitivity);
  147. }
  148. bool StringView::matches(const StringView& mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
  149. {
  150. return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
  151. }
  152. bool StringView::matches(const StringView& mask, CaseSensitivity case_sensitivity) const
  153. {
  154. return StringUtils::matches(*this, mask, case_sensitivity);
  155. }
  156. bool StringView::contains(char needle) const
  157. {
  158. for (char current : *this) {
  159. if (current == needle)
  160. return true;
  161. }
  162. return false;
  163. }
  164. bool StringView::contains(const StringView& needle, CaseSensitivity case_sensitivity) const
  165. {
  166. return StringUtils::contains(*this, needle, case_sensitivity);
  167. }
  168. bool StringView::equals_ignoring_case(const StringView& other) const
  169. {
  170. return StringUtils::equals_ignoring_case(*this, other);
  171. }
  172. StringView StringView::substring_view(size_t start, size_t length) const
  173. {
  174. VERIFY(start + length <= m_length);
  175. return { m_characters + start, length };
  176. }
  177. StringView StringView::substring_view(size_t start) const
  178. {
  179. VERIFY(start <= m_length);
  180. return { m_characters + start, length() - start };
  181. }
  182. StringView StringView::substring_view_starting_from_substring(const StringView& substring) const
  183. {
  184. const char* remaining_characters = substring.characters_without_null_termination();
  185. VERIFY(remaining_characters >= m_characters);
  186. VERIFY(remaining_characters <= m_characters + m_length);
  187. size_t remaining_length = m_length - (remaining_characters - m_characters);
  188. return { remaining_characters, remaining_length };
  189. }
  190. StringView StringView::substring_view_starting_after_substring(const StringView& substring) const
  191. {
  192. const char* remaining_characters = substring.characters_without_null_termination() + substring.length();
  193. VERIFY(remaining_characters >= m_characters);
  194. VERIFY(remaining_characters <= m_characters + m_length);
  195. size_t remaining_length = m_length - (remaining_characters - m_characters);
  196. return { remaining_characters, remaining_length };
  197. }
  198. template<typename T>
  199. Optional<T> StringView::to_int() const
  200. {
  201. return StringUtils::convert_to_int<T>(*this);
  202. }
  203. template Optional<i8> StringView::to_int() const;
  204. template Optional<i16> StringView::to_int() const;
  205. template Optional<i32> StringView::to_int() const;
  206. template Optional<i64> StringView::to_int() const;
  207. template<typename T>
  208. Optional<T> StringView::to_uint() const
  209. {
  210. return StringUtils::convert_to_uint<T>(*this);
  211. }
  212. template Optional<u8> StringView::to_uint() const;
  213. template Optional<u16> StringView::to_uint() const;
  214. template Optional<u32> StringView::to_uint() const;
  215. template Optional<u64> StringView::to_uint() const;
  216. template Optional<long> StringView::to_uint() const;
  217. template Optional<long long> StringView::to_uint() const;
  218. unsigned StringView::hash() const
  219. {
  220. if (is_empty())
  221. return 0;
  222. if (m_impl)
  223. return m_impl->hash();
  224. return string_hash(characters_without_null_termination(), length());
  225. }
  226. bool StringView::operator==(const String& string) const
  227. {
  228. if (string.is_null())
  229. return !m_characters;
  230. if (!m_characters)
  231. return false;
  232. if (m_length != string.length())
  233. return false;
  234. if (m_characters == string.characters())
  235. return true;
  236. return !__builtin_memcmp(m_characters, string.characters(), m_length);
  237. }
  238. Optional<size_t> StringView::find_first_of(char c) const
  239. {
  240. if (const auto location = AK::find(begin(), end(), c); location != end()) {
  241. return location.index();
  242. }
  243. return {};
  244. }
  245. Optional<size_t> StringView::find_first_of(const StringView& view) const
  246. {
  247. if (const auto location = AK::find_if(begin(), end(),
  248. [&](const auto c) {
  249. return AK::any_of(view.begin(), view.end(),
  250. [&](const auto view_char) {
  251. return c == view_char;
  252. });
  253. });
  254. location != end()) {
  255. return location.index();
  256. }
  257. return {};
  258. }
  259. Optional<size_t> StringView::find_last_of(char c) const
  260. {
  261. for (size_t pos = m_length; --pos > 0;) {
  262. if (m_characters[pos] == c)
  263. return pos;
  264. }
  265. return {};
  266. }
  267. Optional<size_t> StringView::find_last_of(const StringView& view) const
  268. {
  269. for (size_t pos = m_length - 1; --pos > 0;) {
  270. char c = m_characters[pos];
  271. for (char view_char : view) {
  272. if (c == view_char)
  273. return pos;
  274. }
  275. }
  276. return {};
  277. }
  278. Optional<size_t> StringView::find(char c) const
  279. {
  280. return find(StringView { &c, 1 });
  281. }
  282. Optional<size_t> StringView::find(const StringView& view) const
  283. {
  284. return StringUtils::find(*this, view);
  285. }
  286. String StringView::to_string() const { return String { *this }; }
  287. }