mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-12 22:22:55 +00:00
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:
parent
6e2f627cb3
commit
4364a28d3d
Notes:
sideshowbarker
2024-07-17 02:06:40 +09:00
Author: https://github.com/DanShaders
Commit: 4364a28d3d
Pull-request: https://github.com/SerenityOS/serenity/pull/21661
Reviewed-by: https://github.com/ADKaster
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/kleinesfilmroellchen
Reviewed-by: https://github.com/trflynn89
5 changed files with 142 additions and 101 deletions
62
AK/StringBase.cpp
Normal file
62
AK/StringBase.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/StringBase.h>
|
||||
#include <AK/StringInternals.h>
|
||||
|
||||
namespace AK::Detail {
|
||||
|
||||
ReadonlyBytes ShortString::bytes() const
|
||||
{
|
||||
return { storage, byte_count() };
|
||||
}
|
||||
|
||||
size_t ShortString::byte_count() const
|
||||
{
|
||||
return byte_count_and_short_string_flag >> 1;
|
||||
}
|
||||
|
||||
StringBase::StringBase(NonnullRefPtr<Detail::StringData const> data)
|
||||
: m_data(&data.leak_ref())
|
||||
{
|
||||
}
|
||||
|
||||
StringBase::StringBase(StringBase const& other)
|
||||
: m_data(other.m_data)
|
||||
{
|
||||
if (!is_short_string())
|
||||
m_data->ref();
|
||||
}
|
||||
|
||||
StringBase& StringBase::operator=(StringBase&& 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;
|
||||
}
|
||||
|
||||
StringBase& StringBase::operator=(StringBase 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;
|
||||
}
|
||||
|
||||
bool StringBase::is_short_string() const
|
||||
{
|
||||
return has_short_string_bit(reinterpret_cast<uintptr_t>(m_data));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue