Blender V5.0
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.h"
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::Int>("Position");
18 b.add_input<decl::Int>("Length").min(0).default_value(10);
19}
20
22{
23 static auto slice_fn = mf::build::SI3_SO<std::string, int, int, std::string>(
24 "Slice", [](const std::string &str, int a, int b) {
25 const int start = BLI_str_utf8_offset_from_index(str.c_str(), str.size(), std::max(0, a));
26 const int end = BLI_str_utf8_offset_from_index(
27 str.c_str(), str.size(), std::max(0, a + b));
28 return str.substr(start, std::max<int>(end - start, 0));
29 });
30 builder.set_matching_fn(&slice_fn);
31}
32
33static void node_register()
34{
35 static blender::bke::bNodeType ntype;
36
37 fn_node_type_base(&ntype, "FunctionNodeSliceString", FN_NODE_SLICE_STRING);
38 ntype.ui_name = "Slice String";
39 ntype.ui_description = "Extract a string segment from a larger string";
40 ntype.enum_name_legacy = "SLICE_STRING";
42 ntype.declare = node_declare;
45}
47
48} // namespace blender::nodes::node_fn_slice_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define FN_NODE_SLICE_STRING
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#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:2416
static void node_declare(NodeDeclarationBuilder &b)
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)
#define min(a, b)
Definition sort.cc:36
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