Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Abrahamsson <erik85>2021-10-19 23:27:47 +0300
committerHans Goudey <h.goudey@me.com>2021-10-19 23:27:47 +0300
commita83b405a4524dc5a0014737ec25e31b12cd7216c (patch)
treea65a9412066571f11dbdf7f6030ff0b6b36721e9 /source/blender/nodes/function
parent85c8dd6c968334fc8c58a1f4ba1b145996510120 (diff)
Geometry Nodes: Replace String node
This commit adds a node that can be used to find and replace strings inside of the input string. One initial use case is to have an easier way to add line breaks to strings to the string to curves node. Differential Revision: https://developer.blender.org/D12721
Diffstat (limited to 'source/blender/nodes/function')
-rw-r--r--source/blender/nodes/function/nodes/node_fn_replace_string.cc67
1 files changed, 67 insertions, 0 deletions
diff --git a/source/blender/nodes/function/nodes/node_fn_replace_string.cc b/source/blender/nodes/function/nodes/node_fn_replace_string.cc
new file mode 100644
index 00000000000..d21044d54a5
--- /dev/null
+++ b/source/blender/nodes/function/nodes/node_fn_replace_string.cc
@@ -0,0 +1,67 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "BLI_string_utf8.h"
+
+#include "node_function_util.hh"
+
+namespace blender::nodes {
+
+static void fn_node_replace_string_declare(NodeDeclarationBuilder &b)
+{
+ b.add_input<decl::String>("String");
+ b.add_input<decl::String>("Find").description("The string to find in the input string");
+ b.add_input<decl::String>("Replace").description("The string to replace each match with");
+ b.add_output<decl::String>("String");
+};
+
+} // namespace blender::nodes
+
+static std::string replace_all(std::string str, const std::string &from, const std::string &to)
+{
+ if (from.length() <= 0) {
+ return str;
+ }
+ const size_t step = to.length() > 0 ? to.length() : 1;
+
+ size_t offset = 0;
+ while ((offset = str.find(from, offset)) != std::string::npos) {
+ str.replace(offset, from.length(), to);
+ offset += step;
+ }
+ return str;
+}
+
+static void fn_node_replace_string_build_multi_function(
+ blender::nodes::NodeMultiFunctionBuilder &builder)
+{
+ static blender::fn::CustomMF_SI_SI_SI_SO<std::string, std::string, std::string, std::string>
+ substring_fn{"Replace",
+ [](const std::string &str,
+ const std::string &find,
+ const std::string &replace) { return replace_all(str, find, replace); }};
+ builder.set_matching_fn(&substring_fn);
+}
+
+void register_node_type_fn_replace_string()
+{
+ static bNodeType ntype;
+
+ fn_node_type_base(&ntype, FN_NODE_REPLACE_STRING, "Replace String", NODE_CLASS_CONVERTER, 0);
+ ntype.declare = blender::nodes::fn_node_replace_string_declare;
+ ntype.build_multi_function = fn_node_replace_string_build_multi_function;
+ nodeRegisterType(&ntype);
+}