2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-04-17 02:18:39 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-04-15 15:56:37 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-04-30 00:17:54 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Checked.h>
|
2024-02-22 18:26:37 +03:00
|
|
|
#include <AK/Concepts.h>
|
2022-10-22 16:38:21 +03:00
|
|
|
#include <AK/EnumBits.h>
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/Forward.h>
|
2021-11-10 13:05:21 +03:00
|
|
|
#include <AK/Optional.h>
|
2020-07-27 15:15:37 +03:00
|
|
|
#include <AK/Span.h>
|
2020-02-14 23:41:10 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2021-05-14 16:21:50 +03:00
|
|
|
#include <AK/StringHash.h>
|
2020-02-26 10:25:24 +03:00
|
|
|
#include <AK/StringUtils.h>
|
2019-04-16 03:39:16 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-04-15 15:56:37 +03:00
|
|
|
class StringView {
|
|
|
|
public:
|
2021-01-11 02:29:28 +03:00
|
|
|
ALWAYS_INLINE constexpr StringView() = default;
|
2022-04-01 20:58:27 +03:00
|
|
|
ALWAYS_INLINE constexpr StringView(char const* characters, size_t length)
|
2019-05-28 12:53:16 +03:00
|
|
|
: m_characters(characters)
|
|
|
|
, m_length(length)
|
|
|
|
{
|
2021-06-27 21:11:38 +03:00
|
|
|
if (!is_constant_evaluated())
|
2023-03-07 17:28:21 +03:00
|
|
|
VERIFY(!Checked<uintptr_t>::addition_would_overflow(reinterpret_cast<uintptr_t>(characters), length));
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2022-04-01 20:58:27 +03:00
|
|
|
ALWAYS_INLINE StringView(unsigned char const* characters, size_t length)
|
2023-03-07 17:28:21 +03:00
|
|
|
: m_characters(reinterpret_cast<char const*>(characters))
|
2019-05-28 12:53:16 +03:00
|
|
|
, m_length(length)
|
|
|
|
{
|
2023-03-07 17:28:21 +03:00
|
|
|
VERIFY(!Checked<uintptr_t>::addition_would_overflow(reinterpret_cast<uintptr_t>(characters), length));
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2020-08-19 12:43:41 +03:00
|
|
|
ALWAYS_INLINE StringView(ReadonlyBytes bytes)
|
2022-04-01 20:58:27 +03:00
|
|
|
: m_characters(reinterpret_cast<char const*>(bytes.data()))
|
2020-08-19 12:43:41 +03:00
|
|
|
, m_length(bytes.size())
|
|
|
|
{
|
|
|
|
}
|
2019-06-29 13:03:28 +03:00
|
|
|
|
2022-12-10 19:13:47 +03:00
|
|
|
// Note: This is here for Jakt.
|
|
|
|
ALWAYS_INLINE static StringView from_string_literal(StringView string)
|
|
|
|
{
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
StringView(ByteBuffer const&);
|
2022-02-16 01:24:24 +03:00
|
|
|
#ifndef KERNEL
|
2022-12-06 23:39:11 +03:00
|
|
|
StringView(String const&);
|
2023-01-28 16:26:22 +03:00
|
|
|
StringView(FlyString const&);
|
2023-12-16 17:19:34 +03:00
|
|
|
StringView(ByteString const&);
|
2023-01-09 03:23:00 +03:00
|
|
|
StringView(DeprecatedFlyString const&);
|
2022-02-16 01:24:24 +03:00
|
|
|
#endif
|
2019-04-15 15:56:37 +03:00
|
|
|
|
2021-09-11 00:26:22 +03:00
|
|
|
explicit StringView(ByteBuffer&&) = delete;
|
2022-02-16 01:24:24 +03:00
|
|
|
#ifndef KERNEL
|
2022-12-06 23:39:11 +03:00
|
|
|
explicit StringView(String&&) = delete;
|
2023-01-28 16:26:22 +03:00
|
|
|
explicit StringView(FlyString&&) = delete;
|
2023-12-16 17:19:34 +03:00
|
|
|
explicit StringView(ByteString&&) = delete;
|
2023-01-09 03:23:00 +03:00
|
|
|
explicit StringView(DeprecatedFlyString&&) = delete;
|
2022-02-16 01:24:24 +03:00
|
|
|
#endif
|
2021-09-04 17:53:43 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
template<OneOf<String, FlyString, ByteString, DeprecatedFlyString, ByteBuffer> StringType>
|
2023-01-28 16:22:54 +03:00
|
|
|
StringView& operator=(StringType&&) = delete;
|
|
|
|
|
2022-02-16 01:24:24 +03:00
|
|
|
[[nodiscard]] constexpr bool is_null() const
|
|
|
|
{
|
|
|
|
return m_characters == nullptr;
|
|
|
|
}
|
2021-04-22 06:19:39 +03:00
|
|
|
[[nodiscard]] constexpr bool is_empty() const { return m_length == 0; }
|
2020-07-27 15:15:37 +03:00
|
|
|
|
2021-05-14 16:21:50 +03:00
|
|
|
[[nodiscard]] constexpr char const* characters_without_null_termination() const { return m_characters; }
|
2021-04-22 06:19:39 +03:00
|
|
|
[[nodiscard]] constexpr size_t length() const { return m_length; }
|
2020-07-27 15:15:37 +03:00
|
|
|
|
2021-04-11 11:30:53 +03:00
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const { return { m_characters, m_length }; }
|
2020-07-27 15:15:37 +03:00
|
|
|
|
2022-07-26 02:38:49 +03:00
|
|
|
constexpr char const& operator[](size_t index) const
|
|
|
|
{
|
|
|
|
if (!is_constant_evaluated())
|
|
|
|
VERIFY(index < m_length);
|
|
|
|
return m_characters[index];
|
|
|
|
}
|
2020-03-10 11:13:29 +03:00
|
|
|
|
2024-01-04 00:43:01 +03:00
|
|
|
using ConstIterator = SimpleIterator<StringView const, char const>;
|
2020-09-06 22:14:08 +03:00
|
|
|
|
2021-04-11 11:30:53 +03:00
|
|
|
[[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
[[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
|
2019-04-15 15:56:37 +03:00
|
|
|
|
2021-05-14 16:21:50 +03:00
|
|
|
[[nodiscard]] constexpr unsigned hash() const
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return 0;
|
|
|
|
return string_hash(characters_without_null_termination(), length());
|
|
|
|
}
|
2019-08-24 23:31:06 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
[[nodiscard]] bool starts_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
[[nodiscard]] bool ends_with(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2021-04-11 11:30:53 +03:00
|
|
|
[[nodiscard]] bool starts_with(char) const;
|
|
|
|
[[nodiscard]] bool ends_with(char) const;
|
2021-11-11 02:55:02 +03:00
|
|
|
[[nodiscard]] bool matches(StringView mask, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
|
|
|
[[nodiscard]] bool matches(StringView mask, Vector<MaskSpan>&, CaseSensitivity = CaseSensitivity::CaseInsensitive) const;
|
2021-04-11 11:30:53 +03:00
|
|
|
[[nodiscard]] bool contains(char) const;
|
2022-09-12 17:31:16 +03:00
|
|
|
[[nodiscard]] bool contains(u32) const;
|
2021-11-11 02:55:02 +03:00
|
|
|
[[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2023-03-10 10:48:54 +03:00
|
|
|
[[nodiscard]] bool equals_ignoring_ascii_case(StringView) const;
|
2019-09-12 14:13:07 +03:00
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
[[nodiscard]] StringView trim(StringView characters, TrimMode mode = TrimMode::Both) const { return StringUtils::trim(*this, characters, mode); }
|
2021-04-11 11:30:53 +03:00
|
|
|
[[nodiscard]] StringView trim_whitespace(TrimMode mode = TrimMode::Both) const { return StringUtils::trim_whitespace(*this, mode); }
|
2020-09-20 16:35:04 +03:00
|
|
|
|
2022-02-16 01:24:24 +03:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:19:34 +03:00
|
|
|
[[nodiscard]] ByteString to_lowercase_string() const;
|
|
|
|
[[nodiscard]] ByteString to_uppercase_string() const;
|
|
|
|
[[nodiscard]] ByteString to_titlecase_string() const;
|
2022-02-16 01:24:24 +03:00
|
|
|
#endif
|
2021-07-01 14:45:59 +03:00
|
|
|
|
2022-02-16 01:24:24 +03:00
|
|
|
[[nodiscard]] Optional<size_t> find(char needle, size_t start = 0) const
|
|
|
|
{
|
|
|
|
return StringUtils::find(*this, needle, start);
|
|
|
|
}
|
2021-11-11 02:55:02 +03:00
|
|
|
[[nodiscard]] Optional<size_t> find(StringView needle, size_t start = 0) const { return StringUtils::find(*this, needle, start); }
|
2021-07-01 15:58:37 +03:00
|
|
|
[[nodiscard]] Optional<size_t> find_last(char needle) const { return StringUtils::find_last(*this, needle); }
|
2022-12-16 00:20:14 +03:00
|
|
|
[[nodiscard]] Optional<size_t> find_last(StringView needle) const { return StringUtils::find_last(*this, needle); }
|
2022-09-30 22:19:53 +03:00
|
|
|
[[nodiscard]] Optional<size_t> find_last_not(char needle) const { return StringUtils::find_last_not(*this, needle); }
|
2021-01-12 22:58:45 +03:00
|
|
|
|
2021-11-10 13:05:21 +03:00
|
|
|
[[nodiscard]] Vector<size_t> find_all(StringView needle) const;
|
2021-07-01 18:00:34 +03:00
|
|
|
|
2021-07-01 19:12:21 +03:00
|
|
|
using SearchDirection = StringUtils::SearchDirection;
|
2021-10-31 23:53:22 +03: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 16:01:29 +03:00
|
|
|
|
2021-04-22 06:19:39 +03:00
|
|
|
[[nodiscard]] constexpr StringView substring_view(size_t start, size_t length) const
|
|
|
|
{
|
2021-06-27 21:11:38 +03:00
|
|
|
if (!is_constant_evaluated())
|
|
|
|
VERIFY(start + length <= m_length);
|
2021-04-22 06:19:39 +03:00
|
|
|
return { m_characters + start, length };
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr StringView substring_view(size_t start) const
|
|
|
|
{
|
2021-10-25 00:29:42 +03:00
|
|
|
if (!is_constant_evaluated())
|
|
|
|
VERIFY(start <= length());
|
2021-04-22 06:19:39 +03:00
|
|
|
return substring_view(start, length() - start);
|
|
|
|
}
|
|
|
|
|
2022-10-22 16:38:21 +03:00
|
|
|
[[nodiscard]] Vector<StringView> split_view(char, SplitBehavior = SplitBehavior::Nothing) const;
|
|
|
|
[[nodiscard]] Vector<StringView> split_view(StringView, SplitBehavior = SplitBehavior::Nothing) const;
|
2019-08-04 12:44:20 +03:00
|
|
|
|
2022-10-22 16:38:21 +03:00
|
|
|
[[nodiscard]] Vector<StringView> split_view_if(Function<bool(char)> const& predicate, SplitBehavior = SplitBehavior::Nothing) const;
|
2021-04-12 14:54:22 +03:00
|
|
|
|
2022-06-18 19:35:36 +03: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 05:31:37 +03: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 20:54:55 +03:00
|
|
|
template<typename Callback>
|
|
|
|
auto for_each_split_view(char separator, SplitBehavior split_behavior, Callback callback) const
|
2022-01-09 13:26:45 +03:00
|
|
|
{
|
|
|
|
StringView seperator_view { &separator, 1 };
|
2023-02-04 20:54:55 +03:00
|
|
|
return for_each_split_view(seperator_view, split_behavior, callback);
|
2022-01-09 13:26:45 +03:00
|
|
|
}
|
|
|
|
|
2023-02-04 20:54:55 +03:00
|
|
|
template<typename Callback>
|
|
|
|
auto for_each_split_view(StringView separator, SplitBehavior split_behavior, Callback callback) const
|
2022-01-09 13:26:45 +03:00
|
|
|
{
|
|
|
|
VERIFY(!separator.is_empty());
|
2023-02-04 20:54:55 +03:00
|
|
|
// FIXME: This can't go in the template header since declval won't allow the incomplete StringView type.
|
2024-02-22 18:26:37 +03:00
|
|
|
using CallbackReturn = InvokeResult<Callback, StringView>;
|
|
|
|
constexpr auto ReturnsErrorOr = FallibleFunction<Callback, StringView>;
|
|
|
|
// FIXME: We might need a concept for this...
|
|
|
|
constexpr auto ReturnsIterationDecision = []() -> bool {
|
|
|
|
if constexpr (ReturnsErrorOr)
|
|
|
|
return IsSame<typename CallbackReturn::ResultType, IterationDecision>;
|
|
|
|
return IsSame<CallbackReturn, IterationDecision>;
|
|
|
|
}();
|
2023-02-04 20:54:55 +03:00
|
|
|
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);
|
2024-02-22 18:26:37 +03:00
|
|
|
if constexpr (ReturnsErrorOr) {
|
|
|
|
if constexpr (ReturnsIterationDecision) {
|
|
|
|
if (TRY(callback(part)) == IterationDecision::Break)
|
|
|
|
return ReturnType();
|
|
|
|
} else {
|
|
|
|
TRY(callback(part));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if constexpr (ReturnsIterationDecision) {
|
|
|
|
if (callback(part) == IterationDecision::Break)
|
|
|
|
return ReturnType();
|
|
|
|
} else {
|
|
|
|
callback(part);
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 20:54:55 +03:00
|
|
|
}
|
|
|
|
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 17:31:59 +03:00
|
|
|
else
|
2023-02-04 20:54:55 +03:00
|
|
|
callback(view);
|
2022-10-22 17:31:59 +03:00
|
|
|
}
|
2023-02-04 20:54:55 +03:00
|
|
|
|
|
|
|
return ReturnType();
|
|
|
|
}();
|
2022-01-09 13:26:45 +03:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:42:33 +03: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.".
|
2024-03-08 19:27:48 +03:00
|
|
|
enum class ConsiderCarriageReturn {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
[[nodiscard]] Vector<StringView> lines(ConsiderCarriageReturn = ConsiderCarriageReturn::Yes) const;
|
2024-03-08 17:57:35 +03:00
|
|
|
[[nodiscard]] size_t count_lines(ConsiderCarriageReturn = ConsiderCarriageReturn::Yes) const;
|
2019-12-02 15:42:33 +03:00
|
|
|
|
2019-06-13 16:30:55 +03: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-11 02:55:02 +03:00
|
|
|
[[nodiscard]] StringView substring_view_starting_from_substring(StringView substring) const;
|
|
|
|
[[nodiscard]] StringView substring_view_starting_after_substring(StringView substring) const;
|
2019-06-13 16:30:55 +03:00
|
|
|
|
2022-03-29 03:52:20 +03:00
|
|
|
[[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr bool operator==(char const* cstring) const
|
2019-06-07 20:22:58 +03:00
|
|
|
{
|
2019-06-08 19:30:40 +03:00
|
|
|
if (is_null())
|
2021-11-06 23:12:16 +03:00
|
|
|
return cstring == nullptr;
|
2019-06-08 19:30:40 +03:00
|
|
|
if (!cstring)
|
|
|
|
return false;
|
2021-02-23 16:13:57 +03:00
|
|
|
// NOTE: `m_characters` is not guaranteed to be null-terminated, but `cstring` is.
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* cp = cstring;
|
2021-02-23 16:13:57 +03:00
|
|
|
for (size_t i = 0; i < m_length; ++i) {
|
2021-11-06 23:12:16 +03:00
|
|
|
if (*cp == '\0')
|
2021-02-25 00:10:32 +03:00
|
|
|
return false;
|
2021-02-23 16:13:57 +03:00
|
|
|
if (m_characters[i] != *(cp++))
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-06 23:12:16 +03:00
|
|
|
return *cp == '\0';
|
2019-06-07 20:22:58 +03:00
|
|
|
}
|
2021-02-23 16:13:57 +03:00
|
|
|
|
2022-06-10 19:52:14 +03:00
|
|
|
constexpr bool operator==(char const c) const
|
|
|
|
{
|
|
|
|
return m_length == 1 && *m_characters == c;
|
|
|
|
}
|
|
|
|
|
2022-02-16 01:24:24 +03:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:19:34 +03:00
|
|
|
bool operator==(ByteString const&) const;
|
2022-02-16 01:24:24 +03:00
|
|
|
#endif
|
2019-04-16 03:39:16 +03:00
|
|
|
|
2022-01-29 17:49:33 +03:00
|
|
|
[[nodiscard]] constexpr int compare(StringView other) const
|
2019-08-15 15:07:23 +03:00
|
|
|
{
|
2022-01-30 18:53:49 +03: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 17:49:33 +03:00
|
|
|
if (c == 0) {
|
|
|
|
if (length() < other.length())
|
|
|
|
return -1;
|
|
|
|
if (length() == other.length())
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return c;
|
2019-08-15 15:07:23 +03:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:49:33 +03:00
|
|
|
constexpr bool operator==(StringView other) const
|
2019-08-15 15:07:23 +03:00
|
|
|
{
|
2022-01-29 17:49:33 +03:00
|
|
|
return length() == other.length() && compare(other) == 0;
|
2019-08-15 15:07:23 +03:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:49:33 +03:00
|
|
|
constexpr bool operator!=(StringView other) const
|
2020-08-10 04:55:32 +03:00
|
|
|
{
|
2022-01-29 17:49:33 +03:00
|
|
|
return length() != other.length() || compare(other) != 0;
|
2020-08-10 04:55:32 +03:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:49:33 +03: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-16 01:24:24 +03:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:19:34 +03:00
|
|
|
[[nodiscard]] ByteString to_byte_string() const;
|
2022-02-16 01:24:24 +03:00
|
|
|
#endif
|
2020-05-06 19:53:05 +03:00
|
|
|
|
2022-02-16 01:24:24 +03:00
|
|
|
[[nodiscard]] bool is_whitespace() const
|
|
|
|
{
|
|
|
|
return StringUtils::is_whitespace(*this);
|
|
|
|
}
|
2021-01-03 02:26:02 +03:00
|
|
|
|
2022-02-16 01:24:24 +03:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:19:34 +03:00
|
|
|
[[nodiscard]] ByteString replace(StringView needle, StringView replacement, ReplaceMode) const;
|
2022-02-16 01:24:24 +03:00
|
|
|
#endif
|
|
|
|
[[nodiscard]] size_t count(StringView needle) const
|
|
|
|
{
|
|
|
|
return StringUtils::count(*this, needle);
|
|
|
|
}
|
2021-09-11 01:02:24 +03:00
|
|
|
|
2023-08-15 04:55:36 +03:00
|
|
|
[[nodiscard]] size_t count(char needle) const
|
|
|
|
{
|
|
|
|
return StringUtils::count(*this, needle);
|
|
|
|
}
|
|
|
|
|
2021-06-04 12:46:29 +03:00
|
|
|
template<typename... Ts>
|
2021-08-01 02:27:25 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
|
2020-10-28 00:58:11 +03:00
|
|
|
{
|
2021-06-04 12:46:29 +03:00
|
|
|
return (... || this->operator==(forward<Ts>(strings)));
|
2020-10-28 00:58:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-21 01:52:36 +03:00
|
|
|
template<typename... Ts>
|
2023-03-10 10:48:54 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of_ignoring_ascii_case(Ts&&... strings) const
|
2022-03-21 01:52:36 +03:00
|
|
|
{
|
|
|
|
return (... ||
|
|
|
|
[this, &strings]() -> bool {
|
|
|
|
if constexpr (requires(Ts a) { a.view()->StringView; })
|
2023-03-10 10:48:54 +03:00
|
|
|
return this->equals_ignoring_ascii_case(forward<Ts>(strings.view()));
|
2022-03-21 01:52:36 +03:00
|
|
|
else
|
2023-03-10 10:48:54 +03:00
|
|
|
return this->equals_ignoring_ascii_case(forward<Ts>(strings));
|
2022-03-21 01:52:36 +03:00
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2023-12-23 01:35:03 +03: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 15:56:37 +03:00
|
|
|
private:
|
2023-12-16 17:19:34 +03:00
|
|
|
friend class ByteString;
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* m_characters { nullptr };
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t m_length { 0 };
|
2019-04-15 15:56:37 +03:00
|
|
|
};
|
2019-04-16 03:39:16 +03:00
|
|
|
|
2020-10-08 20:48:56 +03:00
|
|
|
template<>
|
2023-11-08 22:29:12 +03:00
|
|
|
struct Traits<StringView> : public DefaultTraits<StringView> {
|
2021-11-11 02:55:02 +03:00
|
|
|
static unsigned hash(StringView s) { return s.hash(); }
|
2020-10-08 20:48:56 +03:00
|
|
|
};
|
|
|
|
|
2023-03-14 14:40:53 +03:00
|
|
|
struct CaseInsensitiveASCIIStringViewTraits : public Traits<StringView> {
|
2022-01-10 19:47:23 +03: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 10:48:54 +03:00
|
|
|
static bool equals(StringView const& a, StringView const& b) { return a.equals_ignoring_ascii_case(b); }
|
2022-01-10 19:47:23 +03:00
|
|
|
};
|
|
|
|
|
2019-04-16 03:39:16 +03:00
|
|
|
}
|
|
|
|
|
2023-06-19 18:44:39 +03: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 04:31:58 +03: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 12:56:12 +03: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 21:53:10 +03:00
|
|
|
# define AK_STRING_VIEW_LITERAL_CONSTEVAL constexpr
|
|
|
|
#else
|
|
|
|
# define AK_STRING_VIEW_LITERAL_CONSTEVAL consteval
|
|
|
|
#endif
|
|
|
|
|
2023-08-17 19:27:39 +03:00
|
|
|
[[nodiscard]] ALWAYS_INLINE AK_STRING_VIEW_LITERAL_CONSTEVAL AK::StringView operator""sv(char const* cstring, size_t length)
|
2021-02-24 15:50:00 +03:00
|
|
|
{
|
|
|
|
return AK::StringView(cstring, length);
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-03-14 14:40:53 +03:00
|
|
|
using AK::CaseInsensitiveASCIIStringViewTraits;
|
2019-04-16 03:39:16 +03:00
|
|
|
using AK::StringView;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|