mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-07 18:17:23 +00:00
AK: Add hash traits for Utf16View
This is based on the hash in JS::Utf16StringImpl::compute_hash.
This commit is contained in:
parent
2abc955ca9
commit
a0eb47e2fc
Notes:
github-actions[bot]
2025-07-03 13:53:35 +00:00
Author: https://github.com/trflynn89
Commit: a0eb47e2fc
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5228
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/shannonbooth
1 changed files with 20 additions and 4 deletions
|
@ -14,6 +14,8 @@
|
|||
#include <AK/Optional.h>
|
||||
#include <AK/Span.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringHash.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/UnicodeUtils.h>
|
||||
#include <AK/Vector.h>
|
||||
|
@ -121,6 +123,13 @@ public:
|
|||
return m_length_in_code_points;
|
||||
}
|
||||
|
||||
u32 hash() const
|
||||
{
|
||||
if (is_empty())
|
||||
return 0;
|
||||
return string_hash(reinterpret_cast<char const*>(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<size_t>::max() };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<AK::Utf16View> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, AK::Utf16View const& value)
|
||||
struct Formatter<Utf16View> : Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, Utf16View const& value)
|
||||
{
|
||||
return builder.builder().try_append(value);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Traits<Utf16View> : public DefaultTraits<Utf16View> {
|
||||
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue