2023-01-11 13:26:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/Format.h>
|
2024-10-28 21:53:16 +00:00
|
|
|
#include <AK/Optional.h>
|
2023-01-11 13:26:49 +00:00
|
|
|
#include <AK/Platform.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class FlyString {
|
2023-10-28 22:58:29 +00:00
|
|
|
AK_MAKE_DEFAULT_MOVABLE(FlyString);
|
|
|
|
AK_MAKE_DEFAULT_COPYABLE(FlyString);
|
|
|
|
|
2023-01-11 13:26:49 +00:00
|
|
|
public:
|
2023-10-28 22:58:29 +00:00
|
|
|
FlyString() = default;
|
2023-01-11 13:26:49 +00:00
|
|
|
|
|
|
|
static ErrorOr<FlyString> from_utf8(StringView);
|
2024-03-23 10:33:26 +00:00
|
|
|
static FlyString from_utf8_without_validation(ReadonlyBytes);
|
2023-06-11 17:49:02 +00:00
|
|
|
template<typename T>
|
2023-12-16 14:19:34 +00:00
|
|
|
requires(IsOneOf<RemoveCVReference<T>, ByteString, DeprecatedFlyString, FlyString, String>)
|
2023-06-11 17:49:02 +00:00
|
|
|
static ErrorOr<String> from_utf8(T&&) = delete;
|
|
|
|
|
2023-02-18 14:54:46 +00:00
|
|
|
FlyString(String const&);
|
2023-02-14 14:21:55 +00:00
|
|
|
FlyString& operator=(String const&);
|
2023-01-11 13:26:49 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
|
|
|
[[nodiscard]] unsigned hash() const;
|
2023-09-05 17:55:21 +00:00
|
|
|
[[nodiscard]] u32 ascii_case_insensitive_hash() const;
|
2023-01-11 13:26:49 +00:00
|
|
|
|
|
|
|
explicit operator String() const;
|
|
|
|
String to_string() const;
|
|
|
|
|
|
|
|
[[nodiscard]] Utf8View code_points() const;
|
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const;
|
|
|
|
[[nodiscard]] StringView bytes_as_string_view() const;
|
|
|
|
|
2024-03-13 10:42:36 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE bool operator==(FlyString const& other) const { return m_data.raw({}) == other.m_data.raw({}); }
|
2023-01-11 13:26:49 +00:00
|
|
|
[[nodiscard]] bool operator==(String const&) const;
|
|
|
|
[[nodiscard]] bool operator==(StringView) const;
|
|
|
|
[[nodiscard]] bool operator==(char const*) const;
|
|
|
|
|
2023-09-05 18:05:54 +00:00
|
|
|
[[nodiscard]] int operator<=>(FlyString const& other) const;
|
|
|
|
|
2024-03-23 13:54:23 +00:00
|
|
|
static void did_destroy_fly_string_data(Badge<Detail::StringData>, Detail::StringData const&);
|
2023-10-28 22:58:29 +00:00
|
|
|
[[nodiscard]] Detail::StringBase data(Badge<String>) const;
|
2023-01-11 13:26:49 +00:00
|
|
|
|
|
|
|
// This is primarily interesting to unit tests.
|
|
|
|
[[nodiscard]] static size_t number_of_fly_strings();
|
|
|
|
|
2023-03-09 16:45:33 +00:00
|
|
|
// FIXME: Remove these once all code has been ported to FlyString
|
2023-03-04 20:02:58 +00:00
|
|
|
[[nodiscard]] DeprecatedFlyString to_deprecated_fly_string() const;
|
2023-03-09 16:45:33 +00:00
|
|
|
static ErrorOr<FlyString> from_deprecated_fly_string(DeprecatedFlyString const&);
|
2023-06-11 17:49:02 +00:00
|
|
|
template<typename T>
|
|
|
|
requires(IsSame<RemoveCVReference<T>, StringView>)
|
|
|
|
static ErrorOr<String> from_deprecated_fly_string(T&&) = delete;
|
2023-03-04 20:02:58 +00:00
|
|
|
|
2023-03-07 18:53:21 +00:00
|
|
|
// Compare this FlyString against another string with ASCII caseless matching.
|
|
|
|
[[nodiscard]] bool equals_ignoring_ascii_case(FlyString const&) const;
|
2023-09-04 09:49:29 +00:00
|
|
|
[[nodiscard]] bool equals_ignoring_ascii_case(StringView) const;
|
2023-03-07 18:53:21 +00:00
|
|
|
|
2024-10-14 08:51:15 +00:00
|
|
|
[[nodiscard]] FlyString to_ascii_lowercase() const;
|
|
|
|
[[nodiscard]] FlyString to_ascii_uppercase() const;
|
|
|
|
|
2023-11-06 10:59:15 +00:00
|
|
|
[[nodiscard]] bool starts_with_bytes(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
|
|
|
|
[[nodiscard]] bool ends_with_bytes(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
|
|
|
|
2023-03-24 17:25:10 +00:00
|
|
|
template<typename... Ts>
|
2024-10-26 22:37:14 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE constexpr bool is_one_of(Ts&&... strings) const
|
2023-03-24 17:25:10 +00:00
|
|
|
{
|
|
|
|
return (... || this->operator==(forward<Ts>(strings)));
|
|
|
|
}
|
|
|
|
|
2023-01-11 13:26:49 +00:00
|
|
|
private:
|
2024-10-28 21:53:16 +00:00
|
|
|
friend class Optional<FlyString>;
|
|
|
|
|
|
|
|
explicit FlyString(nullptr_t)
|
|
|
|
: m_data(Detail::StringBase(nullptr))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-23 19:03:38 +00:00
|
|
|
explicit FlyString(Detail::StringBase data)
|
|
|
|
: m_data(move(data))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-28 22:58:29 +00:00
|
|
|
Detail::StringBase m_data;
|
2024-10-28 21:53:16 +00:00
|
|
|
|
|
|
|
bool is_invalid() const { return m_data.is_invalid(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class Optional<FlyString> : public OptionalBase<FlyString> {
|
|
|
|
template<typename U>
|
|
|
|
friend class Optional;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using ValueType = FlyString;
|
|
|
|
|
|
|
|
Optional() = default;
|
|
|
|
|
|
|
|
template<SameAs<OptionalNone> V>
|
|
|
|
Optional(V) { }
|
|
|
|
|
|
|
|
Optional(Optional<FlyString> const& other)
|
|
|
|
{
|
|
|
|
if (other.has_value())
|
|
|
|
m_value = other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional(Optional&& other)
|
|
|
|
: m_value(other.m_value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U = FlyString>
|
|
|
|
requires(!IsSame<OptionalNone, RemoveCVReference<U>>)
|
|
|
|
explicit(!IsConvertible<U&&, FlyString>) Optional(U&& value)
|
|
|
|
requires(!IsSame<RemoveCVReference<U>, Optional<FlyString>> && IsConstructible<FlyString, U &&>)
|
|
|
|
: m_value(forward<U>(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<SameAs<OptionalNone> V>
|
|
|
|
Optional& operator=(V)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional& operator=(Optional const& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_value = other.m_value;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional& operator=(Optional&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
m_value = other.m_value;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename O>
|
|
|
|
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
|
|
|
|
{
|
|
|
|
return has_value() == other.has_value() && (!has_value() || value() == other.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename O>
|
|
|
|
ALWAYS_INLINE bool operator==(O const& other) const
|
|
|
|
{
|
|
|
|
return has_value() && value() == other;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_value = FlyString(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool has_value() const
|
|
|
|
{
|
|
|
|
return !m_value.is_invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] FlyString& value() &
|
|
|
|
{
|
|
|
|
VERIFY(has_value());
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] FlyString const& value() const&
|
|
|
|
{
|
|
|
|
VERIFY(has_value());
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] FlyString value() &&
|
|
|
|
{
|
|
|
|
return release_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] FlyString release_value()
|
|
|
|
{
|
|
|
|
VERIFY(has_value());
|
|
|
|
FlyString released_value = m_value;
|
|
|
|
clear();
|
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FlyString m_value = FlyString(nullptr);
|
2023-01-11 13:26:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<FlyString> : public DefaultTraits<FlyString> {
|
2023-01-11 13:26:49 +00:00
|
|
|
static unsigned hash(FlyString const&);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Formatter<FlyString> : Formatter<StringView> {
|
|
|
|
ErrorOr<void> format(FormatBuilder&, FlyString const&);
|
|
|
|
};
|
|
|
|
|
2023-09-05 17:55:21 +00:00
|
|
|
struct ASCIICaseInsensitiveFlyStringTraits : public Traits<String> {
|
|
|
|
static unsigned hash(FlyString const& s) { return s.ascii_case_insensitive_hash(); }
|
2024-04-06 10:24:04 +00:00
|
|
|
static bool equals(FlyString const& a, FlyString const& b) { return a.equals_ignoring_ascii_case(b); }
|
2023-09-05 17:55:21 +00:00
|
|
|
};
|
|
|
|
|
2023-01-11 13:26:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 10:07:35 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE AK::FlyString operator""_fly_string(char const* cstring, size_t length)
|
2023-02-25 15:14:37 +00:00
|
|
|
{
|
2023-08-07 10:07:35 +00:00
|
|
|
return AK::FlyString::from_utf8(AK::StringView(cstring, length)).release_value();
|
2023-02-25 15:14:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 13:26:49 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
|
|
|
using AK::FlyString;
|
|
|
|
#endif
|