Skip to content

Commit 3c85418

Browse files
committed
Simplify codes.
1 parent f3d73d1 commit 3c85418

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

json_parser.c

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,15 @@ static double __evaluate_json_number(const char *integer,
310310

311311
if (*integer != '0')
312312
{
313-
figures++;
314313
mant = *integer - '0';
315-
while (isdigit(*++integer) && figures < 18)
314+
integer++;
315+
figures++;
316+
while (isdigit(*integer) && figures < 18)
316317
{
317-
figures++;
318318
mant *= 10;
319319
mant += *integer - '0';
320+
integer++;
321+
figures++;
320322
}
321323

322324
while (isdigit(*integer))
@@ -336,12 +338,11 @@ static double __evaluate_json_number(const char *integer,
336338

337339
while (isdigit(*fraction) && figures < 18)
338340
{
339-
figures++;
340341
mant *= 10;
341342
mant += *fraction - '0';
342-
343343
exp--;
344344
fraction++;
345+
figures++;
345346
}
346347

347348
num = mant;
@@ -382,38 +383,44 @@ static int __parse_json_number(const char *cursor, const char **end,
382383
if (*cursor == '0' && isdigit(cursor[1]))
383384
return -2;
384385

385-
while (isdigit(*++cursor))
386-
;
386+
cursor++;
387+
while (isdigit(*cursor))
388+
cursor++;
387389

388390
if (*cursor == '.')
389391
{
390-
fraction = ++cursor;
392+
cursor++;
393+
fraction = cursor;
391394
if (!isdigit(*cursor))
392395
return -2;
393396

394-
while (isdigit(*++cursor))
395-
;
397+
cursor++;
398+
while (isdigit(*cursor))
399+
cursor++;
396400
}
397401

398402
if (*cursor == 'E' || *cursor == 'e')
399403
{
400-
sign = (*++cursor == '-');
404+
cursor++;
405+
sign = (*cursor == '-');
401406
if (sign || *cursor == '+')
402407
cursor++;
403408

404409
if (!isdigit(*cursor))
405410
return -2;
406411

407412
exp = *cursor - '0';
408-
while (isdigit(*++cursor))
413+
cursor++;
414+
while (isdigit(*cursor) && exp < 2000000)
409415
{
410-
if (exp < 2000000)
411-
{
412-
exp *= 10;
413-
exp += *cursor - '0';
414-
}
416+
exp *= 10;
417+
exp += *cursor - '0';
418+
cursor++;
415419
}
416420

421+
while (isdigit(*cursor))
422+
cursor++;
423+
417424
if (sign)
418425
exp = -exp;
419426
}
@@ -845,7 +852,6 @@ static int __set_json_value(int type, va_list ap, json_value_t *val)
845852
json_value_t *json_value_parse(const char *doc)
846853
{
847854
json_value_t *val;
848-
int ret;
849855

850856
val = (json_value_t *)malloc(sizeof (json_value_t));
851857
if (!val)
@@ -854,26 +860,19 @@ json_value_t *json_value_parse(const char *doc)
854860
while (isspace(*doc))
855861
doc++;
856862

857-
ret = __parse_json_value(doc, &doc, 0, val);
858-
if (ret >= 0)
863+
if (__parse_json_value(doc, &doc, 0, val) >= 0)
859864
{
860865
while (isspace(*doc))
861866
doc++;
862867

863-
if (*doc)
864-
{
865-
__destroy_json_value(val);
866-
ret = -2;
867-
}
868-
}
868+
if (*doc == '\0')
869+
return val;
869870

870-
if (ret < 0)
871-
{
872-
free(val);
873-
return NULL;
871+
__destroy_json_value(val);
874872
}
875873

876-
return val;
874+
free(val);
875+
return NULL;
877876
}
878877

879878
json_value_t *json_value_create(int type, ...)

0 commit comments

Comments
 (0)