-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonw.c
266 lines (223 loc) · 7.54 KB
/
jsonw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "jsonw.h"
#include <ctype.h>
#include <string.h>
#define OUT_PARAM(TYPE, IDENT) \
TYPE _out_param_##IDENT; \
if (IDENT == NULL) \
IDENT = &_out_param_##IDENT
// basic parsers
char *jsonw_litchr(char chr, char *json) {
if (json && *json == chr)
return ++json;
return NULL;
}
char *jsonw_litstr(char *str, char *json) {
if (json && strncmp(json, str, strlen(str)) == 0)
return json + strlen(str);
return NULL;
}
// characters
char *jsonw_ws(char *json) {
while (json && *json && strchr(" \t\n\r", *json))
json++;
return json;
}
#define WSCHRWS(CHR, JSON) jsonw_ws(jsonw_litchr(CHR, jsonw_ws(JSON)))
char *jsonw_beginarr(char *json) { return WSCHRWS('[', json); }
char *jsonw_endarr(char *json) { return WSCHRWS(']', json); }
char *jsonw_beginobj(char *json) { return WSCHRWS('{', json); }
char *jsonw_endobj(char *json) { return WSCHRWS('}', json); }
char *jsonw_namesep(char *json) { return WSCHRWS(':', json); }
char *jsonw_valuesep(char *json) { return WSCHRWS(',', json); }
char *jsonw_beginstr(char *json) { return jsonw_litchr('"', json); }
char *jsonw_endstr(char *json) { return jsonw_litchr('"', json); }
#undef WSCHRWS
// values, texts
char *jsonw_null(char *json) { return jsonw_litstr("null", json); }
char *jsonw_boolean(bool *b, char *json) {
OUT_PARAM(bool, b);
char *j;
(j = jsonw_litstr("true", json)) && (*b = true) ||
(j = jsonw_litstr("false", json)) && (*b = false, 1);
return j;
}
char *jsonw_number(double *num, char *json) {
OUT_PARAM(double, num);
if (json == NULL)
return NULL;
int sign = *json == '-' && json++ ? -1 : 1;
double significand = 0.0;
// using an unsigned type because integer overflow has undefined behavior
unsigned short shift = 0, exponent = 0;
#define DIGIT_PLUS(LVALUE) \
do { \
if (!isdigit(*json)) \
return NULL; \
for (; isdigit(*json); json++) \
LVALUE *= 10, LVALUE += *json - '0'; \
} while (0)
if (*json == '0' && json++)
;
else
DIGIT_PLUS(significand);
if (*json == '.' && json++) {
char *j = json;
DIGIT_PLUS(significand);
shift = json - j;
}
if ((*json == 'e' || *json == 'E') && json++) {
int sign = *json == '-' && json++ ? -1 : (*json == '+' && json++, 1);
DIGIT_PLUS(exponent);
exponent *= sign;
}
#undef DIGIT_PLUS
exponent -= shift, significand *= sign;
// conversion from `unsigned short` to `short` preserves bit pattern
for (; (short)exponent > 0; exponent--)
significand *= 10;
for (; (short)exponent < 0; exponent++)
significand /= 10;
return *num = significand, json;
}
char *jsonw_character(char *chr, char *json) {
OUT_PARAM(char, chr);
if (json == NULL)
return NULL;
if (*json == '\\' && ++json) {
switch (*json++) {
case '"':
case '\\':
case '/':
return *chr = json[-1], json;
case 'b':
return *chr = '\b', json;
case 'f':
return *chr = '\f', json;
case 'n':
return *chr = '\n', json;
case 'r':
return *chr = '\r', json;
case 't':
return *chr = '\t', json;
case 'u':;
// ISO/IEC 9899:TC3, $5.2.4.2.1: `USHRT_MAX` is at least 65535
unsigned short codepoint = 0;
for (int i = 0; i < 4; i++) {
codepoint <<= 4;
if (isdigit(*json))
codepoint |= *json - '0';
else if (isxdigit(*json))
codepoint |= tolower(*json) - 'a' + 10;
else
return NULL;
json++;
}
// if a '\uXXXX' escape is beyond 7-bit ASCII, return a sentinel '\0'
// and let clients handle things themselves if they so wish. unicode
// shenanigans are beyond the scope of this library
return *chr = codepoint <= 0x7f ? codepoint : '\0', json;
}
return NULL;
}
if ((unsigned char)*json >= ' ' && *json != '"')
return *chr = *json, ++json;
return NULL;
}
char *jsonw_string(size_t *len, char *json) {
OUT_PARAM(size_t, len);
size_t length = 0; // only write to `len` on success
char *j = json = jsonw_beginstr(json);
while (j = jsonw_character(NULL, j))
json = j, length++;
if (json = jsonw_endstr(json))
return *len = length, json;
return NULL;
}
char *jsonw_name(char *json) { return jsonw_namesep(jsonw_string(NULL, json)); }
char *jsonw_element(char *json) {
return jsonw_valuesep(jsonw_value(NULL, json));
}
char *jsonw_member(char *json) { return jsonw_element(jsonw_name(json)); }
char *jsonw_array(size_t *len, char *json) {
OUT_PARAM(size_t, len);
size_t length = 0; // only write to `len` on success
char *j = json = jsonw_beginarr(json);
if (j = jsonw_endarr(json))
return *len = length, j;
for (; json = jsonw_value(NULL, json); json = jsonw_valuesep(json), length++)
if (j = jsonw_endarr(json))
return *len = length, j;
return NULL;
}
char *jsonw_object(size_t *len, char *json) {
OUT_PARAM(size_t, len);
size_t length = 0; // only write to `len` on success
char *j = json = jsonw_beginobj(json);
if (j = jsonw_endobj(json))
return *len = length, j;
for (; json = jsonw_value(NULL, jsonw_name(json));
json = jsonw_valuesep(json), length++)
if (j = jsonw_endobj(json))
return *len = length, j;
return NULL;
}
char *jsonw_primitive(jsonw_ty *type, char *json) {
OUT_PARAM(jsonw_ty, type);
char *j;
(j = jsonw_null(json)) && (*type = JSONW_NULL, 1) ||
(j = jsonw_boolean(NULL, json)) && (*type = JSONW_BOOLEAN) ||
(j = jsonw_number(NULL, json)) && (*type = JSONW_NUMBER) ||
(j = jsonw_string(NULL, json)) && (*type = JSONW_STRING);
return j;
}
char *jsonw_structured(jsonw_ty *type, char *json) {
OUT_PARAM(jsonw_ty, type);
char *j;
(j = jsonw_array(NULL, json)) && (*type = JSONW_ARRAY) ||
(j = jsonw_object(NULL, json)) && (*type = JSONW_OBJECT);
return j;
}
char *jsonw_value(jsonw_ty *type, char *json) {
// `jsonw_array` calls `jsonw_value` and `jsonw_value` calls `jsonw_array`,
// resulting in a cycle when `json == NULL`. this check breaks that cycle
if (json == NULL)
return NULL;
// no need to `OUT_PARAM(json_ty, type)` because we don't dereference it
char *j;
(j = jsonw_primitive(type, json)) || (j = jsonw_structured(type, json));
return j;
}
char *jsonw_text(jsonw_ty *type, char *json) {
// no need to `OUT_PARAM(json_ty, type)` because we don't dereference it
return jsonw_ws(jsonw_value(type, jsonw_ws(json)));
}
// utilities
int jsonw_strcmp(char *str, char *json) {
// compare is performed without normalization
char c;
do
c = '\0', json = jsonw_character(&c, json);
while (*str && *str == c && str++);
return *str - c;
}
char *jsonw_index(size_t idx, char *json) {
while (idx--)
json = jsonw_element(json);
return json;
}
char *jsonw_find(char *name, char *json) {
do
if (jsonw_strcmp(name, jsonw_beginstr(json)) == 0)
return json;
while (json = jsonw_member(json));
return NULL;
}
char *jsonw_lookup(char *name, char *json) {
return jsonw_name(jsonw_find(name, json));
}
char *jsonw_unescape(char *buf, size_t len, char *json) {
// `len` shall be strictly greater than zero
for (char *j = json; --len && (j = jsonw_character(buf, j)); buf++)
json = j;
return *buf = '\0', json;
}