2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-09-23 11:21:18 +00:00
|
|
|
#include <AK/Format.h>
|
2020-02-14 20:41:10 +00:00
|
|
|
#include <AK/Forward.h>
|
2019-06-21 16:45:35 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2021-01-15 19:13:14 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/StringImpl.h>
|
2020-02-26 07:25:24 +00:00
|
|
|
#include <AK/StringUtils.h>
|
2019-04-15 12:56:37 +00:00
|
|
|
#include <AK/Traits.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
// ByteString is a convenience wrapper around StringImpl, suitable for passing
|
2019-06-02 06:37:01 +00:00
|
|
|
// around as a value type. It's basically the same as passing around a
|
2023-02-19 21:59:26 +00:00
|
|
|
// RefPtr<StringImpl const>, with a bit of syntactic sugar.
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// Note that StringImpl is an immutable object that cannot shrink or grow.
|
|
|
|
// Its allocation size is snugly tailored to the specific string it contains.
|
2023-12-16 14:19:34 +00:00
|
|
|
// Copying a ByteString is very efficient, since the internal StringImpl is
|
2019-06-21 16:40:24 +00:00
|
|
|
// retainable and so copying only requires modifying the ref count.
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
2023-12-16 14:19:34 +00:00
|
|
|
// There are three main ways to construct a new ByteString:
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
2023-12-16 14:19:34 +00:00
|
|
|
// s = ByteString("some literal");
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
2023-12-16 14:19:34 +00:00
|
|
|
// s = ByteString::formatted("{} little piggies", m_piggies);
|
2019-06-02 06:37:01 +00:00
|
|
|
//
|
|
|
|
// StringBuilder builder;
|
|
|
|
// builder.append("abc");
|
|
|
|
// builder.append("123");
|
2023-12-16 14:19:34 +00:00
|
|
|
// s = builder.to_byte_string();
|
2019-06-02 06:37:01 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
class ByteString {
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2023-12-16 14:19:34 +00:00
|
|
|
~ByteString() = default;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString()
|
2023-10-10 11:30:58 +00:00
|
|
|
: m_impl(StringImpl::the_empty_stringimpl())
|
|
|
|
{
|
|
|
|
}
|
2021-06-02 23:35:01 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(StringView view)
|
2023-10-10 11:30:58 +00:00
|
|
|
: m_impl(*StringImpl::create(view.characters_without_null_termination(), view.length()))
|
2021-06-02 23:35:01 +00:00
|
|
|
{
|
|
|
|
}
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(ByteString const& other)
|
2023-02-19 21:59:26 +00:00
|
|
|
: m_impl(other.m_impl)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(ByteString&& other)
|
2018-10-17 08:55:43 +00:00
|
|
|
: m_impl(move(other.m_impl))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2023-10-10 11:30:58 +00:00
|
|
|
other.m_impl = StringImpl::the_empty_stringimpl();
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(char const* cstring, ShouldChomp shouldChomp = NoChomp)
|
2023-10-10 11:30:58 +00:00
|
|
|
: m_impl(*StringImpl::create(cstring, shouldChomp))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(char const* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
|
2023-10-10 11:30:58 +00:00
|
|
|
: m_impl(*StringImpl::create(cstring, length, shouldChomp))
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
explicit ByteString(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp)
|
2023-10-10 11:30:58 +00:00
|
|
|
: m_impl(*StringImpl::create(bytes, shouldChomp))
|
2020-08-05 08:37:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(StringImpl const& impl)
|
2023-02-19 21:59:26 +00:00
|
|
|
: m_impl(impl)
|
2019-04-12 12:43:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(NonnullRefPtr<StringImpl const>&& impl)
|
2023-10-10 11:30:58 +00:00
|
|
|
: m_impl(*move(impl))
|
2019-02-25 15:04:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(DeprecatedFlyString const&);
|
2020-03-22 18:07:02 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
static ErrorOr<ByteString> from_utf8(ReadonlyBytes);
|
|
|
|
static ErrorOr<ByteString> from_utf8(StringView string) { return from_utf8(string.bytes()); }
|
2023-12-18 11:41:25 +00:00
|
|
|
static ByteString must_from_utf8(StringView string) { return MUST(from_utf8(string)); }
|
2023-12-21 12:48:03 +00:00
|
|
|
static ByteString from_utf8_without_validation(StringView string) { return ByteString { string }; }
|
2023-01-28 20:08:14 +00:00
|
|
|
|
2024-01-22 18:26:55 +00:00
|
|
|
template<
|
|
|
|
typename F,
|
|
|
|
typename PossiblyErrorOr = decltype(declval<F>()(declval<Bytes>())),
|
|
|
|
bool is_error_or = IsSpecializationOf<PossiblyErrorOr, ErrorOr>,
|
|
|
|
typename ReturnType = Conditional<is_error_or, ErrorOr<ByteString>, ByteString>>
|
|
|
|
static ReturnType create_and_overwrite(size_t length, F&& fill_function)
|
|
|
|
{
|
|
|
|
char* buffer;
|
|
|
|
auto impl = StringImpl::create_uninitialized(length, buffer);
|
|
|
|
|
|
|
|
if constexpr (is_error_or)
|
|
|
|
TRY(fill_function(Bytes { buffer, length }));
|
|
|
|
else
|
|
|
|
fill_function(Bytes { buffer, length });
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString repeated(char, size_t count);
|
|
|
|
[[nodiscard]] static ByteString repeated(StringView, size_t count);
|
2021-01-15 19:13:14 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString bijective_base_from(size_t value, unsigned base = 26, StringView map = {});
|
|
|
|
[[nodiscard]] static ByteString roman_number_from(size_t value);
|
2021-04-30 21:31:53 +00:00
|
|
|
|
2021-01-15 19:13:14 +00:00
|
|
|
template<class SeparatorType, class CollectionType>
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
|
2021-01-15 19:13:14 +00:00
|
|
|
{
|
|
|
|
StringBuilder builder;
|
2022-02-22 21:31:05 +00:00
|
|
|
builder.join(separator, collection, fmtstr);
|
2023-12-16 14:19:34 +00:00
|
|
|
return builder.to_byte_string();
|
2021-01-15 19:13:14 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2019-04-25 20:56:09 +00:00
|
|
|
|
2023-12-22 22:55:22 +00:00
|
|
|
template<Arithmetic T>
|
|
|
|
Optional<T> to_number(TrimWhitespace trim_whitespace = TrimWhitespace::Yes) const
|
|
|
|
{
|
|
|
|
return view().to_number<T>(trim_whitespace);
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString to_lowercase() const;
|
|
|
|
[[nodiscard]] ByteString to_uppercase() const;
|
|
|
|
[[nodiscard]] ByteString to_snakecase() const;
|
|
|
|
[[nodiscard]] ByteString to_titlecase() const;
|
|
|
|
[[nodiscard]] ByteString invert_case() const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
2021-01-02 23:26:02 +00:00
|
|
|
|
2024-11-14 19:28:30 +00:00
|
|
|
[[nodiscard]] Utf8CodePointIterator code_points() const&;
|
|
|
|
[[nodiscard]] Utf8CodePointIterator code_points() const&& = delete;
|
2023-01-27 15:26:57 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString trim(StringView characters, TrimMode mode = TrimMode::Both) const
|
2021-05-25 07:42:01 +00:00
|
|
|
{
|
2022-02-18 23:36:30 +00:00
|
|
|
auto trimmed_view = StringUtils::trim(view(), characters, mode);
|
|
|
|
if (view() == trimmed_view)
|
|
|
|
return *this;
|
|
|
|
return trimmed_view;
|
2021-05-25 07:42:01 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString trim_whitespace(TrimMode mode = TrimMode::Both) const
|
2020-09-20 13:35:04 +00:00
|
|
|
{
|
2022-02-18 23:36:30 +00:00
|
|
|
auto trimmed_view = StringUtils::trim_whitespace(view(), mode);
|
|
|
|
if (view() == trimmed_view)
|
|
|
|
return *this;
|
|
|
|
return trimmed_view;
|
2020-09-20 13:35:04 +00:00
|
|
|
}
|
2020-05-11 00:43:33 +00:00
|
|
|
|
2023-03-10 07:48:54 +00:00
|
|
|
[[nodiscard]] bool equals_ignoring_ascii_case(StringView) const;
|
2019-12-18 11:43:53 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2021-08-11 18:49:32 +00:00
|
|
|
[[nodiscard]] bool contains(char, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
2019-10-28 17:47:48 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] Vector<ByteString> split_limit(char separator, size_t limit, SplitBehavior = SplitBehavior::Nothing) const;
|
|
|
|
[[nodiscard]] Vector<ByteString> split(char separator, SplitBehavior = SplitBehavior::Nothing) const;
|
2024-04-04 02:01:43 +00:00
|
|
|
|
|
|
|
[[nodiscard]] Vector<StringView> split_view(char separator, SplitBehavior = SplitBehavior::Nothing) const&;
|
|
|
|
[[nodiscard]] Vector<StringView> split_view(char separator, SplitBehavior = SplitBehavior::Nothing) const&& = delete;
|
|
|
|
|
|
|
|
[[nodiscard]] Vector<StringView> split_view(Function<bool(char)> separator, SplitBehavior = SplitBehavior::Nothing) const&;
|
|
|
|
[[nodiscard]] Vector<StringView> split_view(Function<bool(char)> separator, SplitBehavior = SplitBehavior::Nothing) const&& = delete;
|
2021-01-12 19:58:45 +00:00
|
|
|
|
2021-07-01 15:34:25 +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 15:34:25 +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); }
|
2021-11-10 10:05:21 +00:00
|
|
|
Vector<size_t> find_all(StringView needle) const;
|
2021-07-01 16:12:21 +00:00
|
|
|
using SearchDirection = StringUtils::SearchDirection;
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] Optional<size_t> find_any_of(StringView needles, SearchDirection direction) const { return StringUtils::find_any_of(*this, needles, direction); }
|
2021-01-12 19:58:45 +00:00
|
|
|
|
2024-04-04 02:01:43 +00:00
|
|
|
[[nodiscard]] StringView find_last_split_view(char separator) const& { return view().find_last_split_view(separator); }
|
|
|
|
[[nodiscard]] StringView find_last_split_view(char separator) const&& = delete;
|
2022-06-18 16:35:36 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString substring(size_t start, size_t length) const;
|
|
|
|
[[nodiscard]] ByteString substring(size_t start) const;
|
2024-04-04 02:01:43 +00:00
|
|
|
|
|
|
|
[[nodiscard]] StringView substring_view(size_t start, size_t length) const&;
|
|
|
|
[[nodiscard]] StringView substring_view(size_t start, size_t length) const&& = delete;
|
|
|
|
|
|
|
|
[[nodiscard]] StringView substring_view(size_t start) const&;
|
|
|
|
[[nodiscard]] StringView substring_view(size_t start) const&& = delete;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return length() == 0; }
|
2023-10-10 11:30:58 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE size_t length() const { return m_impl->length(); }
|
|
|
|
// Includes NUL-terminator.
|
|
|
|
[[nodiscard]] ALWAYS_INLINE char const* characters() const { return m_impl->characters(); }
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2020-08-25 14:23:18 +00:00
|
|
|
[[nodiscard]] bool copy_characters_to_buffer(char* buffer, size_t buffer_size) const;
|
|
|
|
|
2024-04-04 02:01:43 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const& { return m_impl->bytes(); }
|
|
|
|
[[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const&& = delete;
|
2020-07-27 12:15:37 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE char const& operator[](size_t i) const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
return (*m_impl)[i];
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2022-12-09 16:35:00 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE u8 byte_at(size_t i) const
|
|
|
|
{
|
|
|
|
return bit_cast<u8>((*m_impl)[i]);
|
|
|
|
}
|
|
|
|
|
2024-01-03 21:43:01 +00:00
|
|
|
using ConstIterator = SimpleIterator<ByteString const, char const>;
|
2020-09-06 19:14:08 +00:00
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] constexpr ConstIterator begin() const { return ConstIterator::begin(*this); }
|
|
|
|
[[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
|
2020-03-10 08:13:29 +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-21 19:35:08 +00:00
|
|
|
[[nodiscard]] bool starts_with(char) const;
|
|
|
|
[[nodiscard]] bool ends_with(char) const;
|
2019-03-22 11:43:29 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
bool operator==(ByteString const&) const;
|
2019-07-11 10:58:27 +00:00
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool operator==(StringView) const;
|
2019-08-24 20:28:42 +00:00
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
bool operator==(DeprecatedFlyString const&) const;
|
2020-03-28 08:11:00 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
bool operator<(ByteString const&) const;
|
|
|
|
bool operator>=(ByteString const& other) const { return !(*this < other); }
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator>=(char const* other) const { return !(*this < other); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
bool operator>(ByteString const&) const;
|
|
|
|
bool operator<=(ByteString const& other) const { return !(*this > other); }
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator<=(char const* other) const { return !(*this > other); }
|
2019-07-11 10:58:27 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool operator==(char const* cstring) const;
|
2019-06-08 16:30:40 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString isolated_copy() const;
|
2018-10-26 07:54:29 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString empty()
|
2021-06-02 23:35:01 +00:00
|
|
|
{
|
|
|
|
return StringImpl::the_empty_stringimpl();
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2024-01-24 20:15:48 +00:00
|
|
|
[[nodiscard]] NonnullRefPtr<StringImpl const> impl() const { return m_impl; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString& operator=(ByteString&& other)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-10-24 12:28:22 +00:00
|
|
|
if (this != &other)
|
2018-10-17 08:55:43 +00:00
|
|
|
m_impl = move(other.m_impl);
|
2018-10-24 12:28:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString& operator=(ByteString const& other)
|
2018-10-24 12:28:22 +00:00
|
|
|
{
|
|
|
|
if (this != &other)
|
2023-12-16 14:19:34 +00:00
|
|
|
m_impl = const_cast<ByteString&>(other).m_impl;
|
2018-10-10 09:53:07 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-10-10 11:30:58 +00:00
|
|
|
template<OneOf<ReadonlyBytes, Bytes> T>
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString& operator=(T bytes)
|
2020-08-05 08:37:34 +00:00
|
|
|
{
|
2023-10-10 11:30:58 +00:00
|
|
|
m_impl = *StringImpl::create(bytes);
|
2020-08-05 08:37:34 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] u32 hash() const
|
2019-10-18 15:17:47 +00:00
|
|
|
{
|
|
|
|
return m_impl->hash();
|
|
|
|
}
|
|
|
|
|
2021-04-21 19:35:08 +00:00
|
|
|
[[nodiscard]] ByteBuffer to_byte_buffer() const;
|
2019-04-20 12:13:40 +00:00
|
|
|
|
|
|
|
template<typename BufferType>
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString copy(BufferType const& buffer, ShouldChomp should_chomp = NoChomp)
|
2019-04-20 12:13:40 +00:00
|
|
|
{
|
|
|
|
if (buffer.is_empty())
|
|
|
|
return empty();
|
2023-12-16 14:19:34 +00:00
|
|
|
return ByteString(reinterpret_cast<char const*>(buffer.data()), buffer.size(), should_chomp);
|
2019-04-20 12:13:40 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString vformatted(StringView fmtstr, TypeErasedFormatParams&);
|
2020-09-23 11:21:18 +00:00
|
|
|
|
|
|
|
template<typename... Parameters>
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString formatted(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
2020-09-23 11:21:18 +00:00
|
|
|
{
|
2023-01-13 02:19:40 +00:00
|
|
|
VariadicFormatParams<AllowDebugOnlyFormatters::No, Parameters...> variadic_format_parameters { parameters... };
|
2021-09-01 04:44:05 +00:00
|
|
|
return vformatted(fmtstr.view(), variadic_format_parameters);
|
2020-09-23 11:21:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:20:59 +00:00
|
|
|
template<Arithmetic T>
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] static ByteString number(T value)
|
2021-01-17 08:22:04 +00:00
|
|
|
{
|
|
|
|
return formatted("{}", value);
|
|
|
|
}
|
2019-01-30 15:28:51 +00:00
|
|
|
|
2024-04-04 02:01:43 +00:00
|
|
|
[[nodiscard]] StringView view() const& { return { characters(), length() }; }
|
|
|
|
[[nodiscard]] StringView view() const&& = delete;
|
2019-04-15 12:56:37 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString replace(StringView needle, StringView replacement, ReplaceMode replace_mode = ReplaceMode::All) const { return StringUtils::replace(*this, needle, replacement, replace_mode); }
|
2021-11-10 23:55:02 +00:00
|
|
|
[[nodiscard]] size_t count(StringView needle) const { return StringUtils::count(*this, needle); }
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString reverse() const;
|
2020-04-01 19:27:39 +00:00
|
|
|
|
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-05-25 10:36:41 +00:00
|
|
|
{
|
2021-06-04 09:46:29 +00:00
|
|
|
return (... || this->operator==(forward<Ts>(strings)));
|
2020-05-25 10:36:41 +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
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2023-10-10 11:30:58 +00:00
|
|
|
NonnullRefPtr<StringImpl const> m_impl;
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2023-12-16 14:19:34 +00:00
|
|
|
struct Traits<ByteString> : public DefaultTraits<ByteString> {
|
2024-01-24 20:15:48 +00:00
|
|
|
static unsigned hash(ByteString const& s) { return s.impl()->hash(); }
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2023-03-10 07:48:54 +00:00
|
|
|
// FIXME: Rename this to indicate that it's about ASCII-only case insensitivity.
|
2023-12-16 14:19:34 +00:00
|
|
|
struct CaseInsensitiveStringTraits : public Traits<ByteString> {
|
2024-01-24 20:15:48 +00:00
|
|
|
static unsigned hash(ByteString const& s) { return s.impl()->case_insensitive_hash(); }
|
2023-12-16 14:19:34 +00:00
|
|
|
static bool equals(ByteString const& a, ByteString const& b) { return a.equals_ignoring_ascii_case(b); }
|
2019-07-13 09:00:29 +00:00
|
|
|
};
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString escape_html_entities(StringView html);
|
2020-02-13 07:46:00 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-09-13 12:37:25 +00:00
|
|
|
using AK::CaseInsensitiveStringTraits;
|
2020-02-13 07:46:00 +00:00
|
|
|
using AK::escape_html_entities;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|