Blender V5.3
node_fn_slice_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_utf8.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::Int>("Position"_ustr);
20 b.add_input<decl::Int>("Length"_ustr).min(0).default_value(10);
21}
22
24{
25 static auto slice_fn = mf::build::SI3_SO<std::string, int, int, std::string>(
26 "Slice", [](const std::string &str, int a, int b) {
27 const int start = BLI_str_utf8_offset_from_index(str.c_str(), str.size(), std::max(0, a));
28 const int end = BLI_str_utf8_offset_from_index(
29 str.c_str(), str.size(), std::max(0, a + b));
30 return str.substr(start, std::max<int>(end - start, 0));
31 });
32 builder.set_matching_fn(&slice_fn);
33}
34
35static void node_register()
36{
37 static bke::bNodeType ntype;
38
39 common_node_type_base(&ntype, "FunctionNodeSliceString"_ustr, FN_NODE_SLICE_STRING);
40 ntype.ui_name = "Slice String";
41 ntype.ui_description = "Extract a string segment from a larger string";
42 ntype.enum_name_legacy = "SLICE_STRING";
44 ntype.declare = node_declare;
47}
49
50} // namespace blender::nodes::node_fn_slice_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define FN_NODE_SLICE_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
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 void node_build_multi_function(NodeMultiFunctionBuilder &builder)
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
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