[Bf-blender-cvs] [7db0b9c265c] functions: add utility to combine strings on the stack
Jacques Lucke
noreply at git.blender.org
Thu Jul 25 15:10:42 CEST 2019
Commit: 7db0b9c265c4024bf256be59f826eb7cc0128f20
Author: Jacques Lucke
Date: Thu Jul 25 15:09:57 2019 +0200
Branches: functions
https://developer.blender.org/rB7db0b9c265c4024bf256be59f826eb7cc0128f20
add utility to combine strings on the stack
===================================================================
M source/blender/blenlib/BLI_string_ref.hpp
M source/blender/simulations/bparticles/inserters.cpp
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 ac300f487b2..6749dddf5a1 100644
--- a/source/blender/blenlib/BLI_string_ref.hpp
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -26,6 +26,7 @@
#include <string>
#include "BLI_utildefines.h"
+#include "BLI_alloca.h"
namespace BLI {
@@ -221,3 +222,18 @@ inline bool StringRefBase::endswith(StringRef suffix) const
}
} // namespace BLI
+
+#define BLI_STRINGREF_STACK_COMBINE(result, a, b) \
+ BLI::StringRefNull result; \
+ { \
+ BLI::StringRef a_ref(a); \
+ BLI::StringRef b_ref(b); \
+ uint characters = a_ref.size() + b_ref.size(); \
+ char *result##_ptr = BLI_array_alloca(result##_ptr, characters + 1); \
+ for (uint i = 0; i < a_ref.size(); i++) \
+ result##_ptr[i] = a_ref[i]; \
+ for (uint i = 0; i < b_ref.size(); i++) \
+ result##_ptr[i + a_ref.size()] = b_ref[i]; \
+ result##_ptr[characters] = '\0'; \
+ result = BLI::StringRefNull(result##_ptr, a_ref.size() + b_ref.size()); \
+ }
diff --git a/source/blender/simulations/bparticles/inserters.cpp b/source/blender/simulations/bparticles/inserters.cpp
index d33d0f0063d..74c667e3ffe 100644
--- a/source/blender/simulations/bparticles/inserters.cpp
+++ b/source/blender/simulations/bparticles/inserters.cpp
@@ -69,7 +69,7 @@ ValueOrError<ParticleFunction> create_particle_function(VirtualNode *main_vnode,
VirtualNode *vnode = vsocket->vnode();
SharedType &type = data_graph.graph()->type_of_output(socket);
- std::string name_prefix;
+ StringRef name_prefix;
if (STREQ(vnode->idname(), "bp_ParticleInfoNode")) {
name_prefix = "Attribute: ";
}
@@ -79,7 +79,8 @@ ValueOrError<ParticleFunction> create_particle_function(VirtualNode *main_vnode,
else {
BLI_assert(false);
}
- fn_builder.add_input(name_prefix + vsocket->name(), type);
+ BLI_STRINGREF_STACK_COMBINE(name, name_prefix, vsocket->name());
+ fn_builder.add_input(name, type);
}
SharedFunction fn = fn_builder.build(main_vnode->name());
diff --git a/tests/gtests/blenlib/BLI_string_ref_test.cc b/tests/gtests/blenlib/BLI_string_ref_test.cc
index 2e53f1f2c01..00cb7023de6 100644
--- a/tests/gtests/blenlib/BLI_string_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_string_ref_test.cc
@@ -218,3 +218,23 @@ TEST(string_ref, DropPrefix)
EXPECT_EQ(ref2.size(), 1);
EXPECT_EQ(ref2, "t");
}
+
+TEST(string_ref, CombineOnStack1)
+{
+ StringRef ref1("hello ");
+ StringRef ref2("world");
+ BLI_STRINGREF_STACK_COMBINE(ref_combined, ref1, ref2);
+ EXPECT_EQ(ref_combined.size(), 11);
+ EXPECT_EQ(ref_combined, "hello world");
+}
+
+TEST(string_ref, CombineOnStack2)
+{
+ StringRef ref1("");
+ StringRef ref2("");
+ void *before = alloca(1);
+ BLI_STRINGREF_STACK_COMBINE(ref_combined, ref1, ref2);
+ void *after = alloca(1);
+
+ EXPECT_TRUE(IN_RANGE_INCL((void *)ref_combined.begin(), before, after));
+}
More information about the Bf-blender-cvs
mailing list