Blender
V5.3
source
blender
nodes
intern
socket_items_name_util.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
8
9
#include "
NOD_socket_items_name_util.hh
"
10
11
namespace
blender::nodes::socket_items
{
12
13
std::string
variable_name_find_short
(
StringRef
src_name,
FunctionRef
<
bool
(
StringRef
)> exists_fn)
14
{
15
/* The goal is to find a single-letter name that is not used already. Ideally, it starts with the
16
* same letter as the given name. */
17
18
char
initial =
'a'
;
19
if
(!src_name.
is_empty
()) {
20
const
char
first_c = src_name[0];
21
if
(first_c >=
'a'
&& first_c <=
'z'
) {
22
initial = first_c;
23
}
24
else
if
(first_c >=
'A'
&& first_c <=
'Z'
) {
25
initial = first_c -
'A'
+
'a'
;
26
}
27
}
28
for
(
const
int
i :
IndexRange
(
'z'
-
'a'
+ 1)) {
29
char
c = initial + i;
30
if
(c >
'z'
) {
31
/* Start at 'a' again. */
32
c = c -
'z'
+
'a'
- 1;
33
}
34
const
std::string potential_name = std::string(1, c);
35
if
(!exists_fn(potential_name)) {
36
return
potential_name;
37
}
38
}
39
return
src_name;
40
}
41
42
std::string
variable_name_validate
(
StringRef
name)
43
{
44
/* The name has to start with a letter or underscore. The remaining letters may additionally be
45
* digits. */
46
std::string
result
;
47
if
(name.
is_empty
()) {
48
return
result
;
49
}
50
const
char
first_char = name[0];
51
if
(!std::isalpha(first_char) && first_char !=
'_'
) {
52
result
+=
'_'
;
53
}
54
for
(
const
char
c : name) {
55
if
(std::isalnum(c) || c ==
'_'
) {
56
result
+= c;
57
}
58
if
(
ELEM
(c,
'-'
,
'.'
,
' '
,
'\t'
)) {
59
result
+=
'_'
;
60
}
61
}
62
63
return
result
;
64
}
65
66
}
// namespace blender::nodes::socket_items
NOD_socket_items_name_util.hh
ELEM
#define ELEM(...)
blender::FunctionRef
Definition
BLI_function_ref.hh:72
blender::IndexRange::IndexRange
constexpr IndexRange()=default
blender::StringRef
Definition
BLI_string_ref.hh:165
blender::blender::StringRefBase::is_empty
constexpr bool is_empty() const
Definition
BLI_string_ref.hh:201
blender::nodes::socket_items
Definition
socket_items_name_util.cc:11
blender::nodes::socket_items::variable_name_find_short
std::string variable_name_find_short(StringRef src_name, FunctionRef< bool(StringRef)> exists_fn)
Definition
socket_items_name_util.cc:13
blender::nodes::socket_items::variable_name_validate
std::string variable_name_validate(StringRef name)
Definition
socket_items_name_util.cc:42
blender::result
double result
Definition
BLI_expr_pylike_eval_test.cc:353
Generated on
for Blender by
doxygen
1.18.0