Blender V4.3
node_fn_replace_string.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string_utils.hh"
6
8
10
12{
13 b.add_input<decl::String>("String").hide_label();
14 b.add_input<decl::String>("Find").description("The string to find in the input string");
15 b.add_input<decl::String>("Replace").description("The string to replace each match with");
16 b.add_output<decl::String>("String");
17}
18
19static std::string replace_all(const StringRefNull str,
20 const StringRefNull from,
21 const StringRefNull to)
22{
23 if (from.is_empty()) {
24 return str;
25 }
26 char *new_str_ptr = BLI_string_replaceN(str.c_str(), from.c_str(), to.c_str());
27 std::string new_str{new_str_ptr};
28 MEM_freeN(new_str_ptr);
29 return new_str;
30}
31
33{
34 static auto substring_fn = mf::build::SI3_SO<std::string, std::string, std::string, std::string>(
35 "Replace", [](const std::string &str, const std::string &find, const std::string &replace) {
36 return replace_all(str, find, replace);
37 });
38 builder.set_matching_fn(&substring_fn);
39}
40
41static void node_register()
42{
43 static blender::bke::bNodeType ntype;
44
45 fn_node_type_base(&ntype, FN_NODE_REPLACE_STRING, "Replace String", NODE_CLASS_CONVERTER);
46 ntype.declare = node_declare;
49}
51
52} // namespace blender::nodes::node_fn_replace_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:410
char * BLI_string_replaceN(const char *__restrict str, const char *__restrict substr_old, const char *__restrict substr_new) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void set_matching_fn(const mf::MultiFunction *fn)
local_group_size(16, 16) .push_constant(Type b
#define str(s)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void node_declare(NodeDeclarationBuilder &b)
static std::string replace_all(const StringRefNull str, const StringRefNull from, const StringRefNull to)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
void fn_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
Defines a node type.
Definition BKE_node.hh:218
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:336
NodeDeclareFunction declare
Definition BKE_node.hh:347