2023-10-28 19:17:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-03-13 10:42:36 +00:00
|
|
|
#include <AK/Badge.h>
|
2023-10-28 19:17:06 +00:00
|
|
|
#include <AK/Endian.h>
|
|
|
|
#include <AK/Forward.h>
|
|
|
|
|
|
|
|
namespace AK::Detail {
|
|
|
|
|
|
|
|
class StringData;
|
|
|
|
|
2024-07-04 14:00:44 +00:00
|
|
|
static constexpr size_t MAX_SHORT_STRING_BYTE_COUNT = sizeof(StringData*) - sizeof(u8);
|
2023-10-28 19:17:06 +00:00
|
|
|
|
|
|
|
struct ShortString {
|
|
|
|
ReadonlyBytes bytes() const;
|
|
|
|
size_t byte_count() const;
|
|
|
|
|
|
|
|
// NOTE: This is the byte count shifted left 1 step and or'ed with a 1 (the SHORT_STRING_FLAG)
|
2024-07-04 14:00:44 +00:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2023-10-28 19:17:06 +00:00
|
|
|
u8 byte_count_and_short_string_flag { 0 };
|
|
|
|
u8 storage[MAX_SHORT_STRING_BYTE_COUNT] = { 0 };
|
2024-07-04 14:00:44 +00:00
|
|
|
#else
|
|
|
|
u8 storage[MAX_SHORT_STRING_BYTE_COUNT] = { 0 };
|
|
|
|
u8 byte_count_and_short_string_flag { 0 };
|
|
|
|
#endif
|
2023-10-28 19:17:06 +00:00
|
|
|
};
|
|
|
|
|
2024-07-04 14:00:44 +00:00
|
|
|
static_assert(sizeof(ShortString) == sizeof(StringData*));
|
2023-10-28 19:17:06 +00:00
|
|
|
|
|
|
|
class StringBase {
|
|
|
|
public:
|
2023-10-28 19:47:59 +00:00
|
|
|
// Creates an empty (zero-length) String.
|
|
|
|
constexpr StringBase()
|
2024-07-04 14:00:44 +00:00
|
|
|
: StringBase(ShortString { .byte_count_and_short_string_flag = SHORT_STRING_FLAG })
|
2023-10-28 19:47:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:17:06 +00:00
|
|
|
StringBase(StringBase const&);
|
|
|
|
|
|
|
|
constexpr StringBase(StringBase&& other)
|
|
|
|
: m_short_string(other.m_short_string)
|
|
|
|
{
|
|
|
|
other.m_short_string = ShortString {};
|
|
|
|
other.m_short_string.byte_count_and_short_string_flag = SHORT_STRING_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBase& operator=(StringBase&&);
|
|
|
|
StringBase& operator=(StringBase const&);
|
|
|
|
|
2023-10-28 19:42:33 +00:00
|
|
|
constexpr ~StringBase()
|
|
|
|
{
|
|
|
|
if (!is_constant_evaluated())
|
|
|
|
destroy_string();
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:17:06 +00:00
|
|
|
// NOTE: This is primarily interesting to unit tests.
|
2023-10-28 20:43:56 +00:00
|
|
|
[[nodiscard]] constexpr bool is_short_string() const
|
|
|
|
{
|
|
|
|
return (m_short_string.byte_count_and_short_string_flag & SHORT_STRING_FLAG) != 0;
|
|
|
|
}
|
2023-10-28 19:17:06 +00:00
|
|
|
|
2023-10-28 19:37:10 +00:00
|
|
|
// Returns the underlying UTF-8 encoded bytes.
|
|
|
|
// NOTE: There is no guarantee about null-termination.
|
|
|
|
[[nodiscard]] ReadonlyBytes bytes() const;
|
2023-10-28 19:47:59 +00:00
|
|
|
[[nodiscard]] u32 hash() const;
|
2023-10-28 21:50:24 +00:00
|
|
|
[[nodiscard]] size_t byte_count() const;
|
2023-10-28 19:37:10 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool operator==(StringBase const&) const;
|
|
|
|
|
2024-03-13 10:42:36 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE FlatPtr raw(Badge<FlyString>) const { return bit_cast<FlatPtr>(m_data); }
|
|
|
|
|
2023-10-28 19:17:06 +00:00
|
|
|
protected:
|
2024-10-28 21:53:16 +00:00
|
|
|
bool is_invalid() const { return m_invalid_tag == UINTPTR_MAX; }
|
|
|
|
|
2023-10-28 20:43:56 +00:00
|
|
|
template<typename Func>
|
|
|
|
ErrorOr<void> replace_with_new_string(size_t byte_count, Func&& callback)
|
|
|
|
{
|
|
|
|
Bytes buffer = TRY(replace_with_uninitialized_buffer(byte_count));
|
|
|
|
if (byte_count != 0)
|
|
|
|
TRY(callback(buffer));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Func>
|
|
|
|
constexpr void replace_with_new_short_string(size_t byte_count, Func&& callback)
|
|
|
|
{
|
|
|
|
Bytes buffer = replace_with_uninitialized_short_string(byte_count);
|
|
|
|
if (byte_count != 0)
|
|
|
|
callback(buffer);
|
|
|
|
}
|
|
|
|
|
2024-07-19 19:38:41 +00:00
|
|
|
void replace_with_string_builder(StringBuilder&);
|
|
|
|
|
2023-10-28 21:50:24 +00:00
|
|
|
// This is not a trivial operation with storage, so it does not belong here. Unfortunately, it
|
|
|
|
// is impossible to implement it without access to StringData.
|
|
|
|
ErrorOr<StringBase> substring_from_byte_offset_with_shared_superstring(size_t start, size_t byte_count) const;
|
|
|
|
|
2023-10-28 19:42:33 +00:00
|
|
|
private:
|
2024-03-23 13:54:23 +00:00
|
|
|
friend class ::AK::String;
|
|
|
|
friend class ::AK::FlyString;
|
|
|
|
|
2023-10-28 23:03:31 +00:00
|
|
|
// NOTE: If the least significant bit of the pointer is set, this is a short string.
|
|
|
|
static constexpr uintptr_t SHORT_STRING_FLAG = 1;
|
|
|
|
|
|
|
|
explicit StringBase(NonnullRefPtr<Detail::StringData const>);
|
|
|
|
|
2024-10-28 21:53:16 +00:00
|
|
|
explicit constexpr StringBase(nullptr_t)
|
|
|
|
: m_invalid_tag(UINTPTR_MAX)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-28 23:03:31 +00:00
|
|
|
explicit constexpr StringBase(ShortString short_string)
|
|
|
|
: m_short_string(short_string)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-28 20:43:56 +00:00
|
|
|
ErrorOr<Bytes> replace_with_uninitialized_buffer(size_t byte_count);
|
|
|
|
|
|
|
|
constexpr Bytes replace_with_uninitialized_short_string(size_t byte_count)
|
|
|
|
{
|
|
|
|
VERIFY(is_short_string());
|
|
|
|
VERIFY(byte_count <= MAX_SHORT_STRING_BYTE_COUNT);
|
|
|
|
|
|
|
|
m_short_string = ShortString {};
|
|
|
|
m_short_string.byte_count_and_short_string_flag = (byte_count << 1) | SHORT_STRING_FLAG;
|
|
|
|
return { m_short_string.storage, byte_count };
|
|
|
|
}
|
|
|
|
|
2023-10-28 19:42:33 +00:00
|
|
|
void destroy_string();
|
2023-10-28 23:03:31 +00:00
|
|
|
|
|
|
|
union {
|
|
|
|
ShortString m_short_string;
|
|
|
|
Detail::StringData const* m_data { nullptr };
|
2024-10-28 21:53:16 +00:00
|
|
|
uintptr_t m_invalid_tag;
|
2023-10-28 23:03:31 +00:00
|
|
|
};
|
2023-10-28 19:17:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|