From a0eb47e2fc7c2758577e4829987b2195629567a1 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 19 Jun 2025 09:18:07 -0400 Subject: [PATCH] AK: Add hash traits for Utf16View This is based on the hash in JS::Utf16StringImpl::compute_hash. --- AK/Utf16View.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/AK/Utf16View.h b/AK/Utf16View.h index 986ca69d46a..7bb5a5d124d 100644 --- a/AK/Utf16View.h +++ b/AK/Utf16View.h @@ -14,6 +14,8 @@ #include #include #include +#include +#include #include #include #include @@ -121,6 +123,13 @@ public: return m_length_in_code_points; } + u32 hash() const + { + if (is_empty()) + return 0; + return string_hash(reinterpret_cast(m_code_units.data()), m_code_units.size() * sizeof(u16)); + } + Utf16CodePointIterator begin() const { return { begin_ptr(), m_code_units.size() }; } Utf16CodePointIterator end() const { return { end_ptr(), 0 }; } @@ -163,16 +172,23 @@ private: mutable size_t m_length_in_code_points { NumericLimits::max() }; }; -} - template<> -struct AK::Formatter : Formatter { - ErrorOr format(FormatBuilder& builder, AK::Utf16View const& value) +struct Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, Utf16View const& value) { return builder.builder().try_append(value); } }; +template<> +struct Traits : public DefaultTraits { + using PeekType = Utf16View; + using ConstPeekType = Utf16View; + static unsigned hash(Utf16View const& s) { return s.hash(); } +}; + +} + #if USING_AK_GLOBALLY using AK::Utf16Data; using AK::Utf16View;