Blender V5.0
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.h"
6
8
10
12{
13 b.add_input<decl::String>("String").optional_label();
14 b.add_input<decl::String>("Search");
15 b.add_output<decl::Int>("First Found");
16 b.add_output<decl::Int>("Count");
17}
18
19static int string_find(const StringRef text, const StringRef token)
20{
21 if (text.is_empty() || token.is_empty()) {
22 return 0;
23 }
24 const int pos = text.find(token, 0);
25 size_t r_len_bytes;
26 const int pos_n = BLI_strnlen_utf8_ex(text.data(), pos, &r_len_bytes);
27 return pos_n;
28}
29
30static int string_count(const StringRef text, const StringRef token)
31{
32 if (text.is_empty() || token.is_empty()) {
33 return 0;
34 }
35 int count = 0;
36 const int match_len = token.size();
37 int pos = 0;
38 while ((pos = text.find(token, pos)) != StringRef::not_found) {
39 count++;
40 pos += match_len;
41 }
42 return count;
43}
44
46{
47 static auto token_position_count = mf::build::SI2_SO2<std::string, std::string, int, int>(
48 "Find in String",
49 [](const std::string &text, const std::string &token, int &first, int &count) -> void {
50 first = string_find(text, token);
51 count = string_count(text, token);
52 });
53
54 builder.set_matching_fn(&token_position_count);
55}
56
57static void node_register()
58{
59 static blender::bke::bNodeType ntype;
60
61 fn_node_type_base(&ntype, "FunctionNodeFindInString", FN_NODE_FIND_IN_STRING);
62 ntype.ui_name = "Find in String";
63 ntype.ui_description =
64 "Find the number of times a given string occurs in another string and the position of the "
65 "first match";
67 ntype.declare = node_declare;
70}
72
73} // namespace blender::nodes::node_fn_find_in_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_FIND_IN_STRING
size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes) ATTR_NONNULL(1
#define NOD_REGISTER_NODE(REGISTER_FUNC)
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr const char * data() const
void set_matching_fn(const mf::MultiFunction *fn)
uint pos
int count
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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)
static void node_declare(NodeDeclarationBuilder &b)
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
NodeDeclareFunction declare
Definition BKE_node.hh:362