[Bf-blender-cvs] [5c72fb6a346] functions: implement == operator for StringRef

Jacques Lucke noreply at git.blender.org
Tue May 28 12:13:27 CEST 2019


Commit: 5c72fb6a346f0b7b4c2284848fe0a61cb299d7f2
Author: Jacques Lucke
Date:   Tue May 28 11:42:53 2019 +0200
Branches: functions
https://developer.blender.org/rB5c72fb6a346f0b7b4c2284848fe0a61cb299d7f2

implement == operator for StringRef

===================================================================

M	source/blender/blenlib/BLI_string_ref.hpp
M	tests/gtests/blenlib/BLI_string_ref_test.cc

===================================================================

diff --git a/source/blender/blenlib/BLI_string_ref.hpp b/source/blender/blenlib/BLI_string_ref.hpp
index d13218cc3f5..a5c361a77c9 100644
--- a/source/blender/blenlib/BLI_string_ref.hpp
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -76,6 +76,34 @@ class StringRefBase {
   {
     return a.data() + b;
   }
+
+  friend bool operator==(const StringRefBase a, const StringRefBase b)
+  {
+    if (a.size() != b.size()) {
+      return false;
+    }
+    return STREQLEN(a.data(), b.data(), a.size());
+  }
+
+  friend bool operator==(const StringRefBase a, const char *b)
+  {
+    return STREQ(a.data(), b);
+  }
+
+  friend bool operator==(const char *a, const StringRefBase b)
+  {
+    return b == a;
+  }
+
+  friend bool operator==(const StringRefBase a, const std::string &b)
+  {
+    return a == StringRefBase(b.data(), b.size());
+  }
+
+  friend bool operator==(const std::string &a, const StringRefBase b)
+  {
+    return b == a;
+  }
 };
 
 class StringRefNull : public StringRefBase {
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
index 5aa0aadaf09..199dd7f2c45 100644
--- a/tests/gtests/blenlib/BLI_string_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_string_ref_test.cc
@@ -122,3 +122,40 @@ TEST(string_ref, AddString2)
   std::string result = std::string("asd") + ref;
   EXPECT_EQ(result, "asdtest");
 }
+
+TEST(string_ref, CompareEqual)
+{
+  StringRef ref1("test");
+  StringRef ref2("test");
+  StringRef ref3("other");
+  EXPECT_TRUE(ref1 == ref2);
+  EXPECT_FALSE(ref1 == ref3);
+}
+
+TEST(string_ref, CompareEqualCharPtr1)
+{
+  StringRef ref("test");
+  EXPECT_TRUE(ref == "test");
+  EXPECT_FALSE(ref == "other");
+}
+
+TEST(string_ref, CompareEqualCharPtr2)
+{
+  StringRef ref("test");
+  EXPECT_TRUE("test" == ref);
+  EXPECT_FALSE("other" == ref);
+}
+
+TEST(string_ref, CompareEqualString1)
+{
+  StringRef ref("test");
+  EXPECT_TRUE(ref == std::string("test"));
+  EXPECT_FALSE(ref == std::string("other"));
+}
+
+TEST(string_ref, CompareEqualString2)
+{
+  StringRef ref("test");
+  EXPECT_TRUE(std::string("test") == ref);
+  EXPECT_FALSE(std::string("other") == ref);
+}



More information about the Bf-blender-cvs mailing list