[Bf-blender-cvs] [6e4866465d1] functions: initial StringRef class
Jacques Lucke
noreply at git.blender.org
Wed May 15 11:43:06 CEST 2019
Commit: 6e4866465d16cd7ed6edd890bf412e871d1f9431
Author: Jacques Lucke
Date: Wed May 15 10:49:38 2019 +0200
Branches: functions
https://developer.blender.org/rB6e4866465d16cd7ed6edd890bf412e871d1f9431
initial StringRef class
===================================================================
A source/blender/blenlib/BLI_string_ref.hpp
M source/blender/blenlib/CMakeLists.txt
A tests/gtests/blenlib/BLI_string_ref_test.cc
M tests/gtests/blenlib/CMakeLists.txt
===================================================================
diff --git a/source/blender/blenlib/BLI_string_ref.hpp b/source/blender/blenlib/BLI_string_ref.hpp
new file mode 100644
index 00000000000..69bfe3d0115
--- /dev/null
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -0,0 +1,65 @@
+#pragma once
+
+/* Reference to a string that might NOT be null-terminated. */
+
+#include <cstring>
+#include <string>
+
+#include "BLI_utildefines.h"
+
+namespace BLI {
+
+class StringRef {
+ public:
+ using size_type = size_t;
+
+ private:
+ const char *m_data;
+ size_type m_size;
+
+ public:
+ StringRef() : m_data(nullptr), m_size(0)
+ {
+ }
+
+ StringRef(const char *str) : m_data(str), m_size(str ? strlen(str) : 0)
+ {
+ }
+
+ StringRef(const char *str, size_type length) : m_data(str), m_size(length)
+ {
+ }
+
+ StringRef(const std::string &str) : m_data(str.data()), m_size(str.size())
+ {
+ }
+
+ size_type size() const
+ {
+ return m_size;
+ }
+
+ const char *data() const
+ {
+ return m_data;
+ }
+
+ char operator[](size_type index) const
+ {
+ BLI_assert(index < m_size);
+ return m_data[index];
+ }
+
+ std::string to_std_string() const
+ {
+ return std::string(m_data, m_size);
+ }
+
+ friend std::ostream &operator<<(std::ostream &stream, StringRef ref)
+ {
+ stream << ref.to_std_string();
+ return stream;
+ }
+};
+
+} // namespace BLI
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 9c455e5399c..b6b4a3e7647 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -251,6 +251,7 @@ set(SRC
BLI_small_set.hpp
BLI_small_set_vector.hpp
BLI_small_stack.hpp
+ BLI_string_ref.hpp
BLI_timeit.hpp
)
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
new file mode 100644
index 00000000000..0f86a7233a8
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_string_ref_test.cc
@@ -0,0 +1,64 @@
+#include "testing/testing.h"
+#include "BLI_string_ref.hpp"
+
+using BLI::StringRef;
+
+TEST(string_ref, DefaultConstructor)
+{
+ StringRef ref;
+ EXPECT_EQ(ref.size(), 0);
+}
+
+TEST(string_ref, CStringConstructor)
+{
+ const char *str = "Test";
+ StringRef ref(str);
+ EXPECT_EQ(ref.size(), 4);
+ EXPECT_EQ(ref.data(), str);
+}
+
+TEST(string_ref, PointerWithLengthConstructor)
+{
+ const char *str = "Test";
+ StringRef ref(str, 2);
+ EXPECT_EQ(ref.size(), 2);
+ EXPECT_EQ(ref.data(), str);
+}
+
+TEST(string_ref, StdStringConstructor)
+{
+ std::string str = "Test";
+ StringRef ref(str);
+ EXPECT_EQ(ref.size(), 4);
+ EXPECT_EQ(ref.data(), str.data());
+}
+
+TEST(string_ref, SubscriptOperator)
+{
+ StringRef ref("hello");
+ EXPECT_EQ(ref.size(), 5);
+ EXPECT_EQ(ref[0], 'h');
+ EXPECT_EQ(ref[1], 'e');
+ EXPECT_EQ(ref[2], 'l');
+ EXPECT_EQ(ref[3], 'l');
+ EXPECT_EQ(ref[4], 'o');
+}
+
+TEST(string_ref, ToStdString)
+{
+ StringRef ref("test");
+ std::string str = ref.to_std_string();
+ EXPECT_EQ(str.size(), 4);
+ EXPECT_EQ(str, "test");
+}
+
+TEST(string_ref, Print)
+{
+ StringRef ref("test");
+ std::stringstream ss;
+ ss << ref;
+ ss << ref;
+ std::string str = ss.str();
+ EXPECT_EQ(str.size(), 8);
+ EXPECT_EQ(str, "testtest");
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 49e42a931e1..477a99938ee 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -70,6 +70,7 @@ BLENDER_TEST(BLI_small_stack "bf_blenlib")
BLENDER_TEST(BLI_small_map "bf_blenlib")
BLENDER_TEST(BLI_stack "bf_blenlib")
BLENDER_TEST(BLI_string "bf_blenlib")
+BLENDER_TEST(BLI_string_ref "bf_blenlib")
BLENDER_TEST(BLI_string_utf8 "bf_blenlib")
BLENDER_TEST(BLI_task "bf_blenlib;bf_intern_numaapi")
More information about the Bf-blender-cvs
mailing list