1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

AK: Store data in FlyString as StringBase

Unfortunately, it is not clear to me how to split this commit into
several atomic ones.
This commit is contained in:
Dan Klishch 2023-10-28 18:58:29 -04:00 committed by Andrew Kaster
parent e7700e16ee
commit fa52f68142
7 changed files with 42 additions and 145 deletions

View file

@ -41,12 +41,19 @@ StringData::StringData(StringData const& superstring, size_t start, size_t byte_
StringData::~StringData()
{
if (m_is_fly_string)
FlyString::did_destroy_fly_string_data({}, bytes_as_string_view());
if (m_substring)
substring_data().superstring->unref();
}
void StringData::unref() const
{
if (m_is_fly_string && m_ref_count == 2) {
m_is_fly_string = false; // Otherwise unref from did_destory_fly_string_data will cause infinite recursion.
FlyString::did_destroy_fly_string_data({}, bytes_as_string_view());
}
RefCounted::unref();
}
constexpr size_t allocation_size_for_string_data(size_t length)
{
return sizeof(StringData) + (sizeof(char) * length);
@ -227,9 +234,7 @@ Optional<size_t> String::find_byte_offset(StringView substring, size_t from_byte
bool String::operator==(FlyString const& other) const
{
if (reinterpret_cast<uintptr_t>(m_data) == other.data({}))
return true;
return bytes_as_string_view() == other.bytes_as_string_view();
return static_cast<StringBase const&>(*this) == other.data({});
}
bool String::operator==(StringView other) const
@ -367,67 +372,6 @@ unsigned Traits<String>::hash(String const& string)
return string.hash();
}
String String::fly_string_data_to_string(Badge<FlyString>, uintptr_t const& data)
{
if (has_short_string_bit(data))
return String { *reinterpret_cast<ShortString const*>(&data) };
auto const* string_data = reinterpret_cast<Detail::StringData const*>(data);
return String { NonnullRefPtr<Detail::StringData const>(*string_data) };
}
StringView String::fly_string_data_to_string_view(Badge<FlyString>, uintptr_t const& data)
{
if (has_short_string_bit(data)) {
auto const* short_string = reinterpret_cast<ShortString const*>(&data);
return short_string->bytes();
}
auto const* string_data = reinterpret_cast<Detail::StringData const*>(data);
return string_data->bytes_as_string_view();
}
u32 String::fly_string_data_to_hash(Badge<FlyString>, uintptr_t const& data)
{
if (has_short_string_bit(data)) {
auto const* short_string = reinterpret_cast<ShortString const*>(&data);
auto bytes = short_string->bytes();
return string_hash(reinterpret_cast<char const*>(bytes.data()), bytes.size());
}
auto const* string_data = reinterpret_cast<Detail::StringData const*>(data);
return string_data->hash();
}
uintptr_t String::to_fly_string_data(Badge<FlyString>) const
{
return reinterpret_cast<uintptr_t>(m_data);
}
void String::ref_fly_string_data(Badge<FlyString>, uintptr_t data)
{
if (has_short_string_bit(data))
return;
auto const* string_data = reinterpret_cast<Detail::StringData const*>(data);
string_data->ref();
}
void String::unref_fly_string_data(Badge<FlyString>, uintptr_t data)
{
if (has_short_string_bit(data))
return;
auto const* string_data = reinterpret_cast<Detail::StringData const*>(data);
string_data->unref();
}
void String::did_create_fly_string(Badge<FlyString>) const
{
VERIFY(!is_short_string());
m_data->set_fly_string(true);
}
ByteString String::to_byte_string() const
{
return ByteString(bytes_as_string_view());