From 1e820385d9137f16bfeea5f0aa0fd7248c7aa58e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 5 Sep 2023 19:55:21 +0200 Subject: [PATCH] AK: Add case-insensitive hashing for the new String classes Bringing over this functionality from DeprecatedString. --- AK/FlyString.cpp | 5 +++++ AK/FlyString.h | 6 ++++++ AK/String.cpp | 5 +++++ AK/String.h | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp index 4f1115316e9..3fa7c3e9802 100644 --- a/AK/FlyString.cpp +++ b/AK/FlyString.cpp @@ -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(bytes().data()), bytes().size()); +} + FlyString::operator String() const { return to_string(); diff --git a/AK/FlyString.h b/AK/FlyString.h index aba009dbff6..672c6439ca0 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -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 : Formatter { ErrorOr format(FormatBuilder&, FlyString const&); }; +struct ASCIICaseInsensitiveFlyStringTraits : public Traits { + 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) diff --git a/AK/String.cpp b/AK/String.cpp index 9784ae2c0e6..89017693a33 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -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(bytes().data()), bytes().size()); +} + Utf8View String::code_points() const { return Utf8View(bytes_as_string_view()); diff --git a/AK/String.h b/AK/String.h index d017ef37148..475f62111ec 100644 --- a/AK/String.h +++ b/AK/String.h @@ -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 static ErrorOr number(T value) @@ -263,6 +264,11 @@ struct Formatter : Formatter { ErrorOr format(FormatBuilder&, String const&); }; +struct ASCIICaseInsensitiveStringTraits : public Traits { + 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)