Blender V5.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#include "node_shader_util.hh"
9
11
13{
14 b.is_function_node();
15 b.use_custom_socket_order();
16 b.allow_any_socket_order();
17 b.add_input<decl::String>("String"_ustr).optional_label();
18 b.add_output<decl::String>("String"_ustr).align_with_previous();
19 b.add_input<decl::String>("Find"_ustr).description("The string to find in the input string");
20 b.add_input<decl::String>("Replace"_ustr).description("The string to replace each match with");
21}
22
23static std::string replace_all(const StringRefNull str,
24 const StringRefNull from,
25 const StringRefNull to)
26{
27 if (from.is_empty()) {
28 return str;
29 }
30 char *new_str_ptr = BLI_string_replaceN(str.c_str(), from.c_str(), to.c_str());
31 std::string new_str{new_str_ptr};
32 MEM_delete(new_str_ptr);
33 return new_str;
34}
35
37{
38 static auto substring_fn = mf::build::SI3_SO<std::string, std::string, std::string, std::string>(
39 "Replace", [](const std::string &str, const std::string &find, const std::string &replace) {
40 return replace_all(str, find, replace);
41 });
42 builder.set_matching_fn(&substring_fn);
43}
44
45static void node_register()
46{
47 static bke::bNodeType ntype;
48
49 common_node_type_base(&ntype, "FunctionNodeReplaceString"_ustr, FN_NODE_REPLACE_STRING);
50 ntype.ui_name = "Replace String";
51 ntype.ui_description = "Replace a given string segment with another";
52 ntype.enum_name_legacy = "REPLACE_STRING";
54 ntype.declare = node_declare;
57}
59
60} // namespace blender::nodes::node_fn_replace_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_REPLACE_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr const char * c_str() const
constexpr bool is_empty() const
void set_matching_fn(const mf::MultiFunction *fn)
#define str(s)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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)
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
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393