1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:54:58 +00:00

AK: Define a traits helper for case-insensitive StringView hashing

Currently, we define a CaseInsensitiveStringTraits structure for String.
Using this structure for StringView involves allocating a String from
that view, and a second string to convert that intermediate string to
lowercase.

This defines CaseInsensitiveStringViewTraits (and the underlying helper
case_insensitive_string_hash) to avoid allocations.
This commit is contained in:
Timothy Flynn 2022-01-10 11:47:23 -05:00 committed by Linus Groh
parent 7d9b6b41c3
commit 3dccaa39d8
3 changed files with 43 additions and 0 deletions

View file

@ -221,6 +221,15 @@ struct Traits<StringView> : public GenericTraits<StringView> {
static unsigned hash(StringView s) { return s.hash(); }
};
struct CaseInsensitiveStringViewTraits : public Traits<StringView> {
static unsigned hash(StringView s)
{
if (s.is_empty())
return 0;
return case_insensitive_string_hash(s.characters_without_null_termination(), s.length());
}
};
}
[[nodiscard]] ALWAYS_INLINE constexpr AK::StringView operator"" sv(const char* cstring, size_t length)
@ -228,4 +237,5 @@ struct Traits<StringView> : public GenericTraits<StringView> {
return AK::StringView(cstring, length);
}
using AK::CaseInsensitiveStringViewTraits;
using AK::StringView;