AK: Move data fields from AK::String to a newly created AK::StringBase

This starts separating memory management of string data and string
utilities like `String::formatted`. This would also allow to reuse the
same storage in `DeprecatedString` in the future.
This commit is contained in:
Dan Klishch 2023-10-28 15:17:06 -04:00 committed by Andrew Kaster
parent 6e2f627cb3
commit 4364a28d3d
Notes: sideshowbarker 2024-07-17 02:06:40 +09:00
5 changed files with 142 additions and 101 deletions

View file

@ -122,49 +122,6 @@ void StringData::compute_hash() const
}
String::String(NonnullRefPtr<Detail::StringData const> data)
: m_data(&data.leak_ref())
{
}
String::String(String const& other)
: m_data(other.m_data)
{
if (!is_short_string())
m_data->ref();
}
String::String(String&& other)
: m_data(exchange(other.m_data, nullptr))
{
other.m_short_string.byte_count_and_short_string_flag = SHORT_STRING_FLAG;
}
String& String::operator=(String&& other)
{
if (!is_short_string())
m_data->unref();
m_data = exchange(other.m_data, nullptr);
other.m_short_string.byte_count_and_short_string_flag = SHORT_STRING_FLAG;
return *this;
}
String& String::operator=(String const& other)
{
if (&other != this) {
if (!is_short_string())
m_data->unref();
m_data = other.m_data;
if (!is_short_string())
m_data->ref();
}
return *this;
}
void String::destroy_string()
{
if (!is_short_string())
@ -491,21 +448,6 @@ bool String::ends_with_bytes(StringView bytes, CaseSensitivity case_sensitivity)
return bytes_as_string_view().ends_with(bytes, case_sensitivity);
}
bool String::is_short_string() const
{
return has_short_string_bit(reinterpret_cast<uintptr_t>(m_data));
}
ReadonlyBytes String::ShortString::bytes() const
{
return { storage, byte_count() };
}
size_t String::ShortString::byte_count() const
{
return byte_count_and_short_string_flag >> 1;
}
unsigned Traits<String>::hash(String const& string)
{
return string.hash();