Blender V5.0
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.use_custom_socket_order();
14 b.allow_any_socket_order();
15 b.add_input<decl::String>("String").optional_label();
16 b.add_output<decl::String>("String").align_with_previous();
17 b.add_input<decl::String>("Find").description("The string to find in the input string");
18 b.add_input<decl::String>("Replace").description("The string to replace each match with");
19}
20
21static std::string replace_all(const StringRefNull str,
22 const StringRefNull from,
23 const StringRefNull to)
24{
25 if (from.is_empty()) {
26 return str;
27 }
28 char *new_str_ptr = BLI_string_replaceN(str.c_str(), from.c_str(), to.c_str());
29 std::string new_str{new_str_ptr};
30 MEM_freeN(new_str_ptr);
31 return new_str;
32}
33
35{
36 static auto substring_fn = mf::build::SI3_SO<std::string, std::string, std::string, std::string>(
37 "Replace", [](const std::string &str, const std::string &find, const std::string &replace) {
38 return replace_all(str, find, replace);
39 });
40 builder.set_matching_fn(&substring_fn);
41}
42
43static void node_register()
44{
45 static blender::bke::bNodeType ntype;
46
47 fn_node_type_base(&ntype, "FunctionNodeReplaceString", FN_NODE_REPLACE_STRING);
48 ntype.ui_name = "Replace String";
49 ntype.ui_description = "Replace a given string segment with another";
50 ntype.enum_name_legacy = "REPLACE_STRING";
52 ntype.declare = node_declare;
55}
57
58} // namespace blender::nodes::node_fn_replace_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_REPLACE_STRING
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)
constexpr bool is_empty() const
constexpr const char * c_str() const
void set_matching_fn(const mf::MultiFunction *fn)
#define str(s)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362