Blender  V3.3
node_fn_replace_string.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BLI_string_utf8.h"
4 
5 #include "node_function_util.hh"
6 
8 
10 {
11  b.add_input<decl::String>(N_("String"));
12  b.add_input<decl::String>(N_("Find")).description(N_("The string to find in the input string"));
13  b.add_input<decl::String>(N_("Replace"))
14  .description(N_("The string to replace each match with"));
15  b.add_output<decl::String>(N_("String"));
16 }
17 
18 static std::string replace_all(const StringRefNull str,
19  const StringRefNull from,
20  const StringRefNull to)
21 {
22  if (from.is_empty()) {
23  return str;
24  }
25  char *new_str_ptr = BLI_str_replaceN(str.c_str(), from.c_str(), to.c_str());
26  std::string new_str{new_str_ptr};
27  MEM_freeN(new_str_ptr);
28  return new_str;
29 }
30 
32 {
34  "Replace", [](const std::string &str, const std::string &find, const std::string &replace) {
35  return replace_all(str, find, replace);
36  }};
37  builder.set_matching_fn(&substring_fn);
38 }
39 
40 } // namespace blender::nodes::node_fn_replace_string_cc
41 
43 {
45 
46  static bNodeType ntype;
47 
51  nodeRegisterType(&ntype);
52 }
#define FN_NODE_REPLACE_STRING
Definition: BKE_node.h:1533
#define NODE_CLASS_CONVERTER
Definition: BKE_node.h:351
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
char * BLI_str_replaceN(const char *__restrict str, const char *__restrict substr_old, const char *__restrict substr_new) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:448
constexpr const char * c_str() const
void set_matching_fn(const MultiFunction *fn)
StackEntry * from
#define str(s)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static void fn_node_replace_string_declare(NodeDeclarationBuilder &b)
static std::string replace_all(const StringRefNull str, const StringRefNull from, const StringRefNull to)
static void fn_node_replace_string_build_multi_function(NodeMultiFunctionBuilder &builder)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_fn_replace_string()
void fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
Defines a node type.
Definition: BKE_node.h:226
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)