Skip to content

Commit dc19b1a

Browse files
committed
Improve number parsing in JSON handling
Refactor number parsing to differentiate between integers and floats to match the behaviour of the stringify method.
1 parent 0870525 commit dc19b1a

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

core/io/json.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,26 @@ Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_to
384384

385385
if (p_str[index] == '-' || is_digit(p_str[index])) {
386386
//a number
387-
const char32_t *rptr;
388-
double number = String::to_float(&p_str[index], &rptr);
389-
index += (rptr - &p_str[index]);
390-
r_token.type = TK_NUMBER;
391-
r_token.value = number;
387+
int sub_index = index;
388+
while (is_digit(p_str[sub_index])) {
389+
sub_index++;
390+
}
391+
392+
if (p_str[sub_index] == 'e' || p_str[sub_index] == 'E' || p_str[sub_index] == '.') {
393+
// float detected
394+
const char32_t *rptr;
395+
const double number = String::to_float(&p_str[index], &rptr);
396+
index += (rptr - &p_str[index]);
397+
r_token.type = TK_NUMBER;
398+
r_token.value = number;
399+
} else {
400+
// int detected
401+
--sub_index;
402+
const int64_t number = String::to_int(&p_str[index], sub_index - index);
403+
index = sub_index;
404+
r_token.type = TK_NUMBER;
405+
r_token.value = number;
406+
}
392407
return OK;
393408

394409
} else if (is_ascii_alphabet_char(p_str[index])) {

0 commit comments

Comments
 (0)