[cstdstring] adds CompareNoCase to StringUtils
authorJonathan Marshall <jmarshall@xbmc.org>
Sun, 27 Oct 2013 05:23:21 +0000 (18:23 +1300)
committerJonathan Marshall <jmarshall@xbmc.org>
Wed, 13 Nov 2013 21:53:03 +0000 (10:53 +1300)
xbmc/utils/StringUtils.cpp
xbmc/utils/StringUtils.h

index f585404..f1c05d8 100644 (file)
@@ -199,6 +199,24 @@ bool StringUtils::EqualsNoCase(const char *s1, const char *s2)
   return true;
 }
 
+int StringUtils::CompareNoCase(const std::string &str1, const std::string &str2)
+{
+  return CompareNoCase(str1.c_str(), str2.c_str());
+}
+
+int StringUtils::CompareNoCase(const char *s1, const char *s2)
+{
+  char c2; // we need only one char outside the loop
+  do
+  {
+    const char c1 = *s1++; // const local variable should help compiler to optimize
+    c2 = *s2++;
+    if (c1 != c2 && ::tolower(c1) != ::tolower(c2)) // This includes the possibility that one of the characters is the null-terminator, which implies a string mismatch.
+      return ::tolower(c1) < ::tolower(c2);
+  } while (c2 != '\0'); // At this point, we know c1 == c2, so there's no need to test them both.
+  return 0;
+}
+
 string StringUtils::Left(const string &str, size_t count)
 {
   count = max((size_t)0, min(count, str.size()));
index c407ecd..1d1aade 100644 (file)
@@ -61,6 +61,8 @@ public:
   static bool EqualsNoCase(const std::string &str1, const std::string &str2);
   static bool EqualsNoCase(const std::string &str1, const char *s2);
   static bool EqualsNoCase(const char *s1, const char *s2);
+  static int  CompareNoCase(const std::string &str1, const std::string &str2);
+  static int  CompareNoCase(const char *s1, const char *s2);
   static std::string Left(const std::string &str, size_t count);
   static std::string Mid(const std::string &str, size_t first, size_t count = std::string::npos);
   static std::string Right(const std::string &str, size_t count);