Add checks for valid base in String::num_int64, uint64().

- Ensure String::num_int64, uint64 returns an empty string for bases less than 2 or greater than 36.
- Added corresponding test cases to verify the behavior.
- Error messages are printed when invalid bases are encountered. These messages are suppressed in the test output.
This commit is contained in:
yeojunh 2024-10-19 22:13:14 -07:00
parent 44fa552343
commit b3b24ded19
2 changed files with 21 additions and 1 deletions

View file

@ -1850,6 +1850,8 @@ String String::num(double p_num, int p_decimals) {
} }
String String::num_int64(int64_t p_num, int base, bool capitalize_hex) { String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
ERR_FAIL_COND_V_MSG(base < 2 || base > 36, "", "Cannot convert to base " + itos(base) + ", since the value is " + (base < 2 ? "less than 2." : "greater than 36."));
bool sign = p_num < 0; bool sign = p_num < 0;
int64_t n = p_num; int64_t n = p_num;
@ -1888,6 +1890,8 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
} }
String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) { String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
ERR_FAIL_COND_V_MSG(base < 2 || base > 36, "", "Cannot convert to base " + itos(base) + ", since the value is " + (base < 2 ? "less than 2." : "greater than 36."));
uint64_t n = p_num; uint64_t n = p_num;
int chars = 0; int chars = 0;

View file

@ -460,11 +460,27 @@ TEST_CASE("[String] Number to string") {
CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero. CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero.
CHECK(String::num(3.141593) == "3.141593"); CHECK(String::num(3.141593) == "3.141593");
CHECK(String::num(3.141593, 3) == "3.142"); CHECK(String::num(3.141593, 3) == "3.142");
CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros.
CHECK(String::num_scientific(30000000) == "3e+07"); CHECK(String::num_scientific(30000000) == "3e+07");
// String::num_int64 tests.
CHECK(String::num_int64(3141593) == "3141593"); CHECK(String::num_int64(3141593) == "3141593");
CHECK(String::num_int64(-3141593) == "-3141593");
CHECK(String::num_int64(0xA141593, 16) == "a141593"); CHECK(String::num_int64(0xA141593, 16) == "a141593");
CHECK(String::num_int64(0xA141593, 16, true) == "A141593"); CHECK(String::num_int64(0xA141593, 16, true) == "A141593");
CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros. ERR_PRINT_OFF;
CHECK(String::num_int64(3141593, 1) == ""); // Invalid base < 2.
CHECK(String::num_int64(3141593, 37) == ""); // Invalid base > 36.
ERR_PRINT_ON;
// String::num_uint64 tests.
CHECK(String::num_uint64(4294967295) == "4294967295");
CHECK(String::num_uint64(0xF141593, 16) == "f141593");
CHECK(String::num_uint64(0xF141593, 16, true) == "F141593");
ERR_PRINT_OFF;
CHECK(String::num_uint64(4294967295, 1) == ""); // Invalid base < 2.
CHECK(String::num_uint64(4294967295, 37) == ""); // Invalid base > 36.
ERR_PRINT_ON;
// String::num_real tests. // String::num_real tests.
CHECK(String::num_real(1.0) == "1.0"); CHECK(String::num_real(1.0) == "1.0");