diff options
author | Haoyu Qiu <timothyqiu32@gmail.com> | 2022-05-02 21:04:17 +0800 |
---|---|---|
committer | Haoyu Qiu <timothyqiu32@gmail.com> | 2022-05-17 11:51:22 +0800 |
commit | 3094e739f541e571fea65e3a7a14adb7ee9711b0 (patch) | |
tree | fad5b3d43bd0b629e5e2b92b49e23e64eaf55a52 /core/string | |
parent | 067c1eb923dd38bac7ad86da52d94394eeac44e5 (diff) |
Create onready variables when dropping nodes and holding Ctrl
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 35 | ||||
-rw-r--r-- | core/string/ustring.h | 1 |
2 files changed, 27 insertions, 9 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 015dfbc651..f6ad0fe3b0 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -3655,6 +3655,31 @@ bool String::is_absolute_path() const { } } +static _FORCE_INLINE_ bool _is_valid_identifier_bit(int p_index, char32_t p_char) { + if (p_index == 0 && is_digit(p_char)) { + return false; // No start with number plz. + } + return is_ascii_identifier_char(p_char); +} + +String String::validate_identifier() const { + if (is_empty()) { + return "_"; // Empty string is not a valid identifier; + } + + String result = *this; + int len = result.length(); + char32_t *buffer = result.ptrw(); + + for (int i = 0; i < len; i++) { + if (!_is_valid_identifier_bit(i, buffer[i])) { + buffer[i] = '_'; + } + } + + return result; +} + bool String::is_valid_identifier() const { int len = length(); @@ -3665,15 +3690,7 @@ bool String::is_valid_identifier() const { const char32_t *str = &operator[](0); for (int i = 0; i < len; i++) { - if (i == 0) { - if (is_digit(str[0])) { - return false; // no start with number plz - } - } - - bool valid_char = is_ascii_identifier_char(str[i]); - - if (!valid_char) { + if (!_is_valid_identifier_bit(i, str[i])) { return false; } } diff --git a/core/string/ustring.h b/core/string/ustring.h index 48f2e45105..0d10d4cf10 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -427,6 +427,7 @@ public: // node functions static const String invalid_node_name_characters; String validate_node_name() const; + String validate_identifier() const; bool is_valid_identifier() const; bool is_valid_int() const; |