[Bf-blender-cvs] [d4a3adf0457] obj-import-experiments: strip methods for stringref
Jacques Lucke
noreply at git.blender.org
Sun Mar 22 16:17:38 CET 2020
Commit: d4a3adf0457f195703b854e9212324d8c0c71568
Author: Jacques Lucke
Date: Sun Mar 22 10:56:15 2020 +0100
Branches: obj-import-experiments
https://developer.blender.org/rBd4a3adf0457f195703b854e9212324d8c0c71568
strip methods for stringref
===================================================================
M source/blender/blenlib/BLI_string_ref.h
M tests/gtests/blenlib/BLI_string_ref_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_string_ref.h b/source/blender/blenlib/BLI_string_ref.h
index 92f31bbfe21..94e2848c50e 100644
--- a/source/blender/blenlib/BLI_string_ref.h
+++ b/source/blender/blenlib/BLI_string_ref.h
@@ -115,6 +115,10 @@ class StringRefBase {
bool endswith(char c) const;
StringRef substr(uint start, uint size) const;
+
+ StringRef lstrip(ArrayRef<char> chars = {' ', '\t', '\n', '\r'}) const;
+ StringRef rstrip(ArrayRef<char> chars = {' ', '\t', '\n', '\r'}) const;
+ StringRef strip(ArrayRef<char> chars = {' ', '\t', '\n', '\r'}) const;
};
/**
@@ -141,6 +145,17 @@ class StringRefNull : public StringRefBase {
StringRefNull(const std::string &str) : StringRefNull(str.data())
{
}
+
+ StringRefNull lstrip(ArrayRef<char> chars = {' ', '\t', '\r', '\n'}) const
+ {
+ for (uint i = 0; i < m_size; i++) {
+ char c = m_data[i];
+ if (!chars.contains(c)) {
+ return StringRefNull(m_data + i, m_size - i);
+ }
+ }
+ return "";
+ }
};
/**
@@ -310,6 +325,35 @@ inline StringRef StringRefBase::substr(uint start, uint size) const
return StringRef(m_data + start, size);
}
+inline StringRef StringRefBase::lstrip(ArrayRef<char> chars) const
+{
+ for (uint i = 0; i < m_size; i++) {
+ char c = m_data[i];
+ if (!chars.contains(c)) {
+ return StringRef(m_data + i, m_size - i);
+ }
+ }
+ return "";
+}
+
+inline StringRef StringRefBase::rstrip(ArrayRef<char> chars) const
+{
+ for (int i = m_size - 1; i >= 0; i--) {
+ char c = m_data[i];
+ if (!chars.contains(c)) {
+ return StringRef(m_data, i + 1);
+ }
+ }
+ return "";
+}
+
+inline StringRef StringRefBase::strip(ArrayRef<char> chars) const
+{
+ StringRef lstripped = this->lstrip(chars);
+ StringRef stripped = lstripped.rstrip(chars);
+ return stripped;
+}
+
} // namespace BLI
#endif /* __BLI_STRING_REF_H__ */
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
index 2f1174e93b1..a9687650b06 100644
--- a/tests/gtests/blenlib/BLI_string_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_string_ref_test.cc
@@ -288,3 +288,20 @@ TEST(string_ref, DropSuffix)
EXPECT_EQ(ref2, "hello w");
EXPECT_EQ(ref3, "");
}
+
+TEST(string_ref, Strip)
+{
+ StringRef ref(" \ttest \n ");
+ EXPECT_EQ(ref.lstrip(), "test \n ");
+ EXPECT_EQ(ref.rstrip(), " \ttest");
+ EXPECT_EQ(ref.strip(), "test");
+ EXPECT_EQ(ref.lstrip({' ', '\t', 't'}), "est \n ");
+ EXPECT_EQ(ref.strip({' ', '\t', '\n', 't'}), "es");
+}
+
+TEST(string_ref_null, Strip)
+{
+ StringRefNull ref1(" test ");
+ StringRefNull ref2 = ref1.lstrip();
+ EXPECT_EQ(ref2, "test ");
+}
More information about the Bf-blender-cvs
mailing list