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
commit 3dccaa39d8
Notes: sideshowbarker 2024-07-17 21:13:55 +09:00
3 changed files with 43 additions and 0 deletions

View file

@ -189,3 +189,15 @@ TEST_CASE(constexpr_stuff)
}
#undef do_test
}
TEST_CASE(case_insensitive_hash)
{
auto string1 = "abcdef"sv;
auto string2 = "ABCDEF"sv;
auto string3 = "aBcDeF"sv;
auto string4 = "foo"sv;
EXPECT_EQ(CaseInsensitiveStringViewTraits::hash(string1), CaseInsensitiveStringViewTraits::hash(string2));
EXPECT_EQ(CaseInsensitiveStringViewTraits::hash(string1), CaseInsensitiveStringViewTraits::hash(string3));
EXPECT_NE(CaseInsensitiveStringViewTraits::hash(string1), CaseInsensitiveStringViewTraits::hash(string4));
}