From 2dd3b5482788b2b40ed8a7a9da04700e98e77ea9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 18 Feb 2022 22:56:53 +0100 Subject: [PATCH] AK: Make CaseInsensitiveStringTraits allocation-free Instead of calling String::to_lowercase(), do case-insensitive hashing and comparison. --- AK/String.h | 4 ++-- AK/StringImpl.cpp | 5 +++++ AK/StringImpl.h | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/AK/String.h b/AK/String.h index ceb923895bd..9f7988dcdcf 100644 --- a/AK/String.h +++ b/AK/String.h @@ -305,8 +305,8 @@ struct Traits : public GenericTraits { }; struct CaseInsensitiveStringTraits : public Traits { - static unsigned hash(const String& s) { return s.impl() ? s.to_lowercase().impl()->hash() : 0; } - static bool equals(const String& a, const String& b) { return a.to_lowercase() == b.to_lowercase(); } + static unsigned hash(String const& s) { return s.impl() ? s.impl()->case_insensitive_hash() : 0; } + static bool equals(String const& a, String const& b) { return a.equals_ignoring_case(b); } }; bool operator<(const char*, const String&); diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index a7e95b847b8..9ea6362d597 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -133,6 +133,11 @@ NonnullRefPtr StringImpl::to_uppercase() const return const_cast(*this); } +unsigned StringImpl::case_insensitive_hash() const +{ + return case_insensitive_string_hash(characters(), length()); +} + void StringImpl::compute_hash() const { if (!length()) diff --git a/AK/StringImpl.h b/AK/StringImpl.h index 7468924794f..a06f976ab0d 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -75,6 +75,8 @@ public: return m_hash; } + unsigned case_insensitive_hash() const; + bool is_fly() const { return m_fly; } void set_fly(Badge, bool fly) const { m_fly = fly; }