Skip to content

Commit 3be46a6

Browse files
committed
Fix uppercase B and X parsing in the integer literals.
1 parent c394eaa commit 3be46a6

File tree

8 files changed

+31
-19
lines changed

8 files changed

+31
-19
lines changed

core/math/expression.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ Error Expression::_get_token(Token &r_token) {
350350
case READING_INT: {
351351
if (is_digit(c)) {
352352
if (is_first_char && c == '0') {
353-
if (next_char == 'b') {
353+
if (next_char == 'b' || next_char == 'B') {
354354
reading = READING_BIN;
355-
} else if (next_char == 'x') {
355+
} else if (next_char == 'x' || next_char == 'X') {
356356
reading = READING_HEX;
357357
}
358358
}
@@ -370,15 +370,15 @@ Error Expression::_get_token(Token &r_token) {
370370
case READING_BIN: {
371371
if (bin_beg && !is_binary_digit(c)) {
372372
reading = READING_DONE;
373-
} else if (c == 'b') {
373+
} else if (c == 'b' || c == 'B') {
374374
bin_beg = true;
375375
}
376376

377377
} break;
378378
case READING_HEX: {
379379
if (hex_beg && !is_hex_digit(c)) {
380380
reading = READING_DONE;
381-
} else if (c == 'x') {
381+
} else if (c == 'x' || c == 'X') {
382382
hex_beg = true;
383383
}
384384

editor/import/resource_importer_imagefont.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Error ResourceImporterImageFont::import(ResourceUID::ID p_source_id, const Strin
159159
c++; // Skip "+".
160160
continue;
161161
}
162-
} else if (range[c] == '0' && (c <= range.length() - 2) && range[c + 1] == 'x') {
162+
} else if (range[c] == '0' && (c <= range.length() - 2) && (range[c + 1] == 'x' || range[c + 1] == 'X')) {
163163
// Read hexadecimal value, start.
164164
token = String();
165165
if (step == STEP_START_BEGIN) {

modules/gdscript/editor/gdscript_highlighter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,12 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
351351

352352
// Special cases for numbers.
353353
if (in_number && !is_a_digit) {
354-
if (str[j] == 'b' && str[j - 1] == '0') {
354+
if ((str[j] == 'b' || str[j] == 'B') && str[j - 1] == '0') {
355355
is_bin_notation = true;
356-
} else if (str[j] == 'x' && str[j - 1] == '0') {
356+
} else if ((str[j] == 'x' || str[j] == 'X') && str[j - 1] == '0') {
357357
is_hex_notation = true;
358358
} else if (!((str[j] == '-' || str[j] == '+') && str[j - 1] == 'e' && !prev_is_digit) &&
359-
!(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'x' || str[j - 1] == '.')) &&
359+
!(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'B' || str[j - 1] == 'x' || str[j - 1] == 'X' || str[j - 1] == '.')) &&
360360
!(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] == '~'))))) &&
362362
!((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e')) {

modules/gdscript/gdscript_tokenizer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,13 @@ GDScriptTokenizer::Token GDScriptTokenizerText::number() {
696696
if (_peek(-1) == '.') {
697697
has_decimal = true;
698698
} else if (_peek(-1) == '0') {
699-
if (_peek() == 'x') {
699+
if (_peek() == 'x' || _peek() == 'X') {
700700
// Hexadecimal.
701701
base = 16;
702702
digit_check_func = is_hex_digit;
703703
need_digits = true;
704704
_advance();
705-
} else if (_peek() == 'b') {
705+
} else if (_peek() == 'b' || _peek() == 'B') {
706706
// Binary.
707707
base = 2;
708708
digit_check_func = is_binary_digit;

modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ public static bool IsValidHexNumber(this string instance, bool withPrefix = fals
979979
{
980980
if (instance.Length < 3)
981981
return false;
982-
if (instance[from] != '0' || instance[from + 1] != 'x')
982+
if (instance[from] != '0' || instance[from + 1] != 'x' || instance[from + 1] != 'X')
983983
return false;
984984
from += 2;
985985
}

scene/resources/syntax_highlighter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ 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] == 'X' || str[j] == '_' || str[j] == 'f' || 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;
309309

310-
if (str[j] == 'x' && str[j - 1] == '0') {
310+
if ((str[j] == 'x' || str[j] == 'X') && str[j - 1] == '0') {
311311
is_hex_notation = true;
312312
}
313313
}

tests/core/math/test_expression.h

+9
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ TEST_CASE("[Expression] Underscored numeric literals") {
213213
CHECK_MESSAGE(
214214
expression.parse("0xff_99_00") == OK,
215215
"The expression should parse successfully.");
216+
CHECK_MESSAGE(
217+
expression.parse("0Xff_99_00") == OK,
218+
"The expression should parse successfully.");
219+
CHECK_MESSAGE(
220+
expression.parse("0b10_11_00") == OK,
221+
"The expression should parse successfully.");
222+
CHECK_MESSAGE(
223+
expression.parse("0B10_11_00") == OK,
224+
"The expression should parse successfully.");
216225
}
217226

218227
TEST_CASE("[Expression] Built-in functions") {

tests/core/string/test_string.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ TEST_CASE("[String] String to integer") {
545545
CHECK(String(nums[i]).to_int() == num[i]);
546546
}
547547
CHECK(String("0b1011").to_int() == 1011); // Looks like a binary number, but to_int() handles this as a base-10 number, "b" is just ignored.
548+
CHECK(String("0B1011").to_int() == 1011);
549+
548550
CHECK(String("0x1012").to_int() == 1012); // Looks like a hexadecimal number, but to_int() handles this as a base-10 number, "x" is just ignored.
551+
CHECK(String("0X1012").to_int() == 1012);
549552

550553
ERR_PRINT_OFF
551554
CHECK(String("999999999999999999999999999999999999999999999999999999999").to_int() == INT64_MAX); // Too large, largest possible is returned.
@@ -554,10 +557,10 @@ TEST_CASE("[String] String to integer") {
554557
}
555558

556559
TEST_CASE("[String] Hex to integer") {
557-
static const char *nums[12] = { "0xFFAE", "22", "0", "AADDAD", "0x7FFFFFFFFFFFFFFF", "-0xf", "", "000", "000f", "0xaA", "-ff", "-" };
558-
static const int64_t num[12] = { 0xFFAE, 0x22, 0, 0xAADDAD, 0x7FFFFFFFFFFFFFFF, -0xf, 0, 0, 0xf, 0xaa, -0xff, 0x0 };
560+
static const char *nums[13] = { "0xFFAE", "22", "0", "AADDAD", "0x7FFFFFFFFFFFFFFF", "-0xf", "", "000", "000f", "0xaA", "-ff", "-", "0XFFAE" };
561+
static const int64_t num[13] = { 0xFFAE, 0x22, 0, 0xAADDAD, 0x7FFFFFFFFFFFFFFF, -0xf, 0, 0, 0xf, 0xaa, -0xff, 0x0, 0xFFAE };
559562

560-
for (int i = 0; i < 12; i++) {
563+
for (int i = 0; i < 13; i++) {
561564
CHECK(String(nums[i]).hex_to_int() == num[i]);
562565
}
563566

@@ -575,10 +578,10 @@ TEST_CASE("[String] Hex to integer") {
575578
}
576579

577580
TEST_CASE("[String] Bin to integer") {
578-
static const char *nums[10] = { "", "0", "0b0", "0b1", "0b", "1", "0b1010", "-0b11", "-1010", "0b0111111111111111111111111111111111111111111111111111111111111111" };
579-
static const int64_t num[10] = { 0, 0, 0, 1, 0, 1, 10, -3, -10, 0x7FFFFFFFFFFFFFFF };
581+
static const char *nums[11] = { "", "0", "0b0", "0b1", "0b", "1", "0b1010", "-0b11", "-1010", "0b0111111111111111111111111111111111111111111111111111111111111111", "0B1010" };
582+
static const int64_t num[11] = { 0, 0, 0, 1, 0, 1, 10, -3, -10, 0x7FFFFFFFFFFFFFFF, 10 };
580583

581-
for (int i = 0; i < 10; i++) {
584+
for (int i = 0; i < 11; i++) {
582585
CHECK(String(nums[i]).bin_to_int() == num[i]);
583586
}
584587

0 commit comments

Comments
 (0)