Fix uppercase B and X parsing in the integer literals.

This commit is contained in:
bruvzg
2025-02-04 09:51:10 +02:00
committed by Pāvels Nadtočajevs
parent c394eaa45c
commit 3be46a69c4
8 changed files with 31 additions and 19 deletions

View File

@ -213,6 +213,15 @@ TEST_CASE("[Expression] Underscored numeric literals") {
CHECK_MESSAGE(
expression.parse("0xff_99_00") == OK,
"The expression should parse successfully.");
CHECK_MESSAGE(
expression.parse("0Xff_99_00") == OK,
"The expression should parse successfully.");
CHECK_MESSAGE(
expression.parse("0b10_11_00") == OK,
"The expression should parse successfully.");
CHECK_MESSAGE(
expression.parse("0B10_11_00") == OK,
"The expression should parse successfully.");
}
TEST_CASE("[Expression] Built-in functions") {

View File

@ -545,7 +545,10 @@ TEST_CASE("[String] String to integer") {
CHECK(String(nums[i]).to_int() == num[i]);
}
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.
CHECK(String("0B1011").to_int() == 1011);
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.
CHECK(String("0X1012").to_int() == 1012);
ERR_PRINT_OFF
CHECK(String("999999999999999999999999999999999999999999999999999999999").to_int() == INT64_MAX); // Too large, largest possible is returned.
@ -554,10 +557,10 @@ TEST_CASE("[String] String to integer") {
}
TEST_CASE("[String] Hex to integer") {
static const char *nums[12] = { "0xFFAE", "22", "0", "AADDAD", "0x7FFFFFFFFFFFFFFF", "-0xf", "", "000", "000f", "0xaA", "-ff", "-" };
static const int64_t num[12] = { 0xFFAE, 0x22, 0, 0xAADDAD, 0x7FFFFFFFFFFFFFFF, -0xf, 0, 0, 0xf, 0xaa, -0xff, 0x0 };
static const char *nums[13] = { "0xFFAE", "22", "0", "AADDAD", "0x7FFFFFFFFFFFFFFF", "-0xf", "", "000", "000f", "0xaA", "-ff", "-", "0XFFAE" };
static const int64_t num[13] = { 0xFFAE, 0x22, 0, 0xAADDAD, 0x7FFFFFFFFFFFFFFF, -0xf, 0, 0, 0xf, 0xaa, -0xff, 0x0, 0xFFAE };
for (int i = 0; i < 12; i++) {
for (int i = 0; i < 13; i++) {
CHECK(String(nums[i]).hex_to_int() == num[i]);
}
@ -575,10 +578,10 @@ TEST_CASE("[String] Hex to integer") {
}
TEST_CASE("[String] Bin to integer") {
static const char *nums[10] = { "", "0", "0b0", "0b1", "0b", "1", "0b1010", "-0b11", "-1010", "0b0111111111111111111111111111111111111111111111111111111111111111" };
static const int64_t num[10] = { 0, 0, 0, 1, 0, 1, 10, -3, -10, 0x7FFFFFFFFFFFFFFF };
static const char *nums[11] = { "", "0", "0b0", "0b1", "0b", "1", "0b1010", "-0b11", "-1010", "0b0111111111111111111111111111111111111111111111111111111111111111", "0B1010" };
static const int64_t num[11] = { 0, 0, 0, 1, 0, 1, 10, -3, -10, 0x7FFFFFFFFFFFFFFF, 10 };
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 11; i++) {
CHECK(String(nums[i]).bin_to_int() == num[i]);
}