StringView.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Assertions.h>
  8. #include <AK/Checked.h>
  9. #include <AK/Forward.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Span.h>
  12. #include <AK/StdLibExtras.h>
  13. #include <AK/StringHash.h>
  14. #include <AK/StringUtils.h>
  15. namespace AK {
  16. class StringView {
  17. public:
  18. ALWAYS_INLINE constexpr StringView() = default;
  19. ALWAYS_INLINE constexpr StringView(const char* characters, size_t length)
  20. : m_characters(characters)
  21. , m_length(length)
  22. {
  23. if (!is_constant_evaluated())
  24. VERIFY(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
  25. }
  26. ALWAYS_INLINE StringView(const unsigned char* characters, size_t length)
  27. : m_characters((const char*)characters)
  28. , m_length(length)
  29. {
  30. VERIFY(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
  31. }
  32. ALWAYS_INLINE constexpr StringView(const char* cstring)
  33. : m_characters(cstring)
  34. , m_length(cstring ? __builtin_strlen(cstring) : 0)
  35. {
  36. }
  37. ALWAYS_INLINE StringView(ReadonlyBytes bytes)
  38. : m_characters(reinterpret_cast<const char*>(bytes.data()))
  39. , m_length(bytes.size())
  40. {
  41. }
  42. StringView(const ByteBuffer&);
  43. StringView(const String&);
  44. StringView(const FlyString&);
  45. explicit StringView(ByteBuffer&&) = delete;
  46. explicit StringView(String&&) = delete;
  47. explicit StringView(FlyString&&) = delete;
  48. [[nodiscard]] constexpr bool is_null() const { return m_characters == nullptr; }
  49. [[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
  50. [[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
  51. [[nodiscard]] constexpr size_t length() const { return m_length; }
  52. [[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; }
  53. constexpr const char& operator[](size_t index) const { return m_characters[index]; }
  54. using ConstIterator = SimpleIterator<const StringView, const char>;
  55. [[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
  56. [[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
  57. [[nodiscard]] constexpr unsigned hash() const
  58. {
  59. if (is_empty())
  60. return 0;
  61. return string_hash(characters_without_null_termination(), length());
  62. }
  63. [[nodiscard]] bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  64. [[nodiscard]] bool ends_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  65. [[nodiscard]] bool starts_with(char) const;
  66. [[nodiscard]] bool ends_with(char) const;
  67. [[nodiscard]] bool matches(StringView mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
  68. [[nodiscard]] bool matches(StringView mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
  69. [[nodiscard]] bool contains(char) const;
  70. [[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  71. [[nodiscard]] bool equals_ignoring_case(StringView other) const;
  72. [[nodiscard]] StringView trim(StringView characters, TrimMode mode = TrimMode::Both) const { return StringUtils::trim(*this, characters, mode); }
  73. [[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
  74. [[nodiscard]] String to_lowercase_string() const;
  75. [[nodiscard]] String to_uppercase_string() const;
  76. [[nodiscard]] String to_titlecase_string() const;
  77. [[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
  78. [[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
  79. [[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
  80. // FIXME: Implement find_last(StringView) for API symmetry.
  81. [[nodiscard]] Vector<size_t> find_all(StringView needle) const;
  82. using SearchDirection = StringUtils::SearchDirection;
  83. [[nodiscard]] Optional<size_t> find_any_of(StringView needles, SearchDirection direction = SearchDirection::Forward) const { return StringUtils::find_any_of(*this, needles, direction); }
  84. [[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
  85. {
  86. if (!is_constant_evaluated())
  87. VERIFY(start + length <= m_length);
  88. return { m_characters + start, length };
  89. }
  90. [[nodiscard]] constexpr StringView substring_view(size_t start) const
  91. {
  92. if (!is_constant_evaluated())
  93. VERIFY(start <= length());
  94. return substring_view(start, length() - start);
  95. }
  96. [[nodiscard]] Vector<StringView> split_view(char, bool keep_empty = false) const;
  97. [[nodiscard]] Vector<StringView> split_view(StringView, bool keep_empty = false) const;
  98. [[nodiscard]] Vector<StringView> split_view_if(Function<bool(char)> const& predicate, bool keep_empty = false) const;
  99. // Create a Vector of StringViews split by line endings. As of CommonMark
  100. // 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
  101. // return (U+000D) not followed by a newline, or a carriage return and a
  102. // following newline.".
  103. [[nodiscard]] Vector<StringView> lines(bool consider_cr = true) const;
  104. template<typename T = int>
  105. Optional<T> to_int() const;
  106. template<typename T = unsigned>
  107. Optional<T> to_uint() const;
  108. // Create a new substring view of this string view, starting either at the beginning of
  109. // the given substring view, or after its end, and continuing until the end of this string
  110. // view (that is, for the remaining part of its length). For example,
  111. //
  112. // StringView str { "foobar" };
  113. // StringView substr = str.substring_view(1, 2); // "oo"
  114. // StringView substr_from = str.substring_view_starting_from_substring(subst); // "oobar"
  115. // StringView substr_after = str.substring_view_starting_after_substring(subst); // "bar"
  116. //
  117. // Note that this only works if the string view passed as an argument is indeed a substring
  118. // view of this string view, such as one created by substring_view() and split_view(). It
  119. // does not work for arbitrary strings; for example declaring substr in the example above as
  120. //
  121. // StringView substr { "oo" };
  122. //
  123. // would not work.
  124. [[nodiscard]] StringView substring_view_starting_from_substring(StringView substring) const;
  125. [[nodiscard]] StringView substring_view_starting_after_substring(StringView substring) const;
  126. constexpr bool operator==(const char* cstring) const
  127. {
  128. if (is_null())
  129. return cstring == nullptr;
  130. if (!cstring)
  131. return false;
  132. // NOTE: `m_characters` is not guaranteed to be null-terminated, but `cstring` is.
  133. const char* cp = cstring;
  134. for (size_t i = 0; i < m_length; ++i) {
  135. if (*cp == '\0')
  136. return false;
  137. if (m_characters[i] != *(cp++))
  138. return false;
  139. }
  140. return *cp == '\0';
  141. }
  142. constexpr bool operator!=(const char* cstring) const
  143. {
  144. return !(*this == cstring);
  145. }
  146. bool operator==(const String&) const;
  147. constexpr bool operator==(StringView other) const
  148. {
  149. if (is_null())
  150. return other.is_null();
  151. if (other.is_null())
  152. return false;
  153. if (length() != other.length())
  154. return false;
  155. return __builtin_memcmp(m_characters, other.m_characters, m_length) == 0;
  156. }
  157. constexpr bool operator!=(StringView other) const
  158. {
  159. return !(*this == other);
  160. }
  161. bool operator<(StringView other) const
  162. {
  163. if (int c = __builtin_memcmp(m_characters, other.m_characters, min(m_length, other.m_length)))
  164. return c < 0;
  165. return m_length < other.m_length;
  166. }
  167. [[nodiscard]] String to_string() const;
  168. [[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
  169. [[nodiscard]] String replace(StringView needle, StringView replacement, bool all_occurrences = false) const;
  170. [[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(*this, needle); }
  171. template<typename... Ts>
  172. [[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
  173. {
  174. return (... || this->operator==(forward<Ts>(strings)));
  175. }
  176. private:
  177. friend class String;
  178. const char* m_characters { nullptr };
  179. size_t m_length { 0 };
  180. };
  181. template<>
  182. struct Traits<StringView> : public GenericTraits<StringView> {
  183. static unsigned hash(StringView s) { return s.hash(); }
  184. };
  185. }
  186. [[nodiscard]] ALWAYS_INLINE constexpr AK::StringView operator"" sv(const char* cstring, size_t length)
  187. {
  188. return AK::StringView(cstring, length);
  189. }
  190. using AK::StringView;