2023-10-28 19:17:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-10-28 22:58:29 +00:00
|
|
|
#include <AK/Badge.h>
|
2023-10-28 23:07:10 +00:00
|
|
|
#include <AK/FlyString.h>
|
2023-10-28 19:17:06 +00:00
|
|
|
#include <AK/StringBase.h>
|
2024-03-23 11:18:54 +00:00
|
|
|
#include <AK/StringData.h>
|
2023-10-28 19:17:06 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:37:10 +00:00
|
|
|
ReadonlyBytes StringBase::bytes() const
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2023-10-28 19:37:10 +00:00
|
|
|
if (is_short_string())
|
|
|
|
return m_short_string.bytes();
|
|
|
|
return m_data->bytes();
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:47:59 +00:00
|
|
|
u32 StringBase::hash() const
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2023-10-28 19:47:59 +00:00
|
|
|
if (is_short_string()) {
|
|
|
|
auto bytes = this->bytes();
|
|
|
|
return string_hash(reinterpret_cast<char const*>(bytes.data()), bytes.size());
|
|
|
|
}
|
|
|
|
return m_data->hash();
|
|
|
|
}
|
|
|
|
|
2023-10-28 21:50:24 +00:00
|
|
|
size_t StringBase::byte_count() const
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2023-10-28 21:50:24 +00:00
|
|
|
if (is_short_string())
|
|
|
|
return m_short_string.byte_count_and_short_string_flag >> 1;
|
|
|
|
return m_data->byte_count();
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:37:10 +00:00
|
|
|
bool StringBase::operator==(StringBase const& other) const
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2023-10-28 19:37:10 +00:00
|
|
|
if (is_short_string())
|
|
|
|
return m_data == other.m_data;
|
2023-10-28 22:58:29 +00:00
|
|
|
if (other.is_short_string())
|
|
|
|
return false;
|
|
|
|
if (m_data->is_fly_string() && other.m_data->is_fly_string())
|
|
|
|
return m_data == other.m_data;
|
2023-10-28 19:37:10 +00:00
|
|
|
return bytes() == other.bytes();
|
|
|
|
}
|
|
|
|
|
2024-07-19 19:38:41 +00:00
|
|
|
void StringBase::replace_with_string_builder(StringBuilder& builder)
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2024-07-19 19:38:41 +00:00
|
|
|
if (builder.length() <= MAX_SHORT_STRING_BYTE_COUNT) {
|
|
|
|
return replace_with_new_short_string(builder.length(), [&](Bytes buffer) {
|
|
|
|
builder.string_view().bytes().copy_to(buffer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy_string();
|
|
|
|
|
|
|
|
m_data = &StringData::create_from_string_builder(builder).leak_ref();
|
|
|
|
}
|
|
|
|
|
2023-10-28 20:43:56 +00:00
|
|
|
ErrorOr<Bytes> StringBase::replace_with_uninitialized_buffer(size_t byte_count)
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2023-10-28 20:43:56 +00:00
|
|
|
if (byte_count <= MAX_SHORT_STRING_BYTE_COUNT)
|
|
|
|
return replace_with_uninitialized_short_string(byte_count);
|
|
|
|
|
|
|
|
u8* buffer = nullptr;
|
|
|
|
destroy_string();
|
|
|
|
m_data = &TRY(StringData::create_uninitialized(byte_count, buffer)).leak_ref();
|
|
|
|
return Bytes { buffer, byte_count };
|
|
|
|
}
|
|
|
|
|
2023-10-28 21:50:24 +00:00
|
|
|
ErrorOr<StringBase> StringBase::substring_from_byte_offset_with_shared_superstring(size_t start, size_t length) const
|
|
|
|
{
|
2024-10-28 21:53:16 +00:00
|
|
|
ASSERT(!is_invalid());
|
2023-10-28 21:50:24 +00:00
|
|
|
VERIFY(start + length <= byte_count());
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return StringBase {};
|
|
|
|
if (length <= MAX_SHORT_STRING_BYTE_COUNT) {
|
|
|
|
StringBase result;
|
|
|
|
bytes().slice(start, length).copy_to(result.replace_with_uninitialized_short_string(length));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return StringBase { TRY(Detail::StringData::create_substring(*m_data, start, length)) };
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:42:33 +00:00
|
|
|
void StringBase::destroy_string()
|
|
|
|
{
|
|
|
|
if (!is_short_string())
|
|
|
|
m_data->unref();
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:17:06 +00:00
|
|
|
}
|