[Bf-blender-cvs] [1f2b1d520fc] master: BLI: improve StringRef.copy
Jacques Lucke
noreply at git.blender.org
Fri Apr 24 23:35:30 CEST 2020
Commit: 1f2b1d520fce05acfd06ca42ec939d0017085232
Author: Jacques Lucke
Date: Fri Apr 24 23:35:17 2020 +0200
Branches: master
https://developer.blender.org/rB1f2b1d520fce05acfd06ca42ec939d0017085232
BLI: improve StringRef.copy
===================================================================
M source/blender/blenlib/BLI_string_ref.hh
M tests/gtests/blenlib/BLI_string_ref_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_string_ref.hh b/source/blender/blenlib/BLI_string_ref.hh
index 6c750a81e06..eacc501375a 100644
--- a/source/blender/blenlib/BLI_string_ref.hh
+++ b/source/blender/blenlib/BLI_string_ref.hh
@@ -94,12 +94,28 @@ class StringRefBase {
return m_data + m_size;
}
- void copy_to__with_null(char *dst) const
+ void unsafe_copy(char *dst) const
{
memcpy(dst, m_data, m_size);
dst[m_size] = '\0';
}
+ void copy(char *dst, uint dst_size) const
+ {
+ if (m_size < dst_size) {
+ this->unsafe_copy(dst);
+ }
+ else {
+ BLI_assert(false);
+ dst[0] = '\0';
+ }
+ }
+
+ template<uint N> void copy(char (&dst)[N])
+ {
+ this->copy(dst, N);
+ }
+
/**
* Returns true when the string begins with the given prefix. Otherwise false.
*/
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
index 1c0b641bbb5..393f28d4fc1 100644
--- a/tests/gtests/blenlib/BLI_string_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_string_ref_test.cc
@@ -237,3 +237,14 @@ TEST(string_ref, Substr)
EXPECT_EQ(ref.substr(3, 4), "lo w");
EXPECT_EQ(ref.substr(6, 5), "world");
}
+
+TEST(string_ref, Copy)
+{
+ StringRef ref("hello");
+ char dst[10];
+ memset(dst, 0xFF, 10);
+ ref.copy(dst);
+ EXPECT_EQ(dst[5], '\0');
+ EXPECT_EQ(dst[6], 0xFF);
+ EXPECT_EQ(ref, dst);
+}
More information about the Bf-blender-cvs
mailing list