From 26bc3d4ea02cce2cf7f11b66d1fe978c3de89f76 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 22 Mar 2020 13:07:45 +0100 Subject: [PATCH] AK: Add FlyString::equals_ignoring_case(StringView) And share the code with String by moving the logic to StringUtils. :^) --- AK/FlyString.cpp | 5 +++++ AK/FlyString.h | 2 ++ AK/String.cpp | 17 +---------------- AK/StringUtils.cpp | 20 ++++++++++++++++++++ AK/StringUtils.h | 1 + AK/StringView.h | 2 ++ 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/AK/FlyString.cpp b/AK/FlyString.cpp index 772f100710..dc199ba778 100644 --- a/AK/FlyString.cpp +++ b/AK/FlyString.cpp @@ -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); +} + } diff --git a/AK/FlyString.h b/AK/FlyString.h index c9290a8313..9876cbf031 100644 --- a/AK/FlyString.h +++ b/AK/FlyString.h @@ -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&); private: diff --git a/AK/String.cpp b/AK/String.cpp index 81e6e517a1..ec74a38742 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -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) diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp index 3c96a03384..7f6c6f4a97 100644 --- a/AK/StringUtils.cpp +++ b/AK/StringUtils.cpp @@ -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; +} + } } diff --git a/AK/StringUtils.h b/AK/StringUtils.h index 5f19888d48..f4b433b8ef 100644 --- a/AK/StringUtils.h +++ b/AK/StringUtils.h @@ -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&); } diff --git a/AK/StringView.h b/AK/StringView.h index 00e1af1e69..068873400a 100644 --- a/AK/StringView.h +++ b/AK/StringView.h @@ -139,6 +139,8 @@ public: return !(*this == other); } + const StringImpl* impl() const { return m_impl; } + private: friend class String; const StringImpl* m_impl { nullptr };