ソースを参照

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. :^)
Andreas Kling 6 年 前
コミット
cd8278e489
2 ファイル変更17 行追加0 行削除
  1. 3 0
      AK/AKString.h
  2. 14 0
      AK/String.cpp

+ 3 - 0
AK/AKString.h

@@ -133,6 +133,9 @@ public:
     bool operator==(const String&) const;
     bool operator==(const String&) const;
     bool operator!=(const String& other) const { return !(*this == other); }
     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 String&) const;
     bool operator<(const char*) const;
     bool operator<(const char*) const;
     bool operator>=(const String& other) const { return !(*this < other); }
     bool operator>=(const String& other) const { return !(*this < other); }

+ 14 - 0
AK/String.cpp

@@ -19,6 +19,20 @@ bool String::operator==(const String& other) const
     return !memcmp(characters(), other.characters(), length());
     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
 bool String::operator<(const String& other) const
 {
 {
     if (!m_impl)
     if (!m_impl)