StringView.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/AnyOf.h>
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/ByteString.h>
  9. #include <AK/DeprecatedFlyString.h>
  10. #include <AK/Find.h>
  11. #include <AK/FlyString.h>
  12. #include <AK/Function.h>
  13. #include <AK/String.h>
  14. #include <AK/StringBuilder.h>
  15. #include <AK/StringView.h>
  16. #include <AK/Vector.h>
  17. namespace AK {
  18. StringView::StringView(String const& string)
  19. : m_characters(reinterpret_cast<char const*>(string.bytes().data()))
  20. , m_length(string.bytes().size())
  21. {
  22. }
  23. StringView::StringView(FlyString const& string)
  24. : m_characters(reinterpret_cast<char const*>(string.bytes().data()))
  25. , m_length(string.bytes().size())
  26. {
  27. }
  28. StringView::StringView(ByteString const& string)
  29. : m_characters(string.characters())
  30. , m_length(string.length())
  31. {
  32. }
  33. StringView::StringView(DeprecatedFlyString const& string)
  34. : m_characters(string.characters())
  35. , m_length(string.length())
  36. {
  37. }
  38. StringView::StringView(ByteBuffer const& buffer)
  39. : m_characters((char const*)buffer.data())
  40. , m_length(buffer.size())
  41. {
  42. }
  43. Vector<StringView> StringView::split_view(char const separator, SplitBehavior split_behavior) const
  44. {
  45. StringView seperator_view { &separator, 1 };
  46. return split_view(seperator_view, split_behavior);
  47. }
  48. Vector<StringView> StringView::split_view(StringView separator, SplitBehavior split_behavior) const
  49. {
  50. Vector<StringView> parts;
  51. for_each_split_view(separator, split_behavior, [&](StringView view) {
  52. parts.append(view);
  53. });
  54. return parts;
  55. }
  56. template<typename Callback>
  57. static void for_each_line(StringView string, Callback&& callback)
  58. {
  59. char const* characters = string.characters_without_null_termination();
  60. size_t substart = 0;
  61. bool last_ch_was_cr = false;
  62. for (size_t i = 0; i < string.length(); ++i) {
  63. char ch = characters[i];
  64. bool split_view = false;
  65. switch (ch) {
  66. case '\n':
  67. if (last_ch_was_cr)
  68. substart = i + 1;
  69. else
  70. split_view = true;
  71. last_ch_was_cr = false;
  72. break;
  73. case '\r':
  74. split_view = true;
  75. last_ch_was_cr = true;
  76. break;
  77. default:
  78. last_ch_was_cr = false;
  79. break;
  80. }
  81. if (split_view) {
  82. callback(string.substring_view(substart, i - substart));
  83. substart = i + 1;
  84. }
  85. }
  86. if (size_t taillen = string.length() - substart; taillen != 0)
  87. callback(string.substring_view(substart, taillen));
  88. }
  89. Vector<StringView> StringView::lines(ConsiderCarriageReturn consider_carriage_return) const
  90. {
  91. if (is_empty())
  92. return {};
  93. if (consider_carriage_return == ConsiderCarriageReturn::No)
  94. return split_view('\n', SplitBehavior::KeepEmpty);
  95. Vector<StringView> lines;
  96. for_each_line(*this, [&](auto line) { lines.append(line); });
  97. return lines;
  98. }
  99. size_t StringView::count_lines(ConsiderCarriageReturn consider_carriage_return) const
  100. {
  101. if (is_empty())
  102. return 1;
  103. if (consider_carriage_return == ConsiderCarriageReturn::No)
  104. return count('\n') + 1;
  105. size_t lines = 0;
  106. for_each_line(*this, [&](auto) { ++lines; });
  107. return lines;
  108. }
  109. bool StringView::starts_with(char ch) const
  110. {
  111. if (is_empty())
  112. return false;
  113. return ch == characters_without_null_termination()[0];
  114. }
  115. bool StringView::starts_with(StringView str, CaseSensitivity case_sensitivity) const
  116. {
  117. return StringUtils::starts_with(*this, str, case_sensitivity);
  118. }
  119. bool StringView::ends_with(char ch) const
  120. {
  121. if (is_empty())
  122. return false;
  123. return ch == characters_without_null_termination()[length() - 1];
  124. }
  125. bool StringView::ends_with(StringView str, CaseSensitivity case_sensitivity) const
  126. {
  127. return StringUtils::ends_with(*this, str, case_sensitivity);
  128. }
  129. bool StringView::matches(StringView mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
  130. {
  131. return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
  132. }
  133. bool StringView::matches(StringView mask, CaseSensitivity case_sensitivity) const
  134. {
  135. return StringUtils::matches(*this, mask, case_sensitivity);
  136. }
  137. bool StringView::contains(char needle) const
  138. {
  139. for (char current : *this) {
  140. if (current == needle)
  141. return true;
  142. }
  143. return false;
  144. }
  145. bool StringView::contains(u32 needle) const
  146. {
  147. // A code point should be at most four UTF-8 bytes, which easily fits into StringBuilder's inline-buffer.
  148. // Therefore, this will not allocate.
  149. StringBuilder needle_builder;
  150. auto result = needle_builder.try_append_code_point(needle);
  151. if (result.is_error()) {
  152. // The needle is invalid, therefore the string does not contain it.
  153. return false;
  154. }
  155. return contains(needle_builder.string_view());
  156. }
  157. bool StringView::contains(StringView needle, CaseSensitivity case_sensitivity) const
  158. {
  159. if (needle.length() == 1)
  160. return contains(needle.characters_without_null_termination()[0]);
  161. return StringUtils::contains(*this, needle, case_sensitivity);
  162. }
  163. bool StringView::equals_ignoring_ascii_case(StringView other) const
  164. {
  165. return StringUtils::equals_ignoring_ascii_case(*this, other);
  166. }
  167. ByteString StringView::to_lowercase_string() const
  168. {
  169. return StringImpl::create_lowercased(characters_without_null_termination(), length()).release_nonnull();
  170. }
  171. ByteString StringView::to_uppercase_string() const
  172. {
  173. return StringImpl::create_uppercased(characters_without_null_termination(), length()).release_nonnull();
  174. }
  175. ByteString StringView::to_titlecase_string() const
  176. {
  177. return StringUtils::to_titlecase(*this);
  178. }
  179. StringView StringView::substring_view_starting_from_substring(StringView substring) const
  180. {
  181. char const* remaining_characters = substring.characters_without_null_termination();
  182. VERIFY(remaining_characters >= m_characters);
  183. VERIFY(remaining_characters <= m_characters + m_length);
  184. size_t remaining_length = m_length - (remaining_characters - m_characters);
  185. return { remaining_characters, remaining_length };
  186. }
  187. StringView StringView::substring_view_starting_after_substring(StringView substring) const
  188. {
  189. char const* remaining_characters = substring.characters_without_null_termination() + substring.length();
  190. VERIFY(remaining_characters >= m_characters);
  191. VERIFY(remaining_characters <= m_characters + m_length);
  192. size_t remaining_length = m_length - (remaining_characters - m_characters);
  193. return { remaining_characters, remaining_length };
  194. }
  195. bool StringView::copy_characters_to_buffer(char* buffer, size_t buffer_size) const
  196. {
  197. // We must fit at least the NUL-terminator.
  198. VERIFY(buffer_size > 0);
  199. size_t characters_to_copy = min(m_length, buffer_size - 1);
  200. __builtin_memcpy(buffer, m_characters, characters_to_copy);
  201. buffer[characters_to_copy] = 0;
  202. return characters_to_copy == m_length;
  203. }
  204. bool StringView::operator==(ByteString const& string) const
  205. {
  206. return *this == string.view();
  207. }
  208. ByteString StringView::to_byte_string() const { return ByteString { *this }; }
  209. ByteString StringView::replace(StringView needle, StringView replacement, ReplaceMode replace_mode) const
  210. {
  211. return StringUtils::replace(*this, needle, replacement, replace_mode);
  212. }
  213. Vector<size_t> StringView::find_all(StringView needle) const
  214. {
  215. return StringUtils::find_all(*this, needle);
  216. }
  217. Vector<StringView> StringView::split_view_if(Function<bool(char)> const& predicate, SplitBehavior split_behavior) const
  218. {
  219. if (is_empty())
  220. return {};
  221. Vector<StringView> v;
  222. size_t substart = 0;
  223. bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
  224. bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
  225. for (size_t i = 0; i < length(); ++i) {
  226. char ch = characters_without_null_termination()[i];
  227. if (predicate(ch)) {
  228. size_t sublen = i - substart;
  229. if (sublen != 0 || keep_empty)
  230. v.append(substring_view(substart, keep_separator ? sublen + 1 : sublen));
  231. substart = i + 1;
  232. }
  233. }
  234. size_t taillen = length() - substart;
  235. if (taillen != 0 || keep_empty)
  236. v.append(substring_view(substart, taillen));
  237. return v;
  238. }
  239. }