Don't use strtoll() with explicit base to parse hex, instead use strtod() (#826)

`strtod()` automatically recognizes the `0x` and converts to hex

This has two advantages:
 1. Simplify code (no need of argument `isHex` to `makeNumber`, no `if`)
 2. Allow numbers larger than `long long` if they fit into `double`
This commit is contained in:
Chayim Refael Friedman
2021-01-31 07:44:52 +02:00
committed by GitHub
parent 0fa16a20ec
commit 3a06580b89

View File

@ -713,18 +713,11 @@ static int readHexDigit(Parser* parser)
}
// Parses the numeric value of the current token.
static void makeNumber(Parser* parser, bool isHex)
static void makeNumber(Parser* parser)
{
errno = 0;
if (isHex)
{
parser->next.value = NUM_VAL((double)strtoll(parser->tokenStart, NULL, 16));
}
else
{
parser->next.value = NUM_VAL(strtod(parser->tokenStart, NULL));
}
parser->current.value = NUM_VAL(strtod(parser->tokenStart, NULL));
if (errno == ERANGE)
{
@ -747,7 +740,7 @@ static void readHexNumber(Parser* parser)
// Iterate over all the valid hexadecimal digits found.
while (readHexDigit(parser) != -1) continue;
makeNumber(parser, true);
makeNumber(parser);
}
// Finishes lexing a number literal.
@ -780,7 +773,7 @@ static void readNumber(Parser* parser)
while (isDigit(peekChar(parser))) nextChar(parser);
}
makeNumber(parser, false);
makeNumber(parser);
}
// Finishes lexing an identifier. Handles reserved words.