AK: Add FlyString::equals_ignoring_case(StringView)

And share the code with String by moving the logic to StringUtils. :^)
This commit is contained in:
Andreas Kling 2020-03-22 13:07:45 +01:00
parent 0efa47b7ef
commit 26bc3d4ea0
6 changed files with 31 additions and 16 deletions

View file

@ -88,4 +88,9 @@ int FlyString::to_int(bool& ok) const
return StringUtils::convert_to_int(view(), ok);
}
bool FlyString::equals_ignoring_case(const StringView& other) const
{
return StringUtils::equals_ignoring_case(view(), other);
}
}

View file

@ -48,6 +48,8 @@ public:
int to_int(bool& ok) const;
bool equals_ignoring_case(const StringView&) const;
static void did_destroy_impl(Badge<StringImpl>, StringImpl&);
private:

View file

@ -38,13 +38,6 @@
extern "C" char* strstr(const char* haystack, const char* needle);
#endif
static inline char to_lowercase(char c)
{
if (c >= 'A' && c <= 'Z')
return c | 0x20;
return c;
}
namespace AK {
bool String::operator==(const String& other) const
@ -309,15 +302,7 @@ bool String::contains(const String& needle) const
bool String::equals_ignoring_case(const StringView& other) const
{
if (other.m_impl == impl())
return true;
if (length() != other.length())
return false;
for (size_t i = 0; i < length(); ++i) {
if (::to_lowercase(characters()[i]) != ::to_lowercase(other.characters_without_null_termination()[i]))
return false;
}
return true;
return StringUtils::equals_ignoring_case(view(), other);
}
String escape_html_entities(const StringView& html)

View file

@ -143,6 +143,26 @@ unsigned convert_to_uint(const StringView& str, bool& ok)
return value;
}
static inline char to_lowercase(char c)
{
if (c >= 'A' && c <= 'Z')
return c | 0x20;
return c;
}
bool equals_ignoring_case(const StringView& a, const StringView& b)
{
if (a.impl() && a.impl() == b.impl())
return true;
if (a.length() != b.length())
return false;
for (size_t i = 0; i < a.length(); ++i) {
if (to_lowercase(a.characters_without_null_termination()[i]) != to_lowercase(b.characters_without_null_termination()[i]))
return false;
}
return true;
}
}
}

View file

@ -41,6 +41,7 @@ namespace StringUtils {
bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive);
int convert_to_int(const StringView&, bool& ok);
unsigned convert_to_uint(const StringView&, bool& ok);
bool equals_ignoring_case(const StringView&, const StringView&);
}

View file

@ -139,6 +139,8 @@ public:
return !(*this == other);
}
const StringImpl* impl() const { return m_impl; }
private:
friend class String;
const StringImpl* m_impl { nullptr };