[Bf-blender-cvs] [3b1c34267be] functions: improve copying StringRef to new buffer
Jacques Lucke
noreply at git.blender.org
Wed Feb 19 11:49:12 CET 2020
Commit: 3b1c34267be6b476acfb2a63d7101c73a516f5db
Author: Jacques Lucke
Date: Wed Feb 19 11:49:03 2020 +0100
Branches: functions
https://developer.blender.org/rB3b1c34267be6b476acfb2a63d7101c73a516f5db
improve copying StringRef to new buffer
===================================================================
M source/blender/blenlib/BLI_linear_allocator.h
M source/blender/blenlib/BLI_string_ref.h
M tests/gtests/blenlib/BLI_linear_allocator_test.cc
M tests/gtests/blenlib/BLI_string_ref_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_linear_allocator.h b/source/blender/blenlib/BLI_linear_allocator.h
index e276afbbb68..6aa3e896559 100644
--- a/source/blender/blenlib/BLI_linear_allocator.h
+++ b/source/blender/blenlib/BLI_linear_allocator.h
@@ -107,8 +107,9 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya
StringRefNull copy_string(StringRef str)
{
- char *buffer = (char *)this->allocate(str.size() + 1, 1);
- str.copy_to__with_null(buffer);
+ uint alloc_size = str.size() + 1;
+ char *buffer = (char *)this->allocate(alloc_size, 1);
+ str.copy(buffer, alloc_size);
return StringRefNull((const char *)buffer);
}
diff --git a/source/blender/blenlib/BLI_string_ref.h b/source/blender/blenlib/BLI_string_ref.h
index 54c2f0e7209..7808d4825da 100644
--- a/source/blender/blenlib/BLI_string_ref.h
+++ b/source/blender/blenlib/BLI_string_ref.h
@@ -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_linear_allocator_test.cc b/tests/gtests/blenlib/BLI_linear_allocator_test.cc
index 39280afadcc..0eaa2f2d456 100644
--- a/tests/gtests/blenlib/BLI_linear_allocator_test.cc
+++ b/tests/gtests/blenlib/BLI_linear_allocator_test.cc
@@ -9,7 +9,7 @@ static bool is_aligned(void *ptr, uint alignment)
return (POINTER_AS_UINT(ptr) & (alignment - 1)) == 0;
}
-TEST(monotonic_allocator, AllocationAlignment)
+TEST(linear_allocator, AllocationAlignment)
{
LinearAllocator<> allocator;
@@ -26,7 +26,7 @@ TEST(monotonic_allocator, AllocationAlignment)
EXPECT_TRUE(is_aligned(allocator.allocate(10, 128), 128));
}
-TEST(monotonic_allocator, PackedAllocation)
+TEST(linear_allocator, PackedAllocation)
{
LinearAllocator<> allocator;
BLI::AlignedBuffer<256, 32> buffer;
@@ -47,3 +47,17 @@ TEST(monotonic_allocator, PackedAllocation)
EXPECT_EQ(ptr6 - ptr5, 4); /* 60 - 56 = 4 */
EXPECT_EQ(ptr7 - ptr6, 1); /* 61 - 60 = 1 */
}
+
+TEST(linear_allocator, CopyString)
+{
+ LinearAllocator<> allocator;
+ BLI::AlignedBuffer<256, 1> buffer;
+ allocator.provide_buffer(buffer);
+
+ StringRefNull ref1 = allocator.copy_string("Hello");
+ StringRefNull ref2 = allocator.copy_string("World");
+
+ EXPECT_EQ(ref1, "Hello");
+ EXPECT_EQ(ref2, "World");
+ EXPECT_EQ(ref2.data() - ref1.data(), 6);
+}
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
index 38a970455c0..be1ee112022 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