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()`
This commit is contained in:
Haoyu Qiu
2024-08-23 14:30:51 +08:00
parent db76de5de8
commit 8bf4ecc026
22 changed files with 132 additions and 60 deletions

View File

@@ -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();

View File

@@ -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
*/