Blender V5.3
node_fn_trim_string.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <algorithm>
6
7#include "BLI_string_utf8.hh"
8
10#include "node_shader_util.hh"
11
13
15{
16 b.is_function_node();
17 b.use_custom_socket_order();
18 b.allow_any_socket_order();
19 b.add_input<decl::String>("String"_ustr).optional_label();
20 b.add_output<decl::String>("String"_ustr).align_with_previous();
21 b.add_input<decl::String>("Characters"_ustr)
22 .optional_label()
23 .description("Individual characters to trim. The order of characters does not matter");
24 b.add_input<decl::Bool>("Whitespace"_ustr)
25 .default_value(true)
26 .description("Trim whitespace characters in addition to the provided characters");
27 {
28 auto &p = b.add_panel("Limit"_ustr).default_closed(true);
29 p.add_input<decl::Bool>("Start"_ustr)
30 .default_value(true)
31 .description("Trim the beginning of the string");
32 p.add_input<decl::Bool>("End"_ustr)
33 .default_value(true)
34 .description("Trim at the end of the string");
35 }
36}
37
39{
40 static auto trim_fn = mf::build::SI5_SO<std::string, std::string, bool, bool, bool, std::string>(
41 "Trim",
42 [](const std::string &input_str,
43 const std::string &characters,
44 const bool trim_whitespace,
45 const bool trim_start,
46 const bool trim_end) {
47 std::string characters_to_trim = characters;
48 if (trim_whitespace) {
49 characters_to_trim.append(" \t\n\r");
50 }
51 const StringRef str = input_str;
52 const StringRef chars = characters_to_trim;
53
54 /* Need to check utf-8 characters for trimming, not individual bytes. */
55 const auto is_character_trimmed = [&](const StringRef query) {
56 return chars.find(query) != StringRef::not_found;
57 };
58
59 int64_t start = 0;
60 int64_t end = str.size();
61 if (trim_start) {
62 while (start < end) {
63 const int64_t size = std::min<int64_t>(BLI_str_utf8_size_safe(str.data() + start),
64 end - start);
65 if (!is_character_trimmed(str.substr(start, size))) {
66 break;
67 }
68 start += size;
69 }
70 }
71 if (trim_end) {
72 while (end > start) {
73 const char *prev = std::max(
74 BLI_str_find_prev_char_utf8(str.data() + end, str.data() + start),
75 str.data() + start);
76 const int64_t size = str.data() + end - prev;
77 if (!is_character_trimmed(str.substr(end - size, size))) {
78 break;
79 }
80 end -= size;
81 }
82 }
83 std::string result = str.substr(start, end - start);
84 return result;
85 });
86 builder.set_matching_fn(&trim_fn);
87}
88
89static void node_register()
90{
91 static bke::bNodeType ntype;
92
93 common_node_type_base(&ntype, "FunctionNodeTrimString"_ustr);
94 ntype.ui_name = "Trim String";
95 ntype.ui_description = "Remove characters from the beginning and end of a string";
97 ntype.declare = node_declare;
100}
102
103} // namespace blender::nodes::node_fn_trim_string_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NOD_REGISTER_NODE(REGISTER_FUNC)
static constexpr int64_t not_found
constexpr int64_t find(char c, int64_t pos=0) const
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)
__int64 int64_t
Definition BKE_text.h:90
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL 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