Blender  V3.3
node_fn_slice_string.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BLI_string_utf8.h"
4 
5 #include "node_function_util.hh"
6 
8 
10 {
11  b.add_input<decl::String>(N_("String"));
12  b.add_input<decl::Int>(N_("Position"));
13  b.add_input<decl::Int>(N_("Length")).min(0).default_value(10);
14  b.add_output<decl::String>(N_("String"));
15 }
16 
18 {
20  "Slice", [](const std::string &str, int a, int b) {
21  const int len = BLI_strlen_utf8(str.c_str());
22  const int start = BLI_str_utf8_offset_from_index(str.c_str(), std::clamp(a, 0, len));
23  const int end = BLI_str_utf8_offset_from_index(str.c_str(), std::clamp(a + b, 0, len));
24  return str.substr(start, std::max<int>(end - start, 0));
25  }};
26  builder.set_matching_fn(&slice_fn);
27 }
28 
29 } // namespace blender::nodes::node_fn_slice_string_cc
30 
32 {
33  namespace file_ns = blender::nodes::node_fn_slice_string_cc;
34 
35  static bNodeType ntype;
36 
40  nodeRegisterType(&ntype);
41 }
#define NODE_CLASS_CONVERTER
Definition: BKE_node.h:351
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
#define FN_NODE_SLICE_STRING
Definition: BKE_node.h:1527
int BLI_str_utf8_offset_from_index(const char *str, int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: string_utf8.c:771
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: string_utf8.c:317
void set_matching_fn(const MultiFunction *fn)
int len
Definition: draw_manager.c:108
#define str(s)
static unsigned a[3]
Definition: RandGen.cpp:78
T clamp(const T &a, const T &min, const T &max)
static void fn_node_slice_string_build_multi_function(NodeMultiFunctionBuilder &builder)
static void fn_node_slice_string_declare(NodeDeclarationBuilder &b)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_fn_slice_string()
void fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition: sort.c:35
Defines a node type.
Definition: BKE_node.h:226
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)