2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-04-16 23:18:39 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-04-29 21:17:54 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Checked.h>
|
2022-10-22 13:38:21 +00:00
|
|
|
#include <AK/EnumBits.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Forward.h>
|
2021-11-10 10:05:21 +00:00
|
|
|
#include <AK/Optional.h>
|
2020-07-27 12:15:37 +00:00
|
|
|
#include <AK/Span.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2021-05-14 13:21:50 +00:00
|
|
|
#include <AK/StringHash.h>
|
2020-02-26 07:25:24 +00:00
|
|
|
#include <AK/StringUtils.h>
|
2019-04-16 00:39:16 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
class StringView {
|
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
ALWAYS_INLINE constexpr StringView() = default;
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE constexpr StringView(char const* characters, size_t length)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_characters(characters)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
2021-06-27 18:11:38 +00:00
|
|
|
if (!is_constant_evaluated())
|
2023-03-07 14:28:21 +00:00
|
|
|
VERIFY(!Checked<uintptr_t>::addition_would_overflow(reinterpret_cast<uintptr_t>(characters), length));
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2022-04-01 17:58:27 +00:00
|
|
|
ALWAYS_INLINE StringView(unsigned char const* characters, size_t length)
|
2023-03-07 14:28:21 +00:00
|
|
|
: m_characters(reinterpret_cast<char const*>(characters))
|
2019-05-28 09:53:16 +00:00
|
|
|
, m_length(length)
|
|
|
|
{
|
2023-03-07 14:28:21 +00:00
|
|
|
VERIFY(!Checked<uintptr_t>::addition_would_overflow(reinterpret_cast<uintptr_t>(characters), length));
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2020-08-19 09:43:41 +00:00
|
|
|
ALWAYS_INLINE StringView(ReadonlyBytes bytes)
|
2022-04-01 17:58:27 +00:00
|
|
|
: m_characters(reinterpret_cast<char const*>(bytes.data()))
|
2020-08-19 09:43:41 +00:00
|
|
|
, m_length(bytes.size())
|
|
|
|
{
|
|
|
|
}
|
2019-06-29 10:03:28 +00:00
|
|
|
|
2022-12-10 16:13:47 +00:00
|
|
|
// Note: This is here for Jakt.
|
|
|
|
ALWAYS_INLINE static StringView from_string_literal(StringView string)
|
|
|
|
{
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
StringView(ByteBuffer const&);
|
2022-02-15 22:24:24 +00:00
|
|
|
#ifndef KERNEL
|
2022-12-06 20:39:11 +00:00
|
|
|
StringView(String const&);
|
2023-01-28 13:26:22 +00:00
|
|
|
StringView(FlyString const&);
|
2023-12-16 14:19:34 +00:00
|
|
|
StringView(ByteString const&);
|
2023-01-09 00:23:00 +00:00
|
|
|
StringView(DeprecatedFlyString const&);
|
2022-02-15 22:24:24 +00:00
|
|
|
#endif
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2021-09-10 21:26:22 +00:00
|
|
|
explicit StringView(ByteBuffer&&) = delete;
|
2022-02-15 22:24:24 +00:00
|
|
|
#ifndef KERNEL
|
2022-12-06 20:39:11 +00:00
|
|
|
explicit StringView(String&&) = delete;
|
2023-01-28 13:26:22 +00:00
|
|
|
explicit StringView(FlyString&&) = delete;
|
2023-12-16 14:19:34 +00:00
|
|
|
explicit StringView(ByteString&&) = delete;
|
2023-01-09 00:23:00 +00:00
|
|
|
explicit StringView(DeprecatedFlyString&&) = delete;
|
2022-02-15 22:24:24 +00:00
|
|
|
#endif
|
2021-09-04 14:53:43 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
template<OneOf<String, FlyString, ByteString, DeprecatedFlyString, ByteBuffer> StringType>
|
2023-01-28 13:22:54 +00:00
|
|
|
StringView& operator=(StringType&&) = delete;
|
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
[[nodiscard]] constexpr bool is_null() const
|
|
|
|
{
|
|
|
|
return m_characters == nullptr;
|
|
|
|
}
|
2021-04-22 03:19:39 +00:00
|
|
|
[[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2021-05-14 13:21:50 +00:00
|
|
|
[[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
|
2021-04-22 03:19:39 +00:00
|
|
|
[[nodiscard]] constexpr size_t length() const { return m_length; }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2021-04-11 08:30:53 +00:00
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2022-07-25 23:38:49 +00:00
|
|
|
constexpr char const& operator[](size_t index) const
|
|
|
|
{
|
|
|
|
if (!is_constant_evaluated())
|
|
|
|
VERIFY(index < m_length);
|
|
|
|
return m_characters[index];
|
|
|
|
}
|
2020-03-10 08:13:29 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
using ConstIterator = SimpleIterator<const StringView, char const>;
|
2020-09-06 19:14:08 +00:00
|
|
|
|
2021-04-11 08:30:53 +00:00
|
|
|
[[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
[[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2021-05-14 13:21:50 +00:00
|
|
|
[[nodiscard]] constexpr unsigned hash() const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return 0;
|
|
|
|
return string_hash(characters_without_null_termination(), length());
|
|
|
|
}
|
2019-08-24 20:31:06 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
[[nodiscard]] bool ends_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2021-04-11 08:30:53 +00:00
|
|
|
[[nodiscard]] bool starts_with(char) const;
|
|
|
|
[[nodiscard]] bool ends_with(char) const;
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] bool matches(StringView mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
|
|
|
[[nodiscard]] bool matches(StringView mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
2021-04-11 08:30:53 +00:00
|
|
|
[[nodiscard]] bool contains(char) const;
|
2022-09-12 14:31:16 +00:00
|
|
|
[[nodiscard]] bool contains(u32) const;
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2023-03-10 07:48:54 +00:00
|
|
|
[[nodiscard]] bool equals_ignoring_ascii_case(StringView) const;
|
2019-09-12 11:13:07 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] StringView trim(StringView characters, TrimMode mode = TrimMode::Both) const { return StringUtils::trim(*this, characters, mode); }
|
2021-04-11 08:30:53 +00:00
|
|
|
[[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
|
2020-09-20 13:35:04 +00:00
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
#ifndef KERNEL
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString to_lowercase_string() const;
|
|
|
|
[[nodiscard]] ByteString to_uppercase_string() const;
|
|
|
|
[[nodiscard]] ByteString to_titlecase_string() const;
|
2022-02-15 22:24:24 +00:00
|
|
|
#endif
|
2021-07-01 11:45:59 +00:00
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const
|
|
|
|
{
|
|
|
|
return StringUtils::find(*this, needle, start);
|
|
|
|
}
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
2021-07-01 12:58:37 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
|
2022-12-15 21:20:14 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find_last(StringView needle) const { return StringUtils::find_last(*this, needle); }
|
2022-09-30 19:19:53 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find_last_not(char needle) const { return StringUtils::find_last_not(*this, needle); }
|
2021-01-12 19:58:45 +00:00
|
|
|
|
2021-11-10 10:05:21 +00:00
|
|
|
[[nodiscard]] Vector<size_t> find_all(StringView needle) const;
|
2021-07-01 15:00:34 +00:00
|
|
|
|
2021-07-01 16:12:21 +00:00
|
|
|
using SearchDirection = StringUtils::SearchDirection;
|
2021-10-31 20:53:22 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find_any_of(StringView needles, SearchDirection direction = SearchDirection::Forward) const { return StringUtils::find_any_of(*this, needles, direction); }
|
2021-07-01 13:01:29 +00:00
|
|
|
|
2021-04-22 03:19:39 +00:00
|
|
|
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
|
|
|
|
{
|
2021-06-27 18:11:38 +00:00
|
|
|
if (!is_constant_evaluated())
|
|
|
|
VERIFY(start + length <= m_length);
|
2021-04-22 03:19:39 +00:00
|
|
|
return { m_characters + start, length };
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr StringView substring_view(size_t start) const
|
|
|
|
{
|
2021-10-24 21:29:42 +00:00
|
|
|
if (!is_constant_evaluated())
|
|
|
|
VERIFY(start <= length());
|
2021-04-22 03:19:39 +00:00
|
|
|
return substring_view(start, length() - start);
|
|
|
|
}
|
|
|
|
|
2022-10-22 13:38:21 +00:00
|
|
|
[[nodiscard]] Vector<StringView> split_view(char, SplitBehavior = SplitBehavior::Nothing) const;
|
|
|
|
[[nodiscard]] Vector<StringView> split_view(StringView, SplitBehavior = SplitBehavior::Nothing) const;
|
2019-08-04 09:44:20 +00:00
|
|
|
|
2022-10-22 13:38:21 +00:00
|
|
|
[[nodiscard]] Vector<StringView> split_view_if(Function<bool(char)> const& predicate, SplitBehavior = SplitBehavior::Nothing) const;
|
2021-04-12 11:54:22 +00:00
|
|
|
|
2022-06-18 16:35:36 +00:00
|
|
|
[[nodiscard]] StringView find_last_split_view(char separator) const
|
|
|
|
{
|
|
|
|
auto begin = find_last(separator);
|
|
|
|
if (!begin.has_value())
|
|
|
|
return *this;
|
|
|
|
return substring_view(begin.release_value() + 1);
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:31:37 +00:00
|
|
|
[[nodiscard]] StringView find_first_split_view(char separator) const
|
|
|
|
{
|
|
|
|
auto needle_begin = find(separator);
|
|
|
|
if (!needle_begin.has_value())
|
|
|
|
return *this;
|
|
|
|
return substring_view(0, needle_begin.release_value());
|
|
|
|
}
|
|
|
|
|
2023-02-04 17:54:55 +00:00
|
|
|
template<typename Callback>
|
|
|
|
auto for_each_split_view(char separator, SplitBehavior split_behavior, Callback callback) const
|
2022-01-09 10:26:45 +00:00
|
|
|
{
|
|
|
|
StringView seperator_view { &separator, 1 };
|
2023-02-04 17:54:55 +00:00
|
|
|
return for_each_split_view(seperator_view, split_behavior, callback);
|
2022-01-09 10:26:45 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 17:54:55 +00:00
|
|
|
template<typename Callback>
|
|
|
|
auto for_each_split_view(StringView separator, SplitBehavior split_behavior, Callback callback) const
|
2022-01-09 10:26:45 +00:00
|
|
|
{
|
|
|
|
VERIFY(!separator.is_empty());
|
2023-02-04 17:54:55 +00:00
|
|
|
// FIXME: This can't go in the template header since declval won't allow the incomplete StringView type.
|
|
|
|
using CallbackReturn = decltype(declval<Callback>()(StringView {}));
|
|
|
|
constexpr auto ReturnsErrorOr = IsSpecializationOf<CallbackReturn, ErrorOr>;
|
|
|
|
using ReturnType = Conditional<ReturnsErrorOr, ErrorOr<void>, void>;
|
|
|
|
return [&]() -> ReturnType {
|
|
|
|
if (is_empty())
|
|
|
|
return ReturnType();
|
|
|
|
|
|
|
|
StringView view { *this };
|
|
|
|
auto maybe_separator_index = find(separator);
|
|
|
|
bool keep_empty = has_flag(split_behavior, SplitBehavior::KeepEmpty);
|
|
|
|
bool keep_separator = has_flag(split_behavior, SplitBehavior::KeepTrailingSeparator);
|
|
|
|
while (maybe_separator_index.has_value()) {
|
|
|
|
auto separator_index = maybe_separator_index.value();
|
|
|
|
auto part_with_separator = view.substring_view(0, separator_index + separator.length());
|
|
|
|
if (keep_empty || separator_index > 0) {
|
|
|
|
auto part = part_with_separator;
|
|
|
|
if (!keep_separator)
|
|
|
|
part = part_with_separator.substring_view(0, separator_index);
|
|
|
|
if constexpr (ReturnsErrorOr)
|
|
|
|
TRY(callback(part));
|
|
|
|
else
|
|
|
|
callback(part);
|
|
|
|
}
|
|
|
|
view = view.substring_view_starting_after_substring(part_with_separator);
|
|
|
|
maybe_separator_index = view.find(separator);
|
|
|
|
}
|
|
|
|
if (keep_empty || !view.is_empty()) {
|
|
|
|
if constexpr (ReturnsErrorOr)
|
|
|
|
TRY(callback(view));
|
2022-10-22 14:31:59 +00:00
|
|
|
else
|
2023-02-04 17:54:55 +00:00
|
|
|
callback(view);
|
2022-10-22 14:31:59 +00:00
|
|
|
}
|
2023-02-04 17:54:55 +00:00
|
|
|
|
|
|
|
return ReturnType();
|
|
|
|
}();
|
2022-01-09 10:26:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 12:42:33 +00:00
|
|
|
// Create a Vector of StringViews split by line endings. As of CommonMark
|
|
|
|
// 0.29, the spec defines a line ending as "a newline (U+000A), a carriage
|
|
|
|
// return (U+000D) not followed by a newline, or a carriage return and a
|
|
|
|
// following newline.".
|
2021-04-11 08:30:53 +00:00
|
|
|
[[nodiscard]] Vector<StringView> lines(bool consider_cr = true) const;
|
2019-12-02 12:42:33 +00:00
|
|
|
|
2019-06-13 13:30:55 +00:00
|
|
|
// Create a new substring view of this string view, starting either at the beginning of
|
|
|
|
// the given substring view, or after its end, and continuing until the end of this string
|
|
|
|
// view (that is, for the remaining part of its length). For example,
|
|
|
|
//
|
|
|
|
// StringView str { "foobar" };
|
|
|
|
// StringView substr = str.substring_view(1, 2); // "oo"
|
|
|
|
// StringView substr_from = str.substring_view_starting_from_substring(subst); // "oobar"
|
|
|
|
// StringView substr_after = str.substring_view_starting_after_substring(subst); // "bar"
|
|
|
|
//
|
|
|
|
// Note that this only works if the string view passed as an argument is indeed a substring
|
|
|
|
// view of this string view, such as one created by substring_view() and split_view(). It
|
|
|
|
// does not work for arbitrary strings; for example declaring substr in the example above as
|
|
|
|
//
|
|
|
|
// StringView substr { "oo" };
|
|
|
|
//
|
|
|
|
// would not work.
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] StringView substring_view_starting_from_substring(StringView substring) const;
|
|
|
|
[[nodiscard]] StringView substring_view_starting_after_substring(StringView substring) const;
|
2019-06-13 13:30:55 +00:00
|
|
|
|
2022-03-29 00:52:20 +00:00
|
|
|
[[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const;
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr bool operator==(char const* cstring) const
|
2019-06-07 17:22:58 +00:00
|
|
|
{
|
2019-06-08 16:30:40 +00:00
|
|
|
if (is_null())
|
2021-11-06 20:12:16 +00:00
|
|
|
return cstring == nullptr;
|
2019-06-08 16:30:40 +00:00
|
|
|
if (!cstring)
|
|
|
|
return false;
|
2021-02-23 13:13:57 +00:00
|
|
|
// NOTE: `m_characters` is not guaranteed to be null-terminated, but `cstring` is.
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* cp = cstring;
|
2021-02-23 13:13:57 +00:00
|
|
|
for (size_t i = 0; i < m_length; ++i) {
|
2021-11-06 20:12:16 +00:00
|
|
|
if (*cp == '\0')
|
2021-02-24 21:10:32 +00:00
|
|
|
return false;
|
2021-02-23 13:13:57 +00:00
|
|
|
if (m_characters[i] != *(cp++))
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 20:12:16 +00:00
|
|
|
return *cp == '\0';
|
2019-06-07 17:22:58 +00:00
|
|
|
}
|
2021-02-23 13:13:57 +00:00
|
|
|
|
2022-06-10 16:52:14 +00:00
|
|
|
constexpr bool operator==(char const c) const
|
|
|
|
{
|
|
|
|
return m_length == 1 && *m_characters == c;
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
#ifndef KERNEL
|
2023-12-16 14:19:34 +00:00
|
|
|
bool operator==(ByteString const&) const;
|
2022-02-15 22:24:24 +00:00
|
|
|
#endif
|
2019-04-16 00:39:16 +00:00
|
|
|
|
2022-01-29 14:49:33 +00:00
|
|
|
[[nodiscard]] constexpr int compare(StringView other) const
|
2019-08-15 12:07:23 +00:00
|
|
|
{
|
2022-01-30 15:53:49 +00:00
|
|
|
if (m_characters == nullptr)
|
|
|
|
return other.m_characters ? -1 : 0;
|
|
|
|
|
|
|
|
if (other.m_characters == nullptr)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
size_t rlen = min(m_length, other.m_length);
|
|
|
|
int c = __builtin_memcmp(m_characters, other.m_characters, rlen);
|
2022-01-29 14:49:33 +00:00
|
|
|
if (c == 0) {
|
|
|
|
if (length() < other.length())
|
|
|
|
return -1;
|
|
|
|
if (length() == other.length())
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return c;
|
2019-08-15 12:07:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 14:49:33 +00:00
|
|
|
constexpr bool operator==(StringView other) const
|
2019-08-15 12:07:23 +00:00
|
|
|
{
|
2022-01-29 14:49:33 +00:00
|
|
|
return length() == other.length() && compare(other) == 0;
|
2019-08-15 12:07:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 14:49:33 +00:00
|
|
|
constexpr bool operator!=(StringView other) const
|
2020-08-10 01:55:32 +00:00
|
|
|
{
|
2022-01-29 14:49:33 +00:00
|
|
|
return length() != other.length() || compare(other) != 0;
|
2020-08-10 01:55:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 14:49:33 +00:00
|
|
|
constexpr bool operator<(StringView other) const { return compare(other) < 0; }
|
|
|
|
|
|
|
|
constexpr bool operator<=(StringView other) const { return compare(other) <= 0; }
|
|
|
|
|
|
|
|
constexpr bool operator>(StringView other) const { return compare(other) > 0; }
|
|
|
|
|
|
|
|
constexpr bool operator>=(StringView other) const { return compare(other) >= 0; }
|
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
#ifndef KERNEL
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString to_byte_string() const;
|
2022-02-15 22:24:24 +00:00
|
|
|
#endif
|
2020-05-06 16:53:05 +00:00
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
[[nodiscard]] bool is_whitespace() const
|
|
|
|
{
|
|
|
|
return StringUtils::is_whitespace(*this);
|
|
|
|
}
|
2021-01-02 23:26:02 +00:00
|
|
|
|
2022-02-15 22:24:24 +00:00
|
|
|
#ifndef KERNEL
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString replace(StringView needle, StringView replacement, ReplaceMode) const;
|
2022-02-15 22:24:24 +00:00
|
|
|
#endif
|
|
|
|
[[nodiscard]] size_t count(StringView needle) const
|
|
|
|
{
|
|
|
|
return StringUtils::count(*this, needle);
|
|
|
|
}
|
2021-09-10 22:02:24 +00:00
|
|
|
|
2023-08-15 01:55:36 +00:00
|
|
|
[[nodiscard]] size_t count(char needle) const
|
|
|
|
{
|
|
|
|
return StringUtils::count(*this, needle);
|
|
|
|
}
|
|
|
|
|
2021-06-04 09:46:29 +00:00
|
|
|
template<typename... Ts>
|
2021-07-31 23:27:25 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
|
2020-10-27 21:58:11 +00:00
|
|
|
{
|
2021-06-04 09:46:29 +00:00
|
|
|
return (... || this->operator==(forward<Ts>(strings)));
|
2020-10-27 21:58:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 22:52:36 +00:00
|
|
|
template<typename... Ts>
|
2023-03-10 07:48:54 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of_ignoring_ascii_case(Ts&&... strings) const
|
2022-03-20 22:52:36 +00:00
|
|
|
{
|
|
|
|
return (... ||
|
|
|
|
[this, &strings]() -> bool {
|
|
|
|
if constexpr (requires(Ts a) { a.view()->StringView; })
|
2023-03-10 07:48:54 +00:00
|
|
|
return this->equals_ignoring_ascii_case(forward<Ts>(strings.view()));
|
2022-03-20 22:52:36 +00:00
|
|
|
else
|
2023-03-10 07:48:54 +00:00
|
|
|
return this->equals_ignoring_ascii_case(forward<Ts>(strings));
|
2022-03-20 22:52:36 +00:00
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2023-12-22 22:35:03 +00:00
|
|
|
template<Arithmetic T>
|
|
|
|
Optional<T> to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const
|
|
|
|
{
|
|
|
|
#ifndef KERNEL
|
|
|
|
if constexpr (IsFloatingPoint<T>)
|
|
|
|
return StringUtils::convert_to_floating_point<T>(*this, trim_whitespace);
|
|
|
|
#endif
|
|
|
|
if constexpr (IsSigned<T>)
|
|
|
|
return StringUtils::convert_to_int<T>(*this, trim_whitespace);
|
|
|
|
else
|
|
|
|
return StringUtils::convert_to_uint<T>(*this, trim_whitespace);
|
|
|
|
}
|
|
|
|
|
2019-04-15 12:56:37 +00:00
|
|
|
private:
|
2023-12-16 14:19:34 +00:00
|
|
|
friend class ByteString;
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* m_characters { nullptr };
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t m_length { 0 };
|
2019-04-15 12:56:37 +00:00
|
|
|
};
|
2019-04-16 00:39:16 +00:00
|
|
|
|
2020-10-08 17:48:56 +00:00
|
|
|
template<>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<StringView> : public DefaultTraits<StringView> {
|
2021-11-10 23:55:02 +00:00
|
|
|
static unsigned hash(StringView s) { return s.hash(); }
|
2020-10-08 17:48:56 +00:00
|
|
|
};
|
|
|
|
|
2023-03-14 11:40:53 +00:00
|
|
|
struct CaseInsensitiveASCIIStringViewTraits : public Traits<StringView> {
|
2022-01-10 16:47:23 +00:00
|
|
|
static unsigned hash(StringView s)
|
|
|
|
{
|
|
|
|
if (s.is_empty())
|
|
|
|
return 0;
|
|
|
|
return case_insensitive_string_hash(s.characters_without_null_termination(), s.length());
|
|
|
|
}
|
2023-03-10 07:48:54 +00:00
|
|
|
static bool equals(StringView const& a, StringView const& b) { return a.equals_ignoring_ascii_case(b); }
|
2022-01-10 16:47:23 +00:00
|
|
|
};
|
|
|
|
|
2019-04-16 00:39:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 15:44:39 +00:00
|
|
|
// FIXME: Remove this when clang on BSD distributions fully support consteval (specifically in the context of default parameter initialization).
|
|
|
|
// Note that this is fixed in clang-15, but is not yet picked up by all downstream distributions.
|
|
|
|
// See: https://github.com/llvm/llvm-project/issues/48230
|
2023-06-28 01:31:58 +00:00
|
|
|
// Additionally, oss-fuzz currently ships an llvm-project commit that is a pre-release of 15.0.0.
|
|
|
|
// See: https://github.com/google/oss-fuzz/issues/9989
|
2023-07-19 09:56:12 +00:00
|
|
|
// Android currently doesn't ship clang-15 in any NDK
|
|
|
|
#if defined(AK_OS_BSD_GENERIC) || defined(OSS_FUZZ) || defined(AK_OS_ANDROID)
|
2022-03-17 18:53:10 +00:00
|
|
|
# define AK_STRING_VIEW_LITERAL_CONSTEVAL constexpr
|
|
|
|
#else
|
|
|
|
# define AK_STRING_VIEW_LITERAL_CONSTEVAL consteval
|
|
|
|
#endif
|
|
|
|
|
2023-08-17 16:27:39 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE AK_STRING_VIEW_LITERAL_CONSTEVAL AK::StringView operator""sv(char const* cstring, size_t length)
|
2021-02-24 12:50:00 +00:00
|
|
|
{
|
|
|
|
return AK::StringView(cstring, length);
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-03-14 11:40:53 +00:00
|
|
|
using AK::CaseInsensitiveASCIIStringViewTraits;
|
2019-04-16 00:39:16 +00:00
|
|
|
using AK::StringView;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|