summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-24 22:28:42 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-25 06:45:19 +0200
commitcd8278e489351ba68fbfc3cebc0298e5d3f8d867 (patch)
treede0dc12bea0e08e2715c560ff90bad35ee93f30c /AK
parentb020a5e7ce643663162c319cf91660a2c3039a0f (diff)
downloadserenity-cd8278e489351ba68fbfc3cebc0298e5d3f8d867.zip
AK: Add String::operator==(StringView)
Comparing a String to a StringView would instantiate a temporary String just for the comparison. Let's not do that. :^)
Diffstat (limited to 'AK')
-rwxr-xr-xAK/AKString.h3
-rw-r--r--AK/String.cpp14
2 files changed, 17 insertions, 0 deletions
diff --git a/AK/AKString.h b/AK/AKString.h
index 8606d2934e..0325f3831d 100755
--- a/AK/AKString.h
+++ b/AK/AKString.h
@@ -133,6 +133,9 @@ public:
bool operator==(const String&) const;
bool operator!=(const String& other) const { return !(*this == other); }
+ bool operator==(const StringView&) const;
+ bool operator!=(const StringView& other) const { return !(*this == other); }
+
bool operator<(const String&) const;
bool operator<(const char*) const;
bool operator>=(const String& other) const { return !(*this < other); }
diff --git a/AK/String.cpp b/AK/String.cpp
index 4f8d230259..40513aa6db 100644
--- a/AK/String.cpp
+++ b/AK/String.cpp
@@ -19,6 +19,20 @@ bool String::operator==(const String& other) const
return !memcmp(characters(), other.characters(), length());
}
+bool String::operator==(const StringView& other) const
+{
+ if (!m_impl)
+ return !other.m_characters;
+
+ if (!other.m_characters)
+ return false;
+
+ if (length() != other.length())
+ return false;
+
+ return !memcmp(characters(), other.characters_without_null_termination(), length());
+}
+
bool String::operator<(const String& other) const
{
if (!m_impl)