mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-27 05:05:32 +03:00
AK: Add case-insensitive hashing for the new String classes
Bringing over this functionality from DeprecatedString.
This commit is contained in:
parent
545d8336b8
commit
1e820385d9
Notes:
sideshowbarker
2024-07-17 18:46:30 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1e820385d9 Pull-request: https://github.com/SerenityOS/serenity/pull/20969 Reviewed-by: https://github.com/shannonbooth Reviewed-by: https://github.com/trflynn89
@ -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();
|
||||
|
@ -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)
|
||||
|
@ -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());
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user