mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:57:44 +00:00
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.
This commit is contained in:
parent
f49a65cb28
commit
1d4f287582
8 changed files with 445 additions and 1 deletions
73
AK/FlyString.h
Normal file
73
AK/FlyString.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Platform.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
class FlyString {
|
||||
public:
|
||||
FlyString();
|
||||
~FlyString();
|
||||
|
||||
static ErrorOr<FlyString> from_utf8(StringView);
|
||||
explicit FlyString(String const&);
|
||||
|
||||
FlyString(FlyString const&);
|
||||
FlyString& operator=(FlyString const&);
|
||||
|
||||
FlyString(FlyString&&);
|
||||
FlyString& operator=(FlyString&&);
|
||||
|
||||
[[nodiscard]] bool is_empty() const;
|
||||
[[nodiscard]] unsigned hash() const;
|
||||
|
||||
explicit operator String() const;
|
||||
String to_string() const;
|
||||
|
||||
[[nodiscard]] Utf8View code_points() const;
|
||||
[[nodiscard]] ReadonlyBytes bytes() const;
|
||||
[[nodiscard]] StringView bytes_as_string_view() const;
|
||||
|
||||
[[nodiscard]] bool operator==(FlyString const& other) const;
|
||||
[[nodiscard]] bool operator==(String const&) const;
|
||||
[[nodiscard]] bool operator==(StringView) const;
|
||||
[[nodiscard]] bool operator==(char const*) const;
|
||||
|
||||
static void did_destroy_fly_string_data(Badge<Detail::StringData>, StringView);
|
||||
[[nodiscard]] uintptr_t data(Badge<String>) const;
|
||||
|
||||
// This is primarily interesting to unit tests.
|
||||
[[nodiscard]] static size_t number_of_fly_strings();
|
||||
|
||||
private:
|
||||
// This will hold either the pointer to the Detail::StringData it represents or the raw bytes of
|
||||
// an inlined short string.
|
||||
uintptr_t m_data { 0 };
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<FlyString> : public GenericTraits<FlyString> {
|
||||
static unsigned hash(FlyString const&);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Formatter<FlyString> : Formatter<StringView> {
|
||||
ErrorOr<void> format(FormatBuilder&, FlyString const&);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::FlyString;
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue