Quellcode durchsuchen

AK: Add FlyString::equals_ignoring_case(StringView)

And share the code with String by moving the logic to StringUtils. :^)
Andreas Kling vor 5 Jahren
Ursprung
Commit
26bc3d4ea0
6 geänderte Dateien mit 31 neuen und 16 gelöschten Zeilen
  1. 5 0
      AK/FlyString.cpp
  2. 2 0
      AK/FlyString.h
  3. 1 16
      AK/String.cpp
  4. 20 0
      AK/StringUtils.cpp
  5. 1 0
      AK/StringUtils.h
  6. 2 0
      AK/StringView.h

+ 5 - 0
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);
+}
+
 }

+ 2 - 0
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>, StringImpl&);
 
 private:

+ 1 - 16
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)

+ 20 - 0
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;
+}
+
 }
 
 }

+ 1 - 0
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&);
 
 }
 

+ 2 - 0
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 };