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