Skip to content

Commit 88926a2

Browse files
committed
Fix is_valid_float, Variant parser, Expression parser, script highlighter, and TextServer not handing capital E in scientific notation.
1 parent eee39f0 commit 88926a2

File tree

10 files changed

+52
-35
lines changed

10 files changed

+52
-35
lines changed

core/math/expression.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ Error Expression::_get_token(Token &r_token) {
359359
} else if (c == '.') {
360360
reading = READING_DEC;
361361
is_float = true;
362-
} else if (c == 'e') {
362+
} else if (c == 'e' || c == 'E') {
363363
reading = READING_EXP;
364364
is_float = true;
365365
} else {
@@ -385,7 +385,7 @@ Error Expression::_get_token(Token &r_token) {
385385
} break;
386386
case READING_DEC: {
387387
if (is_digit(c)) {
388-
} else if (c == 'e') {
388+
} else if (c == 'e' || c == 'E') {
389389
reading = READING_EXP;
390390
} else {
391391
reading = READING_DONE;

core/string/ustring.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -4964,17 +4964,18 @@ bool String::is_valid_float() const {
49644964
bool numbers_found = false;
49654965

49664966
for (int i = from; i < len; i++) {
4967-
if (is_digit(operator[](i))) {
4967+
const char32_t c = operator[](i);
4968+
if (is_digit(c)) {
49684969
if (exponent_found) {
49694970
exponent_values_found = true;
49704971
} else {
49714972
numbers_found = true;
49724973
}
4973-
} else if (numbers_found && !exponent_found && operator[](i) == 'e') {
4974+
} else if (numbers_found && !exponent_found && (c == 'e' || c == 'E')) {
49744975
exponent_found = true;
4975-
} else if (!period_found && !exponent_found && operator[](i) == '.') {
4976+
} else if (!period_found && !exponent_found && c == '.') {
49764977
period_found = true;
4977-
} else if ((operator[](i) == '-' || operator[](i) == '+') && exponent_found && !exponent_values_found && !sign_found) {
4978+
} else if ((c == '-' || c == '+') && exponent_found && !exponent_values_found && !sign_found) {
49784979
sign_found = true;
49794980
} else {
49804981
return false; // no start with number plz

core/variant/variant_parser.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
441441
} else if (c == '.') {
442442
reading = READING_DEC;
443443
is_float = true;
444-
} else if (c == 'e') {
444+
} else if (c == 'e' || c == 'E') {
445445
reading = READING_EXP;
446446
is_float = true;
447447
} else {
@@ -451,7 +451,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
451451
} break;
452452
case READING_DEC: {
453453
if (is_digit(c)) {
454-
} else if (c == 'e') {
454+
} else if (c == 'e' || c == 'E') {
455455
reading = READING_EXP;
456456
} else {
457457
reading = READING_DONE;
@@ -1962,7 +1962,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
19621962
case Variant::FLOAT: {
19631963
String s = rtos_fix(p_variant.operator double());
19641964
if (s != "inf" && s != "inf_neg" && s != "nan") {
1965-
if (!s.contains_char('.') && !s.contains_char('e')) {
1965+
if (!s.contains_char('.') && !s.contains_char('e') && !s.contains_char('E')) {
19661966
s += ".0";
19671967
}
19681968
}

modules/gdscript/editor/gdscript_highlighter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,11 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
355355
is_bin_notation = true;
356356
} else if (str[j] == 'x' && str[j - 1] == '0') {
357357
is_hex_notation = true;
358-
} else if (!((str[j] == '-' || str[j] == '+') && str[j - 1] == 'e' && !prev_is_digit) &&
358+
} else if (!((str[j] == '-' || str[j] == '+') && (str[j - 1] == 'e' || str[j - 1] == 'E') && !prev_is_digit) &&
359359
!(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'x' || str[j - 1] == '.')) &&
360-
!(str[j] == 'e' && (prev_is_digit || str[j - 1] == '_')) &&
360+
!((str[j] == 'e' || str[j] == 'E') && (prev_is_digit || str[j - 1] == '_')) &&
361361
!(str[j] == '.' && (prev_is_digit || (!prev_is_binary_op && (j > 0 && (str[j - 1] == '_' || str[j - 1] == '-' || str[j - 1] == '+' || str[j - 1] == '~'))))) &&
362-
!((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e')) {
362+
!((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e' && str[j - 1] != 'E')) {
363363
/* This condition continues number highlighting in special cases.
364364
1st row: '+' or '-' after scientific notation (like 3e-4);
365365
2nd row: '_' as a numeric separator;

modules/gdscript/tests/scripts/parser/features/constants.gd

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ func test():
44
const _VECTOR = Vector2(5, 6)
55
const _ARRAY = []
66
const _DICTIONARY = {"this": "dictionary"}
7+
const _FLOAT1 = 1e2
8+
const _FLOAT2 = 1E2
79

810
# Create user constants from built-in constants.
911
const _HELLO = PI + TAU

modules/text_server_adv/text_server_adv.cpp

+22-12
Original file line numberDiff line numberDiff line change
@@ -6878,7 +6878,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
68786878
ar.lang.insert(StringName("sd_Arab_PK"));
68796879
ar.digits = U"٠١٢٣٤٥٦٧٨٩٫";
68806880
ar.percent_sign = U"٪";
6881-
ar.exp = U"اس";
6881+
ar.exp_l = U"اس";
6882+
ar.exp_u = U"اس";
68826883
num_systems.push_back(ar);
68836884
}
68846885

@@ -6909,7 +6910,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69096910
pr.lang.insert(StringName("uz_Arab_AF"));
69106911
pr.digits = U"۰۱۲۳۴۵۶۷۸۹٫";
69116912
pr.percent_sign = U"٪";
6912-
pr.exp = U"اس";
6913+
pr.exp_l = U"اس";
6914+
pr.exp_u = U"اس";
69136915
num_systems.push_back(pr);
69146916
}
69156917

@@ -6927,7 +6929,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69276929
bn.lang.insert(StringName("mni_Beng_IN"));
69286930
bn.digits = U"০১২৩৪৫৬৭৮৯.";
69296931
bn.percent_sign = U"%";
6930-
bn.exp = U"e";
6932+
bn.exp_l = U"e";
6933+
bn.exp_u = U"E";
69316934
num_systems.push_back(bn);
69326935
}
69336936

@@ -6943,7 +6946,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69436946
mr.lang.insert(StringName("sa_IN"));
69446947
mr.digits = U"०१२३४५६७८९.";
69456948
mr.percent_sign = U"%";
6946-
mr.exp = U"e";
6949+
mr.exp_l = U"e";
6950+
mr.exp_u = U"E";
69476951
num_systems.push_back(mr);
69486952
}
69496953

@@ -6954,7 +6958,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69546958
dz.lang.insert(StringName("dz_BT"));
69556959
dz.digits = U"༠༡༢༣༤༥༦༧༨༩.";
69566960
dz.percent_sign = U"%";
6957-
dz.exp = U"e";
6961+
dz.exp_l = U"e";
6962+
dz.exp_u = U"E";
69586963
num_systems.push_back(dz);
69596964
}
69606965

@@ -6967,7 +6972,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69676972
sat.lang.insert(StringName("sat_Olck_IN"));
69686973
sat.digits = U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.";
69696974
sat.percent_sign = U"%";
6970-
sat.exp = U"e";
6975+
sat.exp_l = U"e";
6976+
sat.exp_u = U"E";
69716977
num_systems.push_back(sat);
69726978
}
69736979

@@ -6978,7 +6984,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69786984
my.lang.insert(StringName("my_MM"));
69796985
my.digits = U"၀၁၂၃၄၅၆၇၈၉.";
69806986
my.percent_sign = U"%";
6981-
my.exp = U"e";
6987+
my.exp_l = U"e";
6988+
my.exp_u = U"E";
69826989
num_systems.push_back(my);
69836990
}
69846991

@@ -6990,7 +6997,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69906997
ccp.lang.insert(StringName("ccp_IN"));
69916998
ccp.digits = U"𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑄿.";
69926999
ccp.percent_sign = U"%";
6993-
ccp.exp = U"e";
7000+
ccp.exp_l = U"e";
7001+
ccp.exp_u = U"E";
69947002
num_systems.push_back(ccp);
69957003
}
69967004

@@ -7012,7 +7020,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
70127020
ff.lang.insert(StringName("ff_Adlm_SN"));
70137021
ff.digits = U"𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘𞥙.";
70147022
ff.percent_sign = U"%";
7015-
ff.exp = U"e";
7023+
ff.exp_l = U"𞤉";
7024+
ff.exp_u = U"𞤉";
70167025
num_systems.push_back(ff);
70177026
}
70187027
}
@@ -7026,8 +7035,8 @@ String TextServerAdvanced::_format_number(const String &p_string, const String &
70267035
if (num_systems[i].digits.is_empty()) {
70277036
return p_string;
70287037
}
7029-
res.replace("e", num_systems[i].exp);
7030-
res.replace("E", num_systems[i].exp);
7038+
res = res.replace("e", num_systems[i].exp_l);
7039+
res = res.replace("E", num_systems[i].exp_u);
70317040
char32_t *data = res.ptrw();
70327041
for (int j = 0; j < res.length(); j++) {
70337042
if (data[j] >= 0x30 && data[j] <= 0x39) {
@@ -7051,7 +7060,8 @@ String TextServerAdvanced::_parse_number(const String &p_string, const String &p
70517060
if (num_systems[i].digits.is_empty()) {
70527061
return p_string;
70537062
}
7054-
res.replace(num_systems[i].exp, "e");
7063+
res = res.replace(num_systems[i].exp_l, "e");
7064+
res = res.replace(num_systems[i].exp_u, "E");
70557065
char32_t *data = res.ptrw();
70567066
for (int j = 0; j < res.length(); j++) {
70577067
if (data[j] == num_systems[i].digits[10]) {

modules/text_server_adv/text_server_adv.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class TextServerAdvanced : public TextServerExtension {
147147
HashSet<StringName> lang;
148148
String digits;
149149
String percent_sign;
150-
String exp;
150+
String exp_l;
151+
String exp_u;
151152
};
152153

153154
Vector<NumSystemData> num_systems;

scene/resources/syntax_highlighter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ Dictionary CodeHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
302302
}
303303

304304
// Check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation.
305-
if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f' || str[j] == 'e' || (uint_suffix_enabled && str[j] == 'u')) && !in_word && prev_is_number && !is_number) {
305+
if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f' || str[j] == 'e' || str[j] == 'E' || (uint_suffix_enabled && str[j] == 'u')) && !in_word && prev_is_number && !is_number) {
306306
is_number = true;
307307
is_a_symbol = false;
308308
is_char = false;

tests/core/math/test_expression.h

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ TEST_CASE("[Expression] Scientific notation") {
181181
CHECK_MESSAGE(
182182
expression.parse("2.e5") == OK,
183183
"The expression should parse successfully.");
184+
CHECK_MESSAGE(
185+
expression.parse("2.E5") == OK,
186+
"The expression should parse successfully.");
184187
CHECK_MESSAGE(
185188
double(expression.execute()) == doctest::Approx(200'000),
186189
"The expression should return the expected result.");

tests/core/string/test_string.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -1885,15 +1885,15 @@ TEST_CASE("[String] Join") {
18851885
}
18861886

18871887
TEST_CASE("[String] Is_*") {
1888-
static const char *data[13] = { "-30", "100", "10.1", "10,1", "1e2", "1e-2", "1e2e3", "0xAB", "AB", "Test1", "1Test", "Test*1", "文字" };
1889-
static bool isnum[13] = { true, true, true, false, false, false, false, false, false, false, false, false, false };
1890-
static bool isint[13] = { true, true, false, false, false, false, false, false, false, false, false, false, false };
1891-
static bool ishex[13] = { true, true, false, false, true, false, true, false, true, false, false, false, false };
1892-
static bool ishex_p[13] = { false, false, false, false, false, false, false, true, false, false, false, false, false };
1893-
static bool isflt[13] = { true, true, true, false, true, true, false, false, false, false, false, false, false };
1894-
static bool isaid[13] = { false, false, false, false, false, false, false, false, true, true, false, false, false };
1895-
static bool isuid[13] = { false, false, false, false, false, false, false, false, true, true, false, false, true };
1896-
for (int i = 0; i < 12; i++) {
1888+
static const char *data[] = { "-30", "100", "10.1", "10,1", "1e2", "1e-2", "1e2e3", "0xAB", "AB", "Test1", "1Test", "Test*1", "文字", "1E2", "1E-2" };
1889+
static bool isnum[] = { true, true, true, false, false, false, false, false, false, false, false, false, false, false, false };
1890+
static bool isint[] = { true, true, false, false, false, false, false, false, false, false, false, false, false, false, false };
1891+
static bool ishex[] = { true, true, false, false, true, false, true, false, true, false, false, false, false, true, false };
1892+
static bool ishex_p[] = { false, false, false, false, false, false, false, true, false, false, false, false, false, false, false };
1893+
static bool isflt[] = { true, true, true, false, true, true, false, false, false, false, false, false, false, true, true };
1894+
static bool isaid[] = { false, false, false, false, false, false, false, false, true, true, false, false, false, false, false };
1895+
static bool isuid[] = { false, false, false, false, false, false, false, false, true, true, false, false, true, false, false };
1896+
for (unsigned int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
18971897
String s = String::utf8(data[i]);
18981898
CHECK(s.is_numeric() == isnum[i]);
18991899
CHECK(s.is_valid_int() == isint[i]);

0 commit comments

Comments
 (0)