diff --git a/AK/StringBase.h b/AK/StringBase.h index f9fc61ed30..37c699a120 100644 --- a/AK/StringBase.h +++ b/AK/StringBase.h @@ -219,7 +219,7 @@ inline u32 StringBase::hash() const return string_hash(reinterpret_cast(bytes.data()), bytes.size()); } if (!m_impl.data) - return string_hash(nullptr, 0); + return string_hash(nullptr, 0); return data_without_union_member_assertion()->hash(); } diff --git a/AK/StringHash.h b/AK/StringHash.h index dd45698252..9f23157f88 100644 --- a/AK/StringHash.h +++ b/AK/StringHash.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace AK { @@ -14,7 +15,8 @@ namespace AK { // We can't use SipHash since that depends on runtime parameters, // but some string hashes like IPC endpoint magic numbers need to be deterministic. // Maybe use a SipHash with a statically-known key? -constexpr u32 string_hash(char const* characters, size_t length, u32 seed = 0) +template T> +constexpr u32 string_hash(T const* characters, size_t length, u32 seed = 0) { u32 hash = seed; for (size_t i = 0; i < length; ++i) { diff --git a/AK/Utf16StringBase.h b/AK/Utf16StringBase.h index e3d74c2502..b2e5a37f10 100644 --- a/AK/Utf16StringBase.h +++ b/AK/Utf16StringBase.h @@ -176,7 +176,7 @@ public: if (auto const* data = data_without_union_member_assertion()) return data->hash(); - return string_hash(nullptr, 0); + return string_hash(nullptr, 0); } [[nodiscard]] ALWAYS_INLINE bool is_empty() const { return length_in_code_units() == 0uz; } diff --git a/AK/Utf16StringData.h b/AK/Utf16StringData.h index dc2f0902ac..6573c98214 100644 --- a/AK/Utf16StringData.h +++ b/AK/Utf16StringData.h @@ -78,8 +78,11 @@ public: ALWAYS_INLINE u32 hash() const { - if (!m_has_hash) - m_hash = calculate_hash(); + if (!m_has_hash) { + m_hash = utf16_view().hash(); + m_has_hash = true; + } + return m_hash; } @@ -128,13 +131,6 @@ private: [[nodiscard]] size_t calculate_code_point_length() const; - [[nodiscard]] ALWAYS_INLINE u32 calculate_hash() const - { - if (has_ascii_storage()) - return ascii_view().hash(); - return utf16_view().hash(); - } - // We store whether this string has ASCII or UTF-16 storage by setting the most significant bit of m_length_in_code_units // to 1 for UTF-16 storage. This shrinks the size of most UTF-16 string related classes, at the cost of not being // allowed to create a string larger than 2**63 - 1. diff --git a/AK/Utf16View.h b/AK/Utf16View.h index 646f6d64a6..6c35addef5 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -324,7 +324,7 @@ public: return 0; if (has_ascii_storage()) return string_hash(m_string.ascii, length_in_code_units()); - return string_hash(reinterpret_cast(m_string.utf16), length_in_code_units() * sizeof(char16_t)); + return string_hash(m_string.utf16, length_in_code_units()); } [[nodiscard]] constexpr bool is_null() const