mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:08:12 +00:00
AK: Move String::bytes() and String::operator==(String) to StringBase
The idea is to eventually get rid of protected state in StringBase. To do this, we first need to remove all references to m_data and m_short_string from String.
This commit is contained in:
parent
4364a28d3d
commit
54d149bc25
4 changed files with 21 additions and 19 deletions
|
@ -213,13 +213,6 @@ StringView String::bytes_as_string_view() const
|
||||||
return StringView(bytes());
|
return StringView(bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadonlyBytes String::bytes() const
|
|
||||||
{
|
|
||||||
if (is_short_string())
|
|
||||||
return m_short_string.bytes();
|
|
||||||
return m_data->bytes();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool String::is_empty() const
|
bool String::is_empty() const
|
||||||
{
|
{
|
||||||
return bytes().size() == 0;
|
return bytes().size() == 0;
|
||||||
|
@ -291,13 +284,6 @@ Optional<size_t> String::find_byte_offset(StringView substring, size_t from_byte
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool String::operator==(String const& other) const
|
|
||||||
{
|
|
||||||
if (is_short_string())
|
|
||||||
return m_data == other.m_data;
|
|
||||||
return bytes_as_string_view() == other.bytes_as_string_view();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool String::operator==(FlyString const& other) const
|
bool String::operator==(FlyString const& other) const
|
||||||
{
|
{
|
||||||
if (reinterpret_cast<uintptr_t>(m_data) == other.data({}))
|
if (reinterpret_cast<uintptr_t>(m_data) == other.data({}))
|
||||||
|
|
|
@ -121,10 +121,6 @@ public:
|
||||||
// Returns an iterable view over the Unicode code points.
|
// Returns an iterable view over the Unicode code points.
|
||||||
[[nodiscard]] Utf8View code_points() const;
|
[[nodiscard]] Utf8View code_points() const;
|
||||||
|
|
||||||
// Returns the underlying UTF-8 encoded bytes.
|
|
||||||
// NOTE: There is no guarantee about null-termination.
|
|
||||||
[[nodiscard]] ReadonlyBytes bytes() const;
|
|
||||||
|
|
||||||
// Returns true if the String is zero-length.
|
// Returns true if the String is zero-length.
|
||||||
[[nodiscard]] bool is_empty() const;
|
[[nodiscard]] bool is_empty() const;
|
||||||
|
|
||||||
|
@ -146,7 +142,7 @@ public:
|
||||||
Optional<size_t> find_byte_offset(u32 code_point, size_t from_byte_offset = 0) const;
|
Optional<size_t> find_byte_offset(u32 code_point, size_t from_byte_offset = 0) const;
|
||||||
Optional<size_t> find_byte_offset(StringView substring, size_t from_byte_offset = 0) const;
|
Optional<size_t> find_byte_offset(StringView substring, size_t from_byte_offset = 0) const;
|
||||||
|
|
||||||
[[nodiscard]] bool operator==(String const&) const;
|
[[nodiscard]] bool operator==(String const&) const = default;
|
||||||
[[nodiscard]] bool operator==(FlyString const&) const;
|
[[nodiscard]] bool operator==(FlyString const&) const;
|
||||||
[[nodiscard]] bool operator==(StringView) const;
|
[[nodiscard]] bool operator==(StringView) const;
|
||||||
[[nodiscard]] bool operator==(char const* cstring) const;
|
[[nodiscard]] bool operator==(char const* cstring) const;
|
||||||
|
|
|
@ -59,4 +59,18 @@ bool StringBase::is_short_string() const
|
||||||
return has_short_string_bit(reinterpret_cast<uintptr_t>(m_data));
|
return has_short_string_bit(reinterpret_cast<uintptr_t>(m_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReadonlyBytes StringBase::bytes() const
|
||||||
|
{
|
||||||
|
if (is_short_string())
|
||||||
|
return m_short_string.bytes();
|
||||||
|
return m_data->bytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StringBase::operator==(StringBase const& other) const
|
||||||
|
{
|
||||||
|
if (is_short_string())
|
||||||
|
return m_data == other.m_data;
|
||||||
|
return bytes() == other.bytes();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,12 @@ public:
|
||||||
// NOTE: This is primarily interesting to unit tests.
|
// NOTE: This is primarily interesting to unit tests.
|
||||||
[[nodiscard]] bool is_short_string() const;
|
[[nodiscard]] bool is_short_string() const;
|
||||||
|
|
||||||
|
// Returns the underlying UTF-8 encoded bytes.
|
||||||
|
// NOTE: There is no guarantee about null-termination.
|
||||||
|
[[nodiscard]] ReadonlyBytes bytes() const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool operator==(StringBase const&) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// NOTE: If the least significant bit of the pointer is set, this is a short string.
|
// NOTE: If the least significant bit of the pointer is set, this is a short string.
|
||||||
static constexpr uintptr_t SHORT_STRING_FLAG = 1;
|
static constexpr uintptr_t SHORT_STRING_FLAG = 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue