Skip to content

Commit b50d974

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

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] == '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] == 'B' || str[j - 1] == 'x' || 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
@@ -6881,7 +6881,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
68816881
ar.lang.insert(StringName("sd_Arab_PK"));
68826882
ar.digits = U"٠١٢٣٤٥٦٧٨٩٫";
68836883
ar.percent_sign = U"٪";
6884-
ar.exp = U"اس";
6884+
ar.exp_l = U"اس";
6885+
ar.exp_u = U"اس";
68856886
num_systems.push_back(ar);
68866887
}
68876888

@@ -6912,7 +6913,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69126913
pr.lang.insert(StringName("uz_Arab_AF"));
69136914
pr.digits = U"۰۱۲۳۴۵۶۷۸۹٫";
69146915
pr.percent_sign = U"٪";
6915-
pr.exp = U"اس";
6916+
pr.exp_l = U"اس";
6917+
pr.exp_u = U"اس";
69166918
num_systems.push_back(pr);
69176919
}
69186920

@@ -6930,7 +6932,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69306932
bn.lang.insert(StringName("mni_Beng_IN"));
69316933
bn.digits = U"০১২৩৪৫৬৭৮৯.";
69326934
bn.percent_sign = U"%";
6933-
bn.exp = U"e";
6935+
bn.exp_l = U"e";
6936+
bn.exp_u = U"E";
69346937
num_systems.push_back(bn);
69356938
}
69366939

@@ -6946,7 +6949,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69466949
mr.lang.insert(StringName("sa_IN"));
69476950
mr.digits = U"०१२३४५६७८९.";
69486951
mr.percent_sign = U"%";
6949-
mr.exp = U"e";
6952+
mr.exp_l = U"e";
6953+
mr.exp_u = U"E";
69506954
num_systems.push_back(mr);
69516955
}
69526956

@@ -6957,7 +6961,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69576961
dz.lang.insert(StringName("dz_BT"));
69586962
dz.digits = U"༠༡༢༣༤༥༦༧༨༩.";
69596963
dz.percent_sign = U"%";
6960-
dz.exp = U"e";
6964+
dz.exp_l = U"e";
6965+
dz.exp_u = U"E";
69616966
num_systems.push_back(dz);
69626967
}
69636968

@@ -6970,7 +6975,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69706975
sat.lang.insert(StringName("sat_Olck_IN"));
69716976
sat.digits = U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.";
69726977
sat.percent_sign = U"%";
6973-
sat.exp = U"e";
6978+
sat.exp_l = U"e";
6979+
sat.exp_u = U"E";
69746980
num_systems.push_back(sat);
69756981
}
69766982

@@ -6981,7 +6987,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69816987
my.lang.insert(StringName("my_MM"));
69826988
my.digits = U"၀၁၂၃၄၅၆၇၈၉.";
69836989
my.percent_sign = U"%";
6984-
my.exp = U"e";
6990+
my.exp_l = U"e";
6991+
my.exp_u = U"E";
69856992
num_systems.push_back(my);
69866993
}
69876994

@@ -6993,7 +7000,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
69937000
ccp.lang.insert(StringName("ccp_IN"));
69947001
ccp.digits = U"𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑄿.";
69957002
ccp.percent_sign = U"%";
6996-
ccp.exp = U"e";
7003+
ccp.exp_l = U"e";
7004+
ccp.exp_u = U"E";
69977005
num_systems.push_back(ccp);
69987006
}
69997007

@@ -7015,7 +7023,8 @@ void TextServerAdvanced::_insert_num_systems_lang() {
70157023
ff.lang.insert(StringName("ff_Adlm_SN"));
70167024
ff.digits = U"𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘𞥙.";
70177025
ff.percent_sign = U"%";
7018-
ff.exp = U"e";
7026+
ff.exp_l = U"𞤉";
7027+
ff.exp_u = U"𞤉";
70197028
num_systems.push_back(ff);
70207029
}
70217030
}
@@ -7029,8 +7038,8 @@ String TextServerAdvanced::_format_number(const String &p_string, const String &
70297038
if (num_systems[i].digits.is_empty()) {
70307039
return p_string;
70317040
}
7032-
res.replace("e", num_systems[i].exp);
7033-
res.replace("E", num_systems[i].exp);
7041+
res = res.replace("e", num_systems[i].exp_l);
7042+
res = res.replace("E", num_systems[i].exp_u);
70347043
char32_t *data = res.ptrw();
70357044
for (int j = 0; j < res.length(); j++) {
70367045
if (data[j] >= 0x30 && data[j] <= 0x39) {
@@ -7054,7 +7063,8 @@ String TextServerAdvanced::_parse_number(const String &p_string, const String &p
70547063
if (num_systems[i].digits.is_empty()) {
70557064
return p_string;
70567065
}
7057-
res.replace(num_systems[i].exp, "e");
7066+
res = res.replace(num_systems[i].exp_l, "e");
7067+
res = res.replace(num_systems[i].exp_u, "E");
70587068
char32_t *data = res.ptrw();
70597069
for (int j = 0; j < res.length(); j++) {
70607070
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] == '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] == '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
@@ -1888,15 +1888,15 @@ TEST_CASE("[String] Join") {
18881888
}
18891889

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

0 commit comments

Comments
 (0)