summaryrefslogtreecommitdiff
path: root/AK/String.h
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-01-11 08:26:49 -0500
committerAndreas Kling <kling@serenityos.org>2023-01-12 11:23:58 +0100
commit1d4f287582f43acfd76661d20bc2915455809f41 (patch)
treedf4b1dc79816ee1e1aa92199b17f65d78bb71e88 /AK/String.h
parentf49a65cb28891b8053d7c36d8d582ea258977c8d (diff)
downloadserenity-1d4f287582f43acfd76661d20bc2915455809f41.zip
AK: Implement FlyString for the new String class
This implements a FlyString that will de-duplicate String instances. The FlyString will store the raw encoded data of the String instance: If the String is a short string, FlyString holds the String::ShortString bytes; otherwise FlyString holds a pointer to the Detail::StringData. FlyString itself does not know about String's storage or how to refcount its Detail::StringData. It defers to String to implement these details.
Diffstat (limited to 'AK/String.h')
-rw-r--r--AK/String.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/AK/String.h b/AK/String.h
index 7a8eecce15..b0d4f3e2a1 100644
--- a/AK/String.h
+++ b/AK/String.h
@@ -73,6 +73,9 @@ public:
[[nodiscard]] bool operator==(String const&) const;
[[nodiscard]] bool operator!=(String const& other) const { return !(*this == other); }
+ [[nodiscard]] bool operator==(FlyString const&) const;
+ [[nodiscard]] bool operator!=(FlyString const& other) const { return !(*this == other); }
+
[[nodiscard]] bool operator==(StringView) const;
[[nodiscard]] bool operator!=(StringView other) const { return !(*this == other); }
@@ -102,6 +105,14 @@ public:
// NOTE: This is primarily interesting to unit tests.
[[nodiscard]] bool is_short_string() const;
+ [[nodiscard]] static String fly_string_data_to_string(Badge<FlyString>, uintptr_t const&);
+ [[nodiscard]] static StringView fly_string_data_to_string_view(Badge<FlyString>, uintptr_t const&);
+ [[nodiscard]] uintptr_t to_fly_string_data(Badge<FlyString>) const;
+
+ static void ref_fly_string_data(Badge<FlyString>, uintptr_t);
+ static void unref_fly_string_data(Badge<FlyString>, uintptr_t);
+ void did_create_fly_string(Badge<FlyString>) const;
+
// FIXME: Remove these once all code has been ported to String
[[nodiscard]] DeprecatedString to_deprecated_string() const;
static ErrorOr<String> from_deprecated_string(DeprecatedString const&);
@@ -110,6 +121,11 @@ private:
// NOTE: If the least significant bit of the pointer is set, this is a short string.
static constexpr uintptr_t SHORT_STRING_FLAG = 1;
+ static constexpr bool has_short_string_bit(uintptr_t data)
+ {
+ return (data & SHORT_STRING_FLAG) != 0;
+ }
+
struct ShortString {
ReadonlyBytes bytes() const;
size_t byte_count() const;