Skip to content

Commit c2b9167

Browse files
committed
Merge pull request #82786 from theraot/donotreplacestartingdigitwithunderscore
Do not replace starting digit with underscore when making identifier
2 parents 57256d7 + 5cd7ca0 commit c2b9167

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

core/string/ustring.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,24 +3974,22 @@ bool String::is_absolute_path() const {
39743974
}
39753975
}
39763976

3977-
static _FORCE_INLINE_ bool _is_valid_identifier_bit(int p_index, char32_t p_char) {
3978-
if (p_index == 0 && is_digit(p_char)) {
3979-
return false; // No start with number plz.
3980-
}
3981-
return is_ascii_identifier_char(p_char);
3982-
}
3983-
39843977
String String::validate_identifier() const {
39853978
if (is_empty()) {
39863979
return "_"; // Empty string is not a valid identifier;
39873980
}
39883981

3989-
String result = *this;
3982+
String result;
3983+
if (is_digit(operator[](0))) {
3984+
result = "_" + *this;
3985+
} else {
3986+
result = *this;
3987+
}
3988+
39903989
int len = result.length();
39913990
char32_t *buffer = result.ptrw();
3992-
39933991
for (int i = 0; i < len; i++) {
3994-
if (!_is_valid_identifier_bit(i, buffer[i])) {
3992+
if (!is_ascii_identifier_char(buffer[i])) {
39953993
buffer[i] = '_';
39963994
}
39973995
}
@@ -4006,10 +4004,14 @@ bool String::is_valid_identifier() const {
40064004
return false;
40074005
}
40084006

4007+
if (is_digit(operator[](0))) {
4008+
return false;
4009+
}
4010+
40094011
const char32_t *str = &operator[](0);
40104012

40114013
for (int i = 0; i < len; i++) {
4012-
if (!_is_valid_identifier_bit(i, str[i])) {
4014+
if (!is_ascii_identifier_char(str[i])) {
40134015
return false;
40144016
}
40154017
}

tests/core/string/test_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ TEST_CASE("[String] validate_identifier") {
17161716
CHECK(empty_string.validate_identifier() == "_");
17171717

17181718
String numeric_only = "12345";
1719-
CHECK(numeric_only.validate_identifier() == "_2345");
1719+
CHECK(numeric_only.validate_identifier() == "_12345");
17201720

17211721
String name_with_spaces = "Name with spaces";
17221722
CHECK(name_with_spaces.validate_identifier() == "Name_with_spaces");

0 commit comments

Comments
 (0)