AK: Add case-insensitive hashing for the new String classes

Bringing over this functionality from DeprecatedString.
This commit is contained in:
Andreas Kling 2023-09-05 19:55:21 +02:00 committed by Tim Flynn
parent 545d8336b8
commit 1e820385d9
Notes: sideshowbarker 2024-07-17 18:46:30 +09:00
4 changed files with 22 additions and 0 deletions

View file

@ -100,6 +100,11 @@ unsigned FlyString::hash() const
return String::fly_string_data_to_hash({}, m_data);
}
u32 FlyString::ascii_case_insensitive_hash() const
{
return case_insensitive_string_hash(reinterpret_cast<char const*>(bytes().data()), bytes().size());
}
FlyString::operator String() const
{
return to_string();

View file

@ -36,6 +36,7 @@ public:
[[nodiscard]] bool is_empty() const;
[[nodiscard]] unsigned hash() const;
[[nodiscard]] u32 ascii_case_insensitive_hash() const;
explicit operator String() const;
String to_string() const;
@ -88,6 +89,11 @@ struct Formatter<FlyString> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder&, FlyString const&);
};
struct ASCIICaseInsensitiveFlyStringTraits : public Traits<String> {
static unsigned hash(FlyString const& s) { return s.ascii_case_insensitive_hash(); }
static bool equals(FlyString const& a, FlyString const& b) { return a.bytes().data() == b.bytes().data() || a.bytes_as_string_view().equals_ignoring_ascii_case(b.bytes_as_string_view()); }
};
}
[[nodiscard]] ALWAYS_INLINE AK::FlyString operator""_fly_string(char const* cstring, size_t length)

View file

@ -439,6 +439,11 @@ u32 String::hash() const
return m_data->hash();
}
u32 String::ascii_case_insensitive_hash() const
{
return case_insensitive_string_hash(reinterpret_cast<char const*>(bytes().data()), bytes().size());
}
Utf8View String::code_points() const
{
return Utf8View(bytes_as_string_view());

View file

@ -168,6 +168,7 @@ public:
[[nodiscard]] bool contains(u32, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] u32 hash() const;
[[nodiscard]] u32 ascii_case_insensitive_hash() const;
template<Arithmetic T>
static ErrorOr<String> number(T value)
@ -263,6 +264,11 @@ struct Formatter<String> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder&, String const&);
};
struct ASCIICaseInsensitiveStringTraits : public Traits<String> {
static unsigned hash(String const& s) { return s.ascii_case_insensitive_hash(); }
static bool equals(String const& a, String const& b) { return a.bytes_as_string_view().equals_ignoring_ascii_case(b.bytes_as_string_view()); }
};
}
[[nodiscard]] ALWAYS_INLINE AK::String operator""_string(char const* cstring, size_t length)