Skip to content

Commit

Permalink
AK: Make CaseInsensitiveStringTraits allocation-free
Browse files Browse the repository at this point in the history
Instead of calling String::to_lowercase(), do case-insensitive hashing
and comparison.
  • Loading branch information
awesomekling committed Feb 19, 2022
1 parent 1b6ed55 commit 2dd3b54
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AK/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ struct Traits<String> : public GenericTraits<String> {
};

struct CaseInsensitiveStringTraits : public Traits<String> {
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&);
Expand Down
5 changes: 5 additions & 0 deletions AK/StringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ NonnullRefPtr<StringImpl> StringImpl::to_uppercase() const
return const_cast<StringImpl&>(*this);
}

unsigned StringImpl::case_insensitive_hash() const
{
return case_insensitive_string_hash(characters(), length());
}

void StringImpl::compute_hash() const
{
if (!length())
Expand Down
2 changes: 2 additions & 0 deletions AK/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class StringImpl : public RefCounted<StringImpl> {
return m_hash;
}

unsigned case_insensitive_hash() const;

bool is_fly() const { return m_fly; }
void set_fly(Badge<FlyString>, bool fly) const { m_fly = fly; }

Expand Down

0 comments on commit 2dd3b54

Please sign in to comment.