From e3c0e750556cc1c12ba6c2240977fcabf4f03c4a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 18 Dec 2019 12:43:53 +0100 Subject: [PATCH] AK: Add String::equals_ignoring_case(StringView) --- AK/String.cpp | 19 +++++++++++++++++++ AK/String.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/AK/String.cpp b/AK/String.cpp index 9ddc833dcd..a085f75b48 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -7,6 +7,13 @@ 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 @@ -322,5 +329,17 @@ bool String::contains(const String& needle) const return strstr(characters(), needle.characters()); } +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; } +} diff --git a/AK/String.h b/AK/String.h index 87842d923d..0d8f7dc836 100755 --- a/AK/String.h +++ b/AK/String.h @@ -110,6 +110,8 @@ public: return m_impl->to_uppercase(); } + bool equals_ignoring_case(const StringView&) const; + bool contains(const String&) const; Vector split_limit(char separator, size_t limit) const;