Skip to content

Commit

Permalink
MFH: Avoid 2 conversions when decoding numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Wilmas committed Mar 19, 2009
1 parent 021e5d1 commit 91d325d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions ext/json/JSON_parser.c
Expand Up @@ -289,16 +289,27 @@ static void json_create_zval(zval **z, smart_str *buf, int type)

if (type == IS_LONG)
{
long l = strtol(buf->c, NULL, 10);
double d = zend_strtod(buf->c, NULL);
if (d > LONG_MAX || d < LONG_MIN) {
ZVAL_DOUBLE(*z, d);
} else {
ZVAL_LONG(*z, l);
if (buf->c[0] == '-') {
buf->len--;
}

if (buf->len >= MAX_LENGTH_OF_LONG - 1) {
if (buf->len == MAX_LENGTH_OF_LONG - 1) {
int cmp = strcmp(buf->c + (buf->c[0] == '-'), long_min_digits);

if (!(cmp < 0 || (cmp == 0 && buf->c[0] == '-'))) {
goto use_double;
}
} else {
goto use_double;
}
}

ZVAL_LONG(*z, strtol(buf->c, NULL, 10));
}
else if (type == IS_DOUBLE)
{
use_double:
ZVAL_DOUBLE(*z, zend_strtod(buf->c, NULL));
}
else if (type == IS_STRING)
Expand Down

0 comments on commit 91d325d

Please sign in to comment.