Blender V5.3
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
10
12
13std::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
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
#define ELEM(...)
constexpr IndexRange()=default
constexpr bool is_empty() const
std::string variable_name_find_short(StringRef src_name, FunctionRef< bool(StringRef)> exists_fn)
std::string variable_name_validate(StringRef name)