mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
AK: Move String::hash() and String::String() to StringBase
This commit is contained in:
parent
1b09a1851e
commit
d6290c4684
Notes:
sideshowbarker
2024-07-16 23:52:22 +09:00
Author: https://github.com/DanShaders Commit: https://github.com/SerenityOS/serenity/commit/d6290c4684 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
4 changed files with 16 additions and 16 deletions
|
@ -323,15 +323,6 @@ bool String::operator==(char const* c_string) const
|
|||
return bytes_as_string_view() == c_string;
|
||||
}
|
||||
|
||||
u32 String::hash() const
|
||||
{
|
||||
if (is_short_string()) {
|
||||
auto bytes = this->bytes();
|
||||
return string_hash(reinterpret_cast<char const*>(bytes.data()), bytes.size());
|
||||
}
|
||||
return m_data->hash();
|
||||
}
|
||||
|
||||
u32 String::ascii_case_insensitive_hash() const
|
||||
{
|
||||
return case_insensitive_string_hash(reinterpret_cast<char const*>(bytes().data()), bytes().size());
|
||||
|
|
|
@ -48,12 +48,6 @@ public:
|
|||
|
||||
using StringBase::StringBase;
|
||||
|
||||
// Creates an empty (zero-length) String.
|
||||
constexpr String()
|
||||
: StringBase(ShortString { SHORT_STRING_FLAG, {} })
|
||||
{
|
||||
}
|
||||
|
||||
// Creates a new String from a sequence of UTF-8 encoded code points.
|
||||
static ErrorOr<String> from_utf8(StringView);
|
||||
template<typename T>
|
||||
|
@ -153,7 +147,6 @@ public:
|
|||
[[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
||||
[[nodiscard]] bool contains(u32, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
|
||||
|
||||
[[nodiscard]] u32 hash() const;
|
||||
[[nodiscard]] u32 ascii_case_insensitive_hash() const;
|
||||
|
||||
template<Arithmetic T>
|
||||
|
|
|
@ -66,6 +66,15 @@ ReadonlyBytes StringBase::bytes() const
|
|||
return m_data->bytes();
|
||||
}
|
||||
|
||||
u32 StringBase::hash() const
|
||||
{
|
||||
if (is_short_string()) {
|
||||
auto bytes = this->bytes();
|
||||
return string_hash(reinterpret_cast<char const*>(bytes.data()), bytes.size());
|
||||
}
|
||||
return m_data->hash();
|
||||
}
|
||||
|
||||
bool StringBase::operator==(StringBase const& other) const
|
||||
{
|
||||
if (is_short_string())
|
||||
|
|
|
@ -30,6 +30,12 @@ static_assert(__builtin_offsetof(ShortString, byte_count_and_short_string_flag)
|
|||
|
||||
class StringBase {
|
||||
public:
|
||||
// Creates an empty (zero-length) String.
|
||||
constexpr StringBase()
|
||||
: StringBase(ShortString { SHORT_STRING_FLAG, {} })
|
||||
{
|
||||
}
|
||||
|
||||
StringBase(StringBase const&);
|
||||
|
||||
constexpr StringBase(StringBase&& other)
|
||||
|
@ -54,6 +60,7 @@ public:
|
|||
// Returns the underlying UTF-8 encoded bytes.
|
||||
// NOTE: There is no guarantee about null-termination.
|
||||
[[nodiscard]] ReadonlyBytes bytes() const;
|
||||
[[nodiscard]] u32 hash() const;
|
||||
|
||||
[[nodiscard]] bool operator==(StringBase const&) const;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue