Blender V5.3
node_fn_find_in_string.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string_utf8.hh"
6
8#include "node_shader_util.hh"
9
11
12enum class Mode {
15};
16
17static const EnumPropertyItem mode_items[] = {
19 "FROM_START",
20 0,
21 N_("From Start"),
22 N_("Find the first occurrence of the string")},
24 "FROM_END",
25 0,
26 N_("From End"),
27 N_("Find the last occurrence of the string")},
28 {},
29};
30
32{
33 b.is_function_node();
34 b.add_input<decl::String>("String"_ustr).optional_label();
35 b.add_input<decl::String>("Search"_ustr);
36 b.add_input<decl::Menu>("Mode"_ustr).static_items(mode_items).optional_label();
37 b.add_output<decl::Int>("First Found"_ustr);
38 b.add_output<decl::Int>("Count"_ustr);
39}
40
41static int string_find(const StringRef text, const StringRef token, const bool from_end)
42{
43 if (text.is_empty() || token.is_empty()) {
44 return 0;
45 }
46 const int pos = from_end ? text.rfind(token) : text.find(token, 0);
47 size_t r_len_bytes;
48 const int pos_n = BLI_strnlen_utf8_ex(text.data(), pos, &r_len_bytes);
49 return pos_n;
50}
51
52static int string_count(const StringRef text, const StringRef token)
53{
54 if (text.is_empty() || token.is_empty()) {
55 return 0;
56 }
57 int count = 0;
58 const int match_len = token.size();
59 int pos = 0;
60 while ((pos = text.find(token, pos)) != StringRef::not_found) {
61 count++;
62 pos += match_len;
63 }
64 return count;
65}
66
68{
69 static auto token_position_count =
70 mf::build::SI3_SO2<std::string, std::string, MenuValue, int, int>(
71 "Find in String",
72 [](const std::string &text,
73 const std::string &token,
74 const MenuValue mode,
75 int &first,
76 int &count) -> void {
77 first = string_find(text, token, mode == Mode::FirstFromEnd);
78 count = string_count(text, token);
79 });
80
81 builder.set_matching_fn(&token_position_count);
82}
83
84static void node_register()
85{
86 static bke::bNodeType ntype;
87
88 common_node_type_base(&ntype, "FunctionNodeFindInString"_ustr, FN_NODE_FIND_IN_STRING);
89 ntype.ui_name = "Find in String";
90 ntype.ui_description =
91 "Find the number of times a given string occurs in another string and the position of the "
92 "first match";
94 ntype.declare = node_declare;
97}
99
100} // namespace blender::nodes::node_fn_find_in_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_FIND_IN_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr const char * data() const
void set_matching_fn(const mf::MultiFunction *fn)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static int string_count(const StringRef text, const StringRef token)
static int string_find(const StringRef text, const StringRef token, const bool from_end)
static void node_declare(NodeDeclarationBuilder &b)
size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes) 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
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)