[Bf-blender-cvs] [cba2b0367c8] functions: stringref: iterate, startswith and endswith

Jacques Lucke noreply at git.blender.org
Mon Jul 8 17:57:04 CEST 2019


Commit: cba2b0367c8045570d243accc322287a6bfaf5f7
Author: Jacques Lucke
Date:   Mon Jul 8 13:57:25 2019 +0200
Branches: functions
https://developer.blender.org/rBcba2b0367c8045570d243accc322287a6bfaf5f7

stringref: iterate, startswith and endswith

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

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 39dbc117410..b9555aed5a3 100644
--- a/source/blender/blenlib/BLI_string_ref.hpp
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -29,6 +29,8 @@
 
 namespace BLI {
 
+class StringRef;
+
 class StringRefBase {
  public:
   using size_type = size_t;
@@ -64,6 +66,20 @@ class StringRefBase {
     return m_data[index];
   }
 
+  const char *begin() const
+  {
+    return m_data;
+  }
+
+  const char *end()
+  {
+    return m_data + m_size;
+  }
+
+  bool startswith(StringRef prefix) const;
+
+  bool endswith(StringRef suffix) const;
+
   /**
    * Convert the referenced string into a std::string object.
    */
@@ -178,4 +194,34 @@ class StringRef : public StringRefBase {
   }
 };
 
+/* More inline functions
+ ***************************************/
+
+inline bool StringRefBase::startswith(StringRef prefix) const
+{
+  if (m_size < prefix.m_size) {
+    return false;
+  }
+  for (uint i = 0; i < prefix.m_size; i++) {
+    if (m_data[i] != prefix.m_data[i]) {
+      return false;
+    }
+  }
+  return true;
+}
+
+inline bool StringRefBase::endswith(StringRef suffix) const
+{
+  if (m_size < suffix.m_size) {
+    return false;
+  }
+  uint offset = m_size - suffix.m_size;
+  for (uint i = 0; i < suffix.m_size; i++) {
+    if (m_data[offset + i] != suffix.m_data[i]) {
+      return false;
+    }
+  }
+  return true;
+}
+
 }  // namespace BLI
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
index 199dd7f2c45..cb81c7676d6 100644
--- a/tests/gtests/blenlib/BLI_string_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_string_ref_test.cc
@@ -1,6 +1,8 @@
 #include "testing/testing.h"
 #include "BLI_string_ref.hpp"
+#include "BLI_small_vector.hpp"
 
+using BLI::SmallVector;
 using BLI::StringRef;
 using BLI::StringRefNull;
 
@@ -159,3 +161,41 @@ TEST(string_ref, CompareEqualString2)
   EXPECT_TRUE(std::string("test") == ref);
   EXPECT_FALSE(std::string("other") == ref);
 }
+
+TEST(string_ref, Iterate)
+{
+  StringRef ref("test");
+  SmallVector<char> chars;
+  for (char c : ref) {
+    chars.append(c);
+  }
+  EXPECT_EQ(chars.size(), 4);
+  EXPECT_EQ(chars[0], 't');
+  EXPECT_EQ(chars[1], 'e');
+  EXPECT_EQ(chars[2], 's');
+  EXPECT_EQ(chars[3], 't');
+}
+
+TEST(string_ref, StartsWith)
+{
+  StringRef ref("test");
+  EXPECT_TRUE(ref.startswith(""));
+  EXPECT_TRUE(ref.startswith("t"));
+  EXPECT_TRUE(ref.startswith("te"));
+  EXPECT_TRUE(ref.startswith("tes"));
+  EXPECT_TRUE(ref.startswith("test"));
+  EXPECT_FALSE(ref.startswith("test "));
+  EXPECT_FALSE(ref.startswith("a"));
+}
+
+TEST(string_ref, EndsWith)
+{
+  StringRef ref("test");
+  EXPECT_TRUE(ref.endswith(""));
+  EXPECT_TRUE(ref.endswith("t"));
+  EXPECT_TRUE(ref.endswith("st"));
+  EXPECT_TRUE(ref.endswith("est"));
+  EXPECT_TRUE(ref.endswith("test"));
+  EXPECT_FALSE(ref.endswith(" test"));
+  EXPECT_FALSE(ref.endswith("a"));
+}



More information about the Bf-blender-cvs mailing list