diff options
author | Haoyu Qiu <timothyqiu32@gmail.com> | 2024-08-23 14:30:51 +0800 |
---|---|---|
committer | Haoyu Qiu <timothyqiu32@gmail.com> | 2024-08-27 11:34:08 +0800 |
commit | 8bf4ecc026029eb7b16645c11930b73cf642dbfa (patch) | |
tree | f681d221372ac3ae1558941b016b4af47ff7ebe3 /core/string | |
parent | db76de5de8a415b29be4c7dd84b99bd0fe260822 (diff) | |
download | redot-engine-8bf4ecc026029eb7b16645c11930b73cf642dbfa.tar.gz |
Add `String.is_valid_unicode_identifier()`
- Adds `is_valid_unicode_identifier()`
- Adds `is_valid_ascii_identifier()`
- Deprecates `is_valid_identifier()`
- Renames `validate_identifier()` to `validate_ascii_identifier()`
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/ustring.cpp | 24 | ||||
-rw-r--r-- | core/string/ustring.h | 8 |
2 files changed, 28 insertions, 4 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 303998ab50..2683addd4b 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -4624,7 +4624,7 @@ bool String::is_absolute_path() const { } } -String String::validate_identifier() const { +String String::validate_ascii_identifier() const { if (is_empty()) { return "_"; // Empty string is not a valid identifier; } @@ -4647,7 +4647,7 @@ String String::validate_identifier() const { return result; } -bool String::is_valid_identifier() const { +bool String::is_valid_ascii_identifier() const { int len = length(); if (len == 0) { @@ -4669,6 +4669,26 @@ bool String::is_valid_identifier() const { return true; } +bool String::is_valid_unicode_identifier() const { + const char32_t *str = ptr(); + int len = length(); + + if (len == 0) { + return false; // Empty string. + } + + if (!is_unicode_identifier_start(str[0])) { + return false; + } + + for (int i = 1; i < len; i++) { + if (!is_unicode_identifier_continue(str[i])) { + return false; + } + } + return true; +} + bool String::is_valid_string() const { int l = length(); const char32_t *src = get_data(); diff --git a/core/string/ustring.h b/core/string/ustring.h index 9df2d56e80..11f15031f9 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -459,10 +459,11 @@ public: // node functions static String get_invalid_node_name_characters(bool p_allow_internal = false); String validate_node_name() const; - String validate_identifier() const; + String validate_ascii_identifier() const; String validate_filename() const; - bool is_valid_identifier() const; + bool is_valid_ascii_identifier() const; + bool is_valid_unicode_identifier() const; bool is_valid_int() const; bool is_valid_float() const; bool is_valid_hex_number(bool p_with_prefix) const; @@ -470,6 +471,9 @@ public: bool is_valid_ip_address() const; bool is_valid_filename() const; + // Use `is_valid_ascii_identifier()` instead. Kept for compatibility. + bool is_valid_identifier() const { return is_valid_ascii_identifier(); } + /** * The constructors must not depend on other overloads */ |