mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 17:18:11 +00:00
AK: Forward substring creation with shared superstring to StringBase
This commit is contained in:
parent
5d6cd65e29
commit
e7700e16ee
5 changed files with 34 additions and 5 deletions
|
@ -252,11 +252,7 @@ ErrorOr<String> String::substring_from_byte_offset(size_t start) const
|
||||||
|
|
||||||
ErrorOr<String> String::substring_from_byte_offset_with_shared_superstring(size_t start, size_t byte_count) const
|
ErrorOr<String> String::substring_from_byte_offset_with_shared_superstring(size_t start, size_t byte_count) const
|
||||||
{
|
{
|
||||||
if (!byte_count)
|
return String { TRY(StringBase::substring_from_byte_offset_with_shared_superstring(start, byte_count)) };
|
||||||
return String {};
|
|
||||||
if (byte_count <= MAX_SHORT_STRING_BYTE_COUNT)
|
|
||||||
return String::from_utf8(bytes_as_string_view().substring_view(start, byte_count));
|
|
||||||
return String { TRY(Detail::StringData::create_substring(*m_data, start, byte_count)) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<String> String::substring_from_byte_offset_with_shared_superstring(size_t start) const
|
ErrorOr<String> String::substring_from_byte_offset_with_shared_superstring(size_t start) const
|
||||||
|
|
|
@ -196,6 +196,11 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using ShortString = Detail::ShortString;
|
using ShortString = Detail::ShortString;
|
||||||
|
|
||||||
|
explicit constexpr String(StringBase&& base)
|
||||||
|
: StringBase(move(base))
|
||||||
|
{
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
|
|
@ -70,6 +70,13 @@ u32 StringBase::hash() const
|
||||||
return m_data->hash();
|
return m_data->hash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t StringBase::byte_count() const
|
||||||
|
{
|
||||||
|
if (is_short_string())
|
||||||
|
return m_short_string.byte_count_and_short_string_flag >> 1;
|
||||||
|
return m_data->byte_count();
|
||||||
|
}
|
||||||
|
|
||||||
bool StringBase::operator==(StringBase const& other) const
|
bool StringBase::operator==(StringBase const& other) const
|
||||||
{
|
{
|
||||||
if (is_short_string())
|
if (is_short_string())
|
||||||
|
@ -88,6 +95,20 @@ ErrorOr<Bytes> StringBase::replace_with_uninitialized_buffer(size_t byte_count)
|
||||||
return Bytes { buffer, byte_count };
|
return Bytes { buffer, byte_count };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<StringBase> StringBase::substring_from_byte_offset_with_shared_superstring(size_t start, size_t length) const
|
||||||
|
{
|
||||||
|
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)) };
|
||||||
|
}
|
||||||
|
|
||||||
void StringBase::destroy_string()
|
void StringBase::destroy_string()
|
||||||
{
|
{
|
||||||
if (!is_short_string())
|
if (!is_short_string())
|
||||||
|
|
|
@ -64,6 +64,7 @@ public:
|
||||||
// NOTE: There is no guarantee about null-termination.
|
// NOTE: There is no guarantee about null-termination.
|
||||||
[[nodiscard]] ReadonlyBytes bytes() const;
|
[[nodiscard]] ReadonlyBytes bytes() const;
|
||||||
[[nodiscard]] u32 hash() const;
|
[[nodiscard]] u32 hash() const;
|
||||||
|
[[nodiscard]] size_t byte_count() const;
|
||||||
|
|
||||||
[[nodiscard]] bool operator==(StringBase const&) const;
|
[[nodiscard]] bool operator==(StringBase const&) const;
|
||||||
|
|
||||||
|
@ -100,6 +101,10 @@ protected:
|
||||||
callback(buffer);
|
callback(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
ShortString m_short_string;
|
ShortString m_short_string;
|
||||||
Detail::StringData const* m_data { nullptr };
|
Detail::StringData const* m_data { nullptr };
|
||||||
|
|
|
@ -59,6 +59,8 @@ public:
|
||||||
bool is_fly_string() const { return m_is_fly_string; }
|
bool is_fly_string() const { return m_is_fly_string; }
|
||||||
void set_fly_string(bool is_fly_string) const { m_is_fly_string = is_fly_string; }
|
void set_fly_string(bool is_fly_string) const { m_is_fly_string = is_fly_string; }
|
||||||
|
|
||||||
|
size_t byte_count() const { return m_byte_count; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit StringData(size_t byte_count);
|
explicit StringData(size_t byte_count);
|
||||||
StringData(StringData const& superstring, size_t start, size_t byte_count);
|
StringData(StringData const& superstring, size_t start, size_t byte_count);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue