Skip to content

Commit e8c2920

Browse files
committed
Optimize speed by using self-defined 'isdigit' and 'isspace'.
1 parent 319f2f1 commit e8c2920

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

json_parser.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <stdlib.h>
33
#include <stdarg.h>
44
#include <string.h>
5-
#include <ctype.h>
65
#include <math.h>
76
#include "list.h"
87
#include "json_parser.h"
@@ -49,6 +48,15 @@ struct __json_element
4948
typedef struct __json_member json_member_t;
5049
typedef struct __json_element json_element_t;
5150

51+
static const int __whitespace_map[256] = {
52+
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
53+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54+
1,
55+
};
56+
57+
#define isspace(c) __whitespace_map[(unsigned char)(c)]
58+
#define isdigit(c) ((c) >= '0' && (c) <= '9')
59+
5260
static int __json_string_length(const char *cursor, size_t *len)
5361
{
5462
size_t n = 0;
@@ -300,9 +308,7 @@ static int __parse_json_number(const char *cursor, const char **end,
300308
if (*cursor == '-')
301309
cursor++;
302310

303-
if (*cursor == '0')
304-
cursor++;
305-
else if (isdigit(*cursor))
311+
if (*cursor >= '1' && *cursor <= '9')
306312
{
307313
mant = *cursor - '0';
308314
figures = 1;
@@ -321,6 +327,8 @@ static int __parse_json_number(const char *cursor, const char **end,
321327
cursor++;
322328
}
323329
}
330+
else if (*cursor == '0')
331+
cursor++;
324332
else
325333
return -2;
326334

0 commit comments

Comments
 (0)