StringView.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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(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(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. template<typename T>
  197. Optional<T> StringView::to_int() const
  198. {
  199. return StringUtils::convert_to_int<T>(*this);
  200. }
  201. template Optional<i8> StringView::to_int() const;
  202. template Optional<i16> StringView::to_int() const;
  203. template Optional<i32> StringView::to_int() const;
  204. template Optional<i64> StringView::to_int() const;
  205. template<typename T>
  206. Optional<T> StringView::to_uint() const
  207. {
  208. return StringUtils::convert_to_uint<T>(*this);
  209. }
  210. template Optional<u8> StringView::to_uint() const;
  211. template Optional<u16> StringView::to_uint() const;
  212. template Optional<u32> StringView::to_uint() const;
  213. template Optional<u64> StringView::to_uint() const;
  214. template Optional<long> StringView::to_uint() const;
  215. template Optional<long long> StringView::to_uint() const;
  216. unsigned StringView::hash() const
  217. {
  218. if (is_empty())
  219. return 0;
  220. if (m_impl)
  221. return m_impl->hash();
  222. return string_hash(characters_without_null_termination(), length());
  223. }
  224. bool StringView::operator==(const String& string) const
  225. {
  226. if (string.is_null())
  227. return !m_characters;
  228. if (!m_characters)
  229. return false;
  230. if (m_length != string.length())
  231. return false;
  232. if (m_characters == string.characters())
  233. return true;
  234. return !__builtin_memcmp(m_characters, string.characters(), m_length);
  235. }
  236. Optional<size_t> StringView::find_first_of(char c) const
  237. {
  238. for (size_t pos = 0; pos < m_length; ++pos) {
  239. if (m_characters[pos] == c)
  240. return pos;
  241. }
  242. return {};
  243. }
  244. Optional<size_t> StringView::find_first_of(const StringView& view) const
  245. {
  246. for (size_t pos = 0; pos < m_length; ++pos) {
  247. char c = m_characters[pos];
  248. for (char view_char : view) {
  249. if (c == view_char)
  250. return pos;
  251. }
  252. }
  253. return {};
  254. }
  255. Optional<size_t> StringView::find_last_of(char c) const
  256. {
  257. for (size_t pos = m_length; --pos > 0;) {
  258. if (m_characters[pos] == c)
  259. return pos;
  260. }
  261. return {};
  262. }
  263. Optional<size_t> StringView::find_last_of(const StringView& view) const
  264. {
  265. for (size_t pos = m_length - 1; --pos > 0;) {
  266. char c = m_characters[pos];
  267. for (char view_char : view) {
  268. if (c == view_char)
  269. return pos;
  270. }
  271. }
  272. return {};
  273. }
  274. Optional<size_t> StringView::find(char c) const
  275. {
  276. return find(StringView { &c, 1 });
  277. }
  278. Optional<size_t> StringView::find(const StringView& view) const
  279. {
  280. return StringUtils::find(*this, view);
  281. }
  282. String StringView::to_string() const { return String { *this }; }
  283. }