Skip to content

Commit

Permalink
Simplify codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Dec 10, 2023
1 parent d51b401 commit fdcd4c0
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/util/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,15 @@ static double __evaluate_json_number(const char *integer,

if (*integer != '0')
{
figures++;
mant = *integer - '0';
while (isdigit(*++integer) && figures < 18)
integer++;
figures++;
while (isdigit(*integer) && figures < 18)
{
figures++;
mant *= 10;
mant += *integer - '0';
integer++;
figures++;
}

while (isdigit(*integer))
Expand All @@ -354,12 +356,11 @@ static double __evaluate_json_number(const char *integer,

while (isdigit(*fraction) && figures < 18)
{
figures++;
mant *= 10;
mant += *fraction - '0';

exp--;
fraction++;
figures++;
}

num = mant;
Expand Down Expand Up @@ -400,38 +401,44 @@ static int __parse_json_number(const char *cursor, const char **end,
if (*cursor == '0' && isdigit(cursor[1]))
return -2;

while (isdigit(*++cursor))
;
cursor++;
while (isdigit(*cursor))
cursor++;

if (*cursor == '.')
{
fraction = ++cursor;
cursor++;
fraction = cursor;
if (!isdigit(*cursor))
return -2;

while (isdigit(*++cursor))
;
cursor++;
while (isdigit(*cursor))
cursor++;
}

if (*cursor == 'E' || *cursor == 'e')
{
sign = (*++cursor == '-');
cursor++;
sign = (*cursor == '-');
if (sign || *cursor == '+')
cursor++;

if (!isdigit(*cursor))
return -2;

exp = *cursor - '0';
while (isdigit(*++cursor))
cursor++;
while (isdigit(*cursor) && exp < 2000000)
{
if (exp < 2000000)
{
exp *= 10;
exp += *cursor - '0';
}
exp *= 10;
exp += *cursor - '0';
cursor++;
}

while (isdigit(*cursor))
cursor++;

if (sign)
exp = -exp;
}
Expand Down Expand Up @@ -863,7 +870,6 @@ static int __set_json_value(int type, va_list ap, json_value_t *val)
json_value_t *json_value_parse(const char *doc)
{
json_value_t *val;
int ret;

val = (json_value_t *)malloc(sizeof (json_value_t));
if (!val)
Expand All @@ -872,26 +878,19 @@ json_value_t *json_value_parse(const char *doc)
while (isspace(*doc))
doc++;

ret = __parse_json_value(doc, &doc, 0, val);
if (ret >= 0)
if (__parse_json_value(doc, &doc, 0, val) >= 0)
{
while (isspace(*doc))
doc++;

if (*doc)
{
__destroy_json_value(val);
ret = -2;
}
}
if (*doc == '\0')
return val;

if (ret < 0)
{
free(val);
return NULL;
__destroy_json_value(val);
}

return val;
free(val);
return NULL;
}

json_value_t *json_value_create(int type, ...)
Expand Down

0 comments on commit fdcd4c0

Please sign in to comment.