Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 106 lines (89 sloc) 2.341 kB
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
1 // by jeremie miller - 2010
2 // public domain, contributions/improvements welcome via github
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
3
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
4 // opportunity to further optimize would be having different jump tables for higher depths
5 #define PUSH(i) if(depth == 1) *out++ = ((cur+i) - js)
6 #define CAP(i) if(depth == 1) *out++ = (cur+i) - (js + *(out-1)) + 1
7
8 int js0n(unsigned char *js, unsigned int len, unsigned short *out)
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
9 {
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
10 unsigned char *cur, *end;
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
11 int depth=0;
12 static void *gostruct[] =
13 {
14 [0 ... 255] = &&l_bad,
15 ['\t'] = &&l_ws, [' '] = &&l_ws, ['\r'] = &&l_ws, ['\n'] = &&l_ws,
16 ['"'] = &&l_quot,
17 [':'] = &&l_is,[','] = &&l_more,
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
18 ['['] = &&l_up, [']'] = &&l_down, // tracking [] and {} individually would allow fuller validation but is really messy
19 ['{'] = &&l_up, ['}'] = &&l_down,
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
20 ['-'] = &&l_bare, [48 ... 57] = &&l_bare, // 0-9
21 ['t'] = &&l_bare, ['f'] = &&l_bare, ['n'] = &&l_bare // true, false, null
22 };
23 static void *gobare[] =
24 {
25 [0 ... 31] = &&l_bad,
26 [32 ... 126] = &&l_char,
27 ['\t'] = &&l_unbare, [' '] = &&l_unbare, ['\r'] = &&l_unbare, ['\n'] = &&l_unbare,
28 [','] = &&l_unbare, [']'] = &&l_unbare, ['}'] = &&l_unbare,
29 [127 ... 255] = &&l_bad
30 };
31 static void *gostring[] =
32 {
33 [0 ... 31] = &&l_bad, [127] = &&l_bad,
34 [32 ... 126] = &&l_char,
35 ['\\'] = &&l_esc, ['"'] = &&l_quot,
36 [128 ... 255] = &&l_char
37 };
38 static void *goesc[] =
39 {
40 [0 ... 255] = &&l_bad,
41 ['"'] = &&l_unesc, ['\\'] = &&l_unesc, ['/'] = &&l_unesc, ['b'] = &&l_unesc,
42 ['f'] = &&l_unesc, ['n'] = &&l_unesc, ['r'] = &&l_unesc, ['t'] = &&l_unesc, ['u'] = &&l_unesc
43 };
44 static void **go = gostruct;
45
46 for(cur=js,end=js+len; cur<end; cur++)
47 {
48 goto *go[*cur];
49 l_loop:;
50 }
51
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
52 return depth; // 0 if successful full parse, >0 for incomplete data
53
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
54 l_bad:
55 return 1;
56
57 l_char:
58 goto l_loop;
59
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
60 l_up:
61 PUSH(0);
62 ++depth;
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
63 goto l_loop;
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
64
65 l_down:
66 --depth;
67 CAP(0);
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
68 goto l_loop;
69
70 l_ws:
71 l_is:
72 l_more:
73 goto l_loop;
74
75 l_quot:
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
76 if(go==gostruct)
77 {
78 PUSH(1);
79 go=gostring;
80 }else{
81 CAP(-1);
82 go=gostruct;
83 }
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
84 goto l_loop;
85
86 l_esc:
87 go = goesc;
88 goto l_loop;
89
90 l_unesc:
91 go = gostring;
92 goto l_loop;
93
94 l_bare:
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
95 PUSH(0);
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
96 go = gobare;
97 goto l_loop;
98
99 l_unbare:
a058668 @quartzjer passing the first tests, seems to be working splendidly
authored
100 CAP(-1);
8084600 @quartzjer first p0st woooo, not quite usable yet as it's in the process of gett…
authored
101 go = gostruct;
102 goto *go[*cur];
103
104 }
105
Something went wrong with that request. Please try again.