tg_geometry 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -79
- data/README.md +82 -191
- data/Rakefile +3 -3
- data/benchmark/falcon_concurrency.rb +1 -1
- data/benchmark/feature_source.rb +92 -0
- data/docs/ARCHITECTURE.md +29 -107
- data/docs/BENCHMARKING.md +20 -1
- data/docs/CASUAL_EXAMPLE.md +71 -458
- data/docs/CONCURRENCY.md +13 -7
- data/docs/ERROR_HANDLING.md +30 -0
- data/docs/FEATURE_SOURCE.md +166 -0
- data/docs/LIMITATIONS.md +11 -50
- data/docs/MEMORY_OWNERSHIP.md +20 -2
- data/ext/tg_geometry/extconf.rb +46 -4
- data/ext/tg_geometry/tg_geometry_ext.c +2453 -150
- data/ext/tg_geometry/tg_geometry_vendor_json.c +17 -0
- data/ext/tg_geometry/tg_geometry_vendor_tg.c +3 -0
- data/ext/tg_geometry/vendor/.vendored +8 -2
- data/ext/tg_geometry/vendor/json/LICENSE +20 -0
- data/ext/tg_geometry/vendor/json/VERSION +3 -0
- data/ext/tg_geometry/vendor/json/json.c +1024 -0
- data/ext/tg_geometry/vendor/json/json.h +207 -0
- data/lib/tg/geometry/registry.rb +3 -3
- data/lib/tg/geometry/version.rb +1 -1
- data/script/vendor_libs.rb +22 -6
- data/spec/{expansion_a_auto_strategy_spec.rb → auto_strategy_spec.rb} +1 -1
- data/spec/{block_12_batch_packed_spec.rb → batch_packed_spec.rb} +1 -1
- data/spec/{block_20_concurrency_spec.rb → concurrency_spec.rb} +1 -1
- data/spec/{block_13_error_hardening_spec.rb → error_hardening_spec.rb} +1 -1
- data/spec/feature_source_nogvl_spec.rb +51 -0
- data/spec/feature_source_spec.rb +268 -0
- data/spec/{expansion_d_format_coverage_spec.rb → format_coverage_spec.rb} +1 -1
- data/spec/{block_20_fuzz_spec.rb → fuzz_spec.rb} +1 -1
- data/spec/{block_4_geom_api_spec.rb → geom_api_spec.rb} +1 -1
- data/spec/{block_3_geom_parse_spec.rb → geom_parse_spec.rb} +1 -1
- data/spec/{block_8_index_borrowed_geometry_spec.rb → index_borrowed_geometry_spec.rb} +1 -1
- data/spec/{block_6_index_build_spec.rb → index_build_spec.rb} +2 -2
- data/spec/{block_9_flat_query_spec.rb → index_flat_query_spec.rb} +1 -1
- data/spec/{block_7_index_owned_geometry_spec.rb → index_owned_geometry_spec.rb} +1 -1
- data/spec/{block_10_rtree_strategy_spec.rb → index_rtree_accounting_spec.rb} +1 -1
- data/spec/{block_11_rtree_order_spec.rb → index_rtree_order_spec.rb} +1 -1
- data/spec/{block_1_skeleton_spec.rb → load_and_errors_spec.rb} +1 -1
- data/spec/{expansion_e_low_level_geometry_spec.rb → low_level_geometry_spec.rb} +1 -1
- data/spec/{block_14_memory_gc_hardening_spec.rb → memory_gc_spec.rb} +1 -1
- data/spec/{expansion_i_ractor_spec.rb → ractor_spec.rb} +1 -1
- data/spec/{block_5_rect_api_spec.rb → rect_api_spec.rb} +1 -1
- data/spec/{expansion_b_registry_spec.rb → registry_spec.rb} +1 -1
- data/spec/{expansion_j_full_tg_api_coverage_spec.rb → tg_api_coverage_spec.rb} +1 -1
- data/spec/{block_2_vendor_spec.rb → vendor_sources_spec.rb} +4 -4
- metadata +39 -38
- data/docs/ACTIVE_RECORD.md +0 -26
- data/docs/AUTO_STRATEGY.md +0 -15
- data/docs/EXPANSION_E_TO_H_STATUS.md +0 -51
- data/docs/FORMAT_COVERAGE.md +0 -23
- data/docs/FULL_TG_API_COVERAGE.md +0 -109
- data/docs/LOW_LEVEL_GEOMETRY.md +0 -121
- data/docs/RACTOR.md +0 -40
- data/docs/REGISTRY.md +0 -37
- data/docs/RELEASE_CHECKLIST.md +0 -39
- /data/spec/{expansion_c_active_record_source_spec.rb → active_record_source_spec.rb} +0 -0
|
@@ -0,0 +1,1024 @@
|
|
|
1
|
+
// https://github.com/tidwall/json.c
|
|
2
|
+
//
|
|
3
|
+
// Copyright 2023 Joshua J Baker. All rights reserved.
|
|
4
|
+
// Use of this source code is governed by an MIT-style
|
|
5
|
+
// license that can be found in the LICENSE file.
|
|
6
|
+
#include <limits.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <string.h>
|
|
9
|
+
|
|
10
|
+
#ifndef JSON_STATIC
|
|
11
|
+
#include "json.h"
|
|
12
|
+
#else
|
|
13
|
+
enum json_type {
|
|
14
|
+
JSON_NULL,
|
|
15
|
+
JSON_FALSE,
|
|
16
|
+
JSON_NUMBER,
|
|
17
|
+
JSON_STRING,
|
|
18
|
+
JSON_TRUE,
|
|
19
|
+
JSON_ARRAY,
|
|
20
|
+
JSON_OBJECT,
|
|
21
|
+
};
|
|
22
|
+
struct json { void *priv[4]; };
|
|
23
|
+
|
|
24
|
+
struct json_valid {
|
|
25
|
+
bool valid;
|
|
26
|
+
size_t pos;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
#define JSON_EXTERN static
|
|
30
|
+
#endif
|
|
31
|
+
|
|
32
|
+
#ifndef JSON_EXTERN
|
|
33
|
+
#define JSON_EXTERN
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
#ifndef JSON_MAXDEPTH
|
|
37
|
+
#define JSON_MAXDEPTH 1024
|
|
38
|
+
#endif
|
|
39
|
+
|
|
40
|
+
struct vutf8res { int n; uint32_t cp; };
|
|
41
|
+
|
|
42
|
+
// parse and validate a single utf8 codepoint.
|
|
43
|
+
// The first byte has already been checked from the vstring function.
|
|
44
|
+
static inline struct vutf8res vutf8(const uint8_t data[], int64_t len) {
|
|
45
|
+
uint32_t cp;
|
|
46
|
+
int n = 0;
|
|
47
|
+
if (data[0]>>4 == 14) {
|
|
48
|
+
if (len < 3) goto fail;
|
|
49
|
+
if (((data[1]>>6)|(data[2]>>6<<2)) != 10) goto fail;
|
|
50
|
+
cp = ((uint32_t)(data[0]&15)<<12)|((uint32_t)(data[1]&63)<<6)|
|
|
51
|
+
((uint32_t)(data[2]&63));
|
|
52
|
+
n = 3;
|
|
53
|
+
} else if (data[0]>>3 == 30) {
|
|
54
|
+
if (len < 4) goto fail;
|
|
55
|
+
if (((data[1]>>6)|(data[2]>>6<<2)|(data[3]>>6<<4)) != 42) goto fail;
|
|
56
|
+
cp = ((uint32_t)(data[0]&7)<<18)|((uint32_t)(data[1]&63)<<12)|
|
|
57
|
+
((uint32_t)(data[2]&63)<<6)|((uint32_t)(data[3]&63));
|
|
58
|
+
n = 4;
|
|
59
|
+
} else if (data[0]>>5 == 6) {
|
|
60
|
+
if (len < 2) goto fail;
|
|
61
|
+
if (data[1]>>6 != 2) goto fail;
|
|
62
|
+
cp = ((uint32_t)(data[0]&31)<<6)|((uint32_t)(data[1]&63));
|
|
63
|
+
n = 2;
|
|
64
|
+
} else {
|
|
65
|
+
goto fail;
|
|
66
|
+
}
|
|
67
|
+
if (cp < 128) goto fail; // don't allow multibyte ascii characters
|
|
68
|
+
if (cp >= 0x10FFFF) goto fail; // restricted to utf-16
|
|
69
|
+
if (cp >= 0xD800 && cp <= 0xDFFF) goto fail; // needs surrogate pairs
|
|
70
|
+
return (struct vutf8res) { .n = n, .cp = cp };
|
|
71
|
+
fail:
|
|
72
|
+
return (struct vutf8res) { 0 };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static inline int64_t vesc(const uint8_t *json, int64_t jlen, int64_t i) {
|
|
76
|
+
// The first byte has already been checked from the vstring function.
|
|
77
|
+
i += 1;
|
|
78
|
+
if (i == jlen) return -(i+1);
|
|
79
|
+
switch (json[i]) {
|
|
80
|
+
case '"': case '\\': case '/': case 'b': case 'f': case 'n':
|
|
81
|
+
case 'r': case 't': return i+1;
|
|
82
|
+
case 'u':
|
|
83
|
+
for (int j = 0; j < 4; j++) {
|
|
84
|
+
i++;
|
|
85
|
+
if (i == jlen) return -(i+1);
|
|
86
|
+
if (!((json[i] >= '0' && json[i] <= '9') ||
|
|
87
|
+
(json[i] >= 'a' && json[i] <= 'f') ||
|
|
88
|
+
(json[i] >= 'A' && json[i] <= 'F'))) return -(i+1);
|
|
89
|
+
}
|
|
90
|
+
return i+1;
|
|
91
|
+
}
|
|
92
|
+
return -(i+1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#ifndef ludo
|
|
96
|
+
#define ludo
|
|
97
|
+
#define ludo1(i,f) f; i++;
|
|
98
|
+
#define ludo2(i,f) ludo1(i,f); ludo1(i,f);
|
|
99
|
+
#define ludo4(i,f) ludo2(i,f); ludo2(i,f);
|
|
100
|
+
#define ludo8(i,f) ludo4(i,f); ludo4(i,f);
|
|
101
|
+
#define ludo16(i,f) ludo8(i,f); ludo8(i,f);
|
|
102
|
+
#define ludo32(i,f) ludo16(i,f); ludo16(i,f);
|
|
103
|
+
#define ludo64(i,f) ludo32(i,f); ludo32(i,f);
|
|
104
|
+
#define for1(i,n,f) while(i+1<=(n)) { ludo1(i,f); }
|
|
105
|
+
#define for2(i,n,f) while(i+2<=(n)) { ludo2(i,f); } for1(i,n,f);
|
|
106
|
+
#define for4(i,n,f) while(i+4<=(n)) { ludo4(i,f); } for1(i,n,f);
|
|
107
|
+
#define for8(i,n,f) while(i+8<=(n)) { ludo8(i,f); } for1(i,n,f);
|
|
108
|
+
#define for16(i,n,f) while(i+16<=(n)) { ludo16(i,f); } for1(i,n,f);
|
|
109
|
+
#define for32(i,n,f) while(i+32<=(n)) { ludo32(i,f); } for1(i,n,f);
|
|
110
|
+
#define for64(i,n,f) while(i+64<=(n)) { ludo64(i,f); } for1(i,n,f);
|
|
111
|
+
#endif
|
|
112
|
+
|
|
113
|
+
static const uint8_t strtoksu[256] = {
|
|
114
|
+
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
|
115
|
+
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
116
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,
|
|
117
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
118
|
+
#ifndef JSON_NOVALIDATEUTF8
|
|
119
|
+
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
|
120
|
+
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
|
121
|
+
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
|
|
122
|
+
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,
|
|
123
|
+
//0=ascii, 1=quote, 2=escape, 3=utf82, 4=utf83, 5=utf84, 6=error
|
|
124
|
+
#endif
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
static int64_t vstring(const uint8_t *json, int64_t jlen, int64_t i) {
|
|
128
|
+
while (1) {
|
|
129
|
+
for8(i, jlen, { if (strtoksu[json[i]]) goto tok; })
|
|
130
|
+
break;
|
|
131
|
+
tok:
|
|
132
|
+
if (json[i] == '"') {
|
|
133
|
+
return i+1;
|
|
134
|
+
#ifndef JSON_NOVALIDATEUTF8
|
|
135
|
+
} else if (json[i] > 127) {
|
|
136
|
+
struct vutf8res res = vutf8(json+i, jlen-i);
|
|
137
|
+
if (res.n == 0) break;
|
|
138
|
+
i += res.n;
|
|
139
|
+
#endif
|
|
140
|
+
} else if (json[i] == '\\') {
|
|
141
|
+
if ((i = vesc(json, jlen, i)) < 0) break;
|
|
142
|
+
} else {
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return -(i+1);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static int64_t vnumber(const uint8_t *data, int64_t dlen, int64_t i) {
|
|
150
|
+
i--;
|
|
151
|
+
// sign
|
|
152
|
+
if (data[i] == '-') {
|
|
153
|
+
i++;
|
|
154
|
+
if (i == dlen) return -(i+1);
|
|
155
|
+
if (data[i] < '0' || data[i] > '9') return -(i+1);
|
|
156
|
+
}
|
|
157
|
+
// int
|
|
158
|
+
if (data[i] == '0') {
|
|
159
|
+
i++;
|
|
160
|
+
} else {
|
|
161
|
+
for (; i < dlen; i++) {
|
|
162
|
+
if (data[i] >= '0' && data[i] <= '9') continue;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// frac
|
|
167
|
+
if (i == dlen) return i;
|
|
168
|
+
if (data[i] == '.') {
|
|
169
|
+
i++;
|
|
170
|
+
if (i == dlen) return -(i+1);
|
|
171
|
+
if (data[i] < '0' || data[i] > '9') return -(i+1);
|
|
172
|
+
i++;
|
|
173
|
+
for (; i < dlen; i++) {
|
|
174
|
+
if (data[i] >= '0' && data[i] <= '9') continue;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// exp
|
|
179
|
+
if (i == dlen) return i;
|
|
180
|
+
if (data[i] == 'e' || data[i] == 'E') {
|
|
181
|
+
i++;
|
|
182
|
+
if (i == dlen) return -(i+1);
|
|
183
|
+
if (data[i] == '+' || data[i] == '-') i++;
|
|
184
|
+
if (i == dlen) return -(i+1);
|
|
185
|
+
if (data[i] < '0' || data[i] > '9') return -(i+1);
|
|
186
|
+
i++;
|
|
187
|
+
for (; i < dlen; i++) {
|
|
188
|
+
if (data[i] >= '0' && data[i] <= '9') continue;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return i;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static int64_t vnull(const uint8_t *data, int64_t dlen, int64_t i) {
|
|
196
|
+
return i+3 <= dlen && data[i] == 'u' && data[i+1] == 'l' &&
|
|
197
|
+
data[i+2] == 'l' ? i+3 : -(i+1);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static int64_t vtrue(const uint8_t *data, int64_t dlen, int64_t i) {
|
|
201
|
+
return i+3 <= dlen && data[i] == 'r' && data[i+1] == 'u' &&
|
|
202
|
+
data[i+2] == 'e' ? i+3 : -(i+1);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static int64_t vfalse(const uint8_t *data, int64_t dlen, int64_t i) {
|
|
206
|
+
return i+4 <= dlen && data[i] == 'a' && data[i+1] == 'l' &&
|
|
207
|
+
data[i+2] == 's' && data[i+3] == 'e' ? i+4 : -(i+1);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
static int64_t vcolon(const uint8_t *json, int64_t len, int64_t i) {
|
|
211
|
+
if (i == len) return -(i+1);
|
|
212
|
+
if (json[i] == ':') return i+1;
|
|
213
|
+
do {
|
|
214
|
+
switch (json[i]) {
|
|
215
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
216
|
+
case ':': return i+1;
|
|
217
|
+
default: return -(i+1);
|
|
218
|
+
}
|
|
219
|
+
} while (++i < len);
|
|
220
|
+
return -(i+1);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static int64_t vcomma(const uint8_t *json, int64_t len, int64_t i, uint8_t end)
|
|
224
|
+
{
|
|
225
|
+
if (i == len) return -(i+1);
|
|
226
|
+
if (json[i] == ',') return i;
|
|
227
|
+
do {
|
|
228
|
+
switch (json[i]) {
|
|
229
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
230
|
+
case ',': return i;
|
|
231
|
+
default: return json[i] == end ? i : -(i+1);
|
|
232
|
+
}
|
|
233
|
+
} while (++i < len);
|
|
234
|
+
return -(i+1);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static int64_t vany(const uint8_t *data, int64_t dlen, int64_t i, int depth);
|
|
238
|
+
|
|
239
|
+
static int64_t varray(const uint8_t *data, int64_t dlen, int64_t i, int depth) {
|
|
240
|
+
for (; i < dlen; i++) {
|
|
241
|
+
switch (data[i]) {
|
|
242
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
243
|
+
case ']': return i+1;
|
|
244
|
+
default:
|
|
245
|
+
for (; i < dlen; i++) {
|
|
246
|
+
if ((i = vany(data, dlen, i, depth+1)) < 0) return i;
|
|
247
|
+
if ((i = vcomma(data, dlen, i, ']')) < 0) return i;
|
|
248
|
+
if (data[i] == ']') return i+1;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return -(i+1);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static int64_t vkey(const uint8_t *json, int64_t len, int64_t i) {
|
|
256
|
+
for16(i, len, { if (strtoksu[json[i]]) goto tok; })
|
|
257
|
+
return -(i+1);
|
|
258
|
+
tok:
|
|
259
|
+
if (json[i] == '"') return i+1;
|
|
260
|
+
return vstring(json, len, i);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
static int64_t vobject(const uint8_t *data, int64_t dlen, int64_t i, int depth)
|
|
264
|
+
{
|
|
265
|
+
for (; i < dlen; i++) {
|
|
266
|
+
switch (data[i]) {
|
|
267
|
+
case '"':
|
|
268
|
+
key:
|
|
269
|
+
if ((i = vkey(data, dlen, i+1)) < 0) return i;
|
|
270
|
+
if ((i = vcolon(data, dlen, i)) < 0) return i;
|
|
271
|
+
if ((i = vany(data, dlen, i, depth+1)) < 0) return i;
|
|
272
|
+
if ((i = vcomma(data, dlen, i, '}')) < 0) return i;
|
|
273
|
+
if (data[i] == '}') return i+1;
|
|
274
|
+
i++;
|
|
275
|
+
for (; i < dlen; i++) {
|
|
276
|
+
switch (data[i]) {
|
|
277
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
278
|
+
case '"': goto key;
|
|
279
|
+
default: return -(i+1);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return -(i+1);
|
|
283
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
284
|
+
case '}': return i+1;
|
|
285
|
+
default:
|
|
286
|
+
return -(i+1);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return -(i+1);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
static int64_t vany(const uint8_t *data, int64_t dlen, int64_t i, int depth) {
|
|
293
|
+
if (depth > JSON_MAXDEPTH) return -(i+1);
|
|
294
|
+
for (; i < dlen; i++) {
|
|
295
|
+
switch (data[i]) {
|
|
296
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
297
|
+
case '{': return vobject(data, dlen, i+1, depth);
|
|
298
|
+
case '[': return varray(data, dlen, i+1, depth);
|
|
299
|
+
case '"': return vstring(data, dlen, i+1);
|
|
300
|
+
case 't': return vtrue(data, dlen, i+1);
|
|
301
|
+
case 'f': return vfalse(data, dlen, i+1);
|
|
302
|
+
case 'n': return vnull(data, dlen, i+1);
|
|
303
|
+
case '-': case '0': case '1': case '2': case '3': case '4':
|
|
304
|
+
case '5': case '6': case '7': case '8': case '9':
|
|
305
|
+
return vnumber(data, dlen, i+1);
|
|
306
|
+
}
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
return -(i+1);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
static int64_t vpayload(const uint8_t *data, int64_t dlen, int64_t i) {
|
|
313
|
+
for (; i < dlen; i++) {
|
|
314
|
+
switch (data[i]) {
|
|
315
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
316
|
+
default:
|
|
317
|
+
if ((i = vany(data, dlen, i, 1)) < 0) return i;
|
|
318
|
+
for (; i < dlen; i++) {
|
|
319
|
+
switch (data[i]) {
|
|
320
|
+
case ' ': case '\t': case '\n': case '\r': continue;
|
|
321
|
+
default: return -(i+1);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return i;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return -(i+1);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
JSON_EXTERN
|
|
331
|
+
struct json_valid json_validn_ex(const char *json_str, size_t len, int opts) {
|
|
332
|
+
(void)opts; // for future use
|
|
333
|
+
int64_t ilen = len;
|
|
334
|
+
if (ilen < 0) return (struct json_valid) { 0 };
|
|
335
|
+
int64_t pos = vpayload((uint8_t*)json_str, len, 0);
|
|
336
|
+
if (pos > 0) return (struct json_valid) { .valid = true };
|
|
337
|
+
return (struct json_valid) { .pos = (-pos)-1 };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
JSON_EXTERN struct json_valid json_valid_ex(const char *json_str, int opts) {
|
|
341
|
+
return json_validn_ex(json_str, json_str?strlen(json_str):0, opts);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
JSON_EXTERN bool json_validn(const char *json_str, size_t len) {
|
|
345
|
+
return json_validn_ex(json_str, len, 0).valid;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
JSON_EXTERN bool json_valid(const char *json_str) {
|
|
349
|
+
return json_validn(json_str, json_str?strlen(json_str):0);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// don't changes these flags without changing the numtoks table too.
|
|
353
|
+
enum iflags { IESC = 1, IDOT = 2, ISCI = 4, ISIGN = 8 };
|
|
354
|
+
|
|
355
|
+
#define jmake(info, raw, end, len) ((struct json) { .priv = { \
|
|
356
|
+
(void*)(uintptr_t)(info), (void*)(uintptr_t)(raw), \
|
|
357
|
+
(void*)(uintptr_t)(end), (void*)(uintptr_t)(len) } })
|
|
358
|
+
#define jinfo(json) ((int)(uintptr_t)((json).priv[0]))
|
|
359
|
+
#define jraw(json) ((uint8_t*)(uintptr_t)((json).priv[1]))
|
|
360
|
+
#define jend(json) ((uint8_t*)(uintptr_t)((json).priv[2]))
|
|
361
|
+
#define jlen(json) ((size_t)(uintptr_t)((json).priv[3]))
|
|
362
|
+
|
|
363
|
+
static const uint8_t strtoksa[256] = {
|
|
364
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
365
|
+
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
366
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
static inline size_t count_string(uint8_t *raw, uint8_t *end, int *infoout) {
|
|
370
|
+
size_t len = end-raw;
|
|
371
|
+
size_t i = 1;
|
|
372
|
+
int info = 0;
|
|
373
|
+
bool e = false;
|
|
374
|
+
while (1) {
|
|
375
|
+
for8(i, len, {
|
|
376
|
+
if (strtoksa[raw[i]]) goto tok;
|
|
377
|
+
e = false;
|
|
378
|
+
});
|
|
379
|
+
break;
|
|
380
|
+
tok:
|
|
381
|
+
if (raw[i] == '"') {
|
|
382
|
+
i++;
|
|
383
|
+
if (!e) {
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
e = false;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (raw[i] == '\\') {
|
|
390
|
+
info |= IESC;
|
|
391
|
+
e = !e;
|
|
392
|
+
}
|
|
393
|
+
i++;
|
|
394
|
+
}
|
|
395
|
+
*infoout = info;
|
|
396
|
+
return i;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
static struct json take_string(uint8_t *raw, uint8_t *end) {
|
|
400
|
+
int info = 0;
|
|
401
|
+
size_t i = count_string(raw, end, &info);
|
|
402
|
+
return jmake(info, raw, end, i);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
static const uint8_t numtoks[256] = {
|
|
406
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
407
|
+
0,0,0,0,0,0,0,0,0,0,0,1,0,1,3,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
|
|
408
|
+
0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
409
|
+
0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
410
|
+
// don't changes these flags without changing enum iflags too.
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
static struct json take_number(uint8_t *raw, uint8_t *end) {
|
|
414
|
+
int64_t len = end-raw;
|
|
415
|
+
int info = raw[0] == '-' ? ISIGN : 0;
|
|
416
|
+
int64_t i = 1;
|
|
417
|
+
for16(i, len, {
|
|
418
|
+
if (!numtoks[raw[i]]) goto done;
|
|
419
|
+
info |= (numtoks[raw[i]]-1);
|
|
420
|
+
});
|
|
421
|
+
done:
|
|
422
|
+
return jmake(info, raw, end, i);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
static const uint8_t nesttoks[256] = {
|
|
426
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
427
|
+
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
428
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,0,0,
|
|
429
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,0,0,
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
static size_t count_nested(uint8_t *raw, uint8_t *end) {
|
|
433
|
+
size_t len = end-raw;
|
|
434
|
+
size_t i = 1;
|
|
435
|
+
int depth = 1;
|
|
436
|
+
int kind = 0;
|
|
437
|
+
if (i >= len) return i;
|
|
438
|
+
while (depth) {
|
|
439
|
+
for16(i, len, { if (nesttoks[raw[i]]) goto tok0; });
|
|
440
|
+
break;
|
|
441
|
+
tok0:
|
|
442
|
+
kind = nesttoks[raw[i]];
|
|
443
|
+
i++;
|
|
444
|
+
if (kind-1) {
|
|
445
|
+
depth += kind-3;
|
|
446
|
+
} else {
|
|
447
|
+
while (1) {
|
|
448
|
+
for16(i, len, { if (raw[i]=='"') goto tok1; });
|
|
449
|
+
break;
|
|
450
|
+
tok1:
|
|
451
|
+
i++;
|
|
452
|
+
if (raw[i-2] == '\\') {
|
|
453
|
+
size_t j = i-3;
|
|
454
|
+
size_t e = 1;
|
|
455
|
+
while (j > 0 && raw[j] == '\\') {
|
|
456
|
+
e = (e+1)&1;
|
|
457
|
+
j--;
|
|
458
|
+
}
|
|
459
|
+
if (e) continue;
|
|
460
|
+
}
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return i;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
static struct json take_literal(uint8_t *raw, uint8_t *end, size_t litlen) {
|
|
469
|
+
size_t rlen = end-raw;
|
|
470
|
+
return jmake(0, raw, end, rlen < litlen ? rlen : litlen);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
static struct json peek_any(uint8_t *raw, uint8_t *end) {
|
|
474
|
+
while (raw < end) {
|
|
475
|
+
switch (*raw){
|
|
476
|
+
case '}': case ']': return (struct json){ 0 };
|
|
477
|
+
case '{': case '[': return jmake(0, raw, end, 0);
|
|
478
|
+
case '"': return take_string(raw, end);
|
|
479
|
+
case 'n': return take_literal(raw, end, 4);
|
|
480
|
+
case 't': return take_literal(raw, end, 4);
|
|
481
|
+
case 'f': return take_literal(raw, end, 5);
|
|
482
|
+
case '-': case '0': case '1': case '2': case '3': case '4': case '5':
|
|
483
|
+
case '6': case '7': case '8': case '9': return take_number(raw, end);
|
|
484
|
+
}
|
|
485
|
+
raw++;
|
|
486
|
+
}
|
|
487
|
+
return (struct json){ 0 };
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
JSON_EXTERN struct json json_first(struct json json) {
|
|
491
|
+
uint8_t *raw = jraw(json);
|
|
492
|
+
uint8_t *end = jend(json);
|
|
493
|
+
if (end <= raw || (*raw != '{' && *raw != '[')) return (struct json){0};
|
|
494
|
+
return peek_any(raw+1, end);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
JSON_EXTERN struct json json_next(struct json json) {
|
|
498
|
+
uint8_t *raw = jraw(json);
|
|
499
|
+
uint8_t *end = jend(json);
|
|
500
|
+
if (end <= raw) return (struct json){ 0 };
|
|
501
|
+
raw += jlen(json) == 0 ? count_nested(raw, end): jlen(json);
|
|
502
|
+
return peek_any(raw, end);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
JSON_EXTERN struct json json_parsen(const char *json_str, size_t len) {
|
|
506
|
+
if (len > 0 && (json_str[0] == '[' || json_str[0] == '{')) {
|
|
507
|
+
return jmake(0, json_str, json_str+len, 0);
|
|
508
|
+
}
|
|
509
|
+
if (len == 0) return (struct json){ 0 };
|
|
510
|
+
return peek_any((uint8_t*)json_str, (uint8_t*)json_str+len);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
JSON_EXTERN struct json json_parse(const char *json_str) {
|
|
514
|
+
return json_parsen(json_str, json_str?strlen(json_str):0);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
JSON_EXTERN bool json_exists(struct json json) {
|
|
518
|
+
return jraw(json) && jend(json);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
JSON_EXTERN const char *json_raw(struct json json) {
|
|
522
|
+
return (char*)jraw(json);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
JSON_EXTERN size_t json_raw_length(struct json json) {
|
|
526
|
+
if (jlen(json)) return jlen(json);
|
|
527
|
+
if (jraw(json) < jend(json)) return count_nested(jraw(json), jend(json));
|
|
528
|
+
return 0;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
static const uint8_t typetoks[256] = {
|
|
532
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
533
|
+
0,0,3,0,0,0,0,0,0,0,0,0,0,2,0,0,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,
|
|
534
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,
|
|
535
|
+
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,6,0,0,0,0,
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
JSON_EXTERN enum json_type json_type(struct json json) {
|
|
539
|
+
return jraw(json) < jend(json) ? typetoks[*jraw(json)] : JSON_NULL;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
JSON_EXTERN struct json json_ensure(struct json json) {
|
|
543
|
+
return jmake(jinfo(json), jraw(json), jend(json), json_raw_length(json));
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
static int strcmpn(const char *a, size_t alen, const char *b, size_t blen) {
|
|
547
|
+
size_t n = alen < blen ? alen : blen;
|
|
548
|
+
int cmp = strncmp(a, b, n);
|
|
549
|
+
if (cmp == 0) {
|
|
550
|
+
cmp = alen < blen ? -1 : alen > blen ? 1 : 0;
|
|
551
|
+
}
|
|
552
|
+
return cmp;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
static const uint8_t hextoks[256] = {
|
|
556
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
557
|
+
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,
|
|
558
|
+
0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
559
|
+
0,0,0,0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
static uint32_t decode_hex(const uint8_t *str) {
|
|
563
|
+
return (((int)hextoks[str[0]])<<12) | (((int)hextoks[str[1]])<<8) |
|
|
564
|
+
(((int)hextoks[str[2]])<<4) | (((int)hextoks[str[3]])<<0);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
static bool is_surrogate(uint32_t cp) {
|
|
568
|
+
return cp > 55296 && cp < 57344;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
static uint32_t decode_codepoint(uint32_t cp1, uint32_t cp2) {
|
|
572
|
+
return cp1 > 55296 && cp1 < 56320 && cp2 > 56320 && cp2 < 57344 ?
|
|
573
|
+
((cp1 - 55296) << 10) | ((cp2 - 56320) + 65536) :
|
|
574
|
+
65533;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
static inline int encode_codepoint(uint8_t dst[], uint32_t cp) {
|
|
578
|
+
if (cp < 128) {
|
|
579
|
+
dst[0] = cp;
|
|
580
|
+
return 1;
|
|
581
|
+
} else if (cp < 2048) {
|
|
582
|
+
dst[0] = 192 | (cp >> 6);
|
|
583
|
+
dst[1] = 128 | (cp & 63);
|
|
584
|
+
return 2;
|
|
585
|
+
} else if (cp > 1114111 || is_surrogate(cp)) {
|
|
586
|
+
cp = 65533; // error codepoint
|
|
587
|
+
}
|
|
588
|
+
if (cp < 65536) {
|
|
589
|
+
dst[0] = 224 | (cp >> 12);
|
|
590
|
+
dst[1] = 128 | ((cp >> 6) & 63);
|
|
591
|
+
dst[2] = 128 | (cp & 63);
|
|
592
|
+
return 3;
|
|
593
|
+
}
|
|
594
|
+
dst[0] = 240 | (cp >> 18);
|
|
595
|
+
dst[1] = 128 | ((cp >> 12) & 63);
|
|
596
|
+
dst[2] = 128 | ((cp >> 6) & 63);
|
|
597
|
+
dst[3] = 128 | (cp & 63);
|
|
598
|
+
return 4;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// for_each_utf8 iterates over each UTF-8 bytes in jstr, unescaping along the
|
|
602
|
+
// way. 'f' is a loop expression that will make available the 'ch' char which
|
|
603
|
+
// is just a single byte in a UTF-8 series.
|
|
604
|
+
#define for_each_utf8(jstr, len, f) { \
|
|
605
|
+
size_t nn = (len); \
|
|
606
|
+
int ch = 0; \
|
|
607
|
+
(void)ch; \
|
|
608
|
+
for (size_t ii = 0; ii < nn; ii++) { \
|
|
609
|
+
if ((jstr)[ii] != '\\') { \
|
|
610
|
+
ch = (jstr)[ii]; \
|
|
611
|
+
if (1) f \
|
|
612
|
+
continue; \
|
|
613
|
+
}; \
|
|
614
|
+
ii++; \
|
|
615
|
+
if (ii == nn) break; \
|
|
616
|
+
switch ((jstr)[ii]) { \
|
|
617
|
+
case '\\': ch = '\\'; break; \
|
|
618
|
+
case '/' : ch = '/'; break; \
|
|
619
|
+
case 'b' : ch = '\b'; break; \
|
|
620
|
+
case 'f' : ch = '\f'; break; \
|
|
621
|
+
case 'n' : ch = '\n'; break; \
|
|
622
|
+
case 'r' : ch = '\r'; break; \
|
|
623
|
+
case 't' : ch = '\t'; break; \
|
|
624
|
+
case '"' : ch = '"'; break; \
|
|
625
|
+
case 'u' : \
|
|
626
|
+
if (ii+5 > nn) { nn = 0; continue; }; \
|
|
627
|
+
uint32_t cp = decode_hex((jstr)+ii+1); \
|
|
628
|
+
ii += 5; \
|
|
629
|
+
if (is_surrogate(cp)) { \
|
|
630
|
+
if (nn-ii >= 6 && (jstr)[ii] == '\\' && (jstr)[ii+1] == 'u') { \
|
|
631
|
+
cp = decode_codepoint(cp, decode_hex((jstr)+ii+2)); \
|
|
632
|
+
ii += 6; \
|
|
633
|
+
} \
|
|
634
|
+
} \
|
|
635
|
+
uint8_t _bytes[4]; \
|
|
636
|
+
int _n = encode_codepoint(_bytes, cp); \
|
|
637
|
+
for (int _j = 0; _j < _n; _j++) { \
|
|
638
|
+
ch = _bytes[_j]; \
|
|
639
|
+
if (1) f \
|
|
640
|
+
} \
|
|
641
|
+
ii--; \
|
|
642
|
+
continue; \
|
|
643
|
+
default: \
|
|
644
|
+
continue; \
|
|
645
|
+
}; \
|
|
646
|
+
if (1) f \
|
|
647
|
+
} \
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
JSON_EXTERN
|
|
651
|
+
int json_raw_comparen(struct json json, const char *str, size_t len) {
|
|
652
|
+
char *raw = (char*)jraw(json);
|
|
653
|
+
if (!raw) raw = "";
|
|
654
|
+
size_t rlen = json_raw_length(json);
|
|
655
|
+
return strcmpn(raw, rlen, str, len);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
JSON_EXTERN int json_raw_compare(struct json json, const char *str) {
|
|
659
|
+
return json_raw_comparen(json, str, strlen(str));
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
JSON_EXTERN size_t json_string_length(struct json json) {
|
|
663
|
+
size_t len = json_raw_length(json);
|
|
664
|
+
if (json_type(json) != JSON_STRING) {
|
|
665
|
+
return len;
|
|
666
|
+
}
|
|
667
|
+
len = len < 2 ? 0 : len - 2;
|
|
668
|
+
if ((jinfo(json)&IESC) != IESC) {
|
|
669
|
+
return len;
|
|
670
|
+
}
|
|
671
|
+
uint8_t *raw = jraw(json)+1;
|
|
672
|
+
size_t count = 0;
|
|
673
|
+
for_each_utf8(raw, len, { count++; });
|
|
674
|
+
return count;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
JSON_EXTERN
|
|
678
|
+
int json_string_comparen(struct json json, const char *str, size_t slen) {
|
|
679
|
+
if (json_type(json) != JSON_STRING) {
|
|
680
|
+
return json_raw_comparen(json, str, slen);
|
|
681
|
+
}
|
|
682
|
+
uint8_t *raw = jraw(json);
|
|
683
|
+
size_t rlen = json_raw_length(json);
|
|
684
|
+
raw++;
|
|
685
|
+
rlen = rlen < 2 ? 0 : rlen - 2;
|
|
686
|
+
if ((jinfo(json)&IESC) != IESC) {
|
|
687
|
+
return strcmpn((char*)raw, rlen, str, slen);
|
|
688
|
+
}
|
|
689
|
+
int cmp = 0;
|
|
690
|
+
uint8_t *sp = (uint8_t*)(str ? str : "");
|
|
691
|
+
for_each_utf8(raw, rlen, {
|
|
692
|
+
if (!*sp || ch > *sp) {
|
|
693
|
+
cmp = 1;
|
|
694
|
+
goto done;
|
|
695
|
+
} else if (ch < *sp) {
|
|
696
|
+
cmp = -1;
|
|
697
|
+
goto done;
|
|
698
|
+
}
|
|
699
|
+
sp++;
|
|
700
|
+
});
|
|
701
|
+
done:
|
|
702
|
+
if (cmp == 0 && *sp) cmp = -1;
|
|
703
|
+
return cmp;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
JSON_EXTERN
|
|
707
|
+
int json_string_compare(struct json json, const char *str) {
|
|
708
|
+
return json_string_comparen(json, str, str?strlen(str):0);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
JSON_EXTERN size_t json_string_copy(struct json json, char *str, size_t n) {
|
|
712
|
+
size_t len = json_raw_length(json);
|
|
713
|
+
uint8_t *raw = jraw(json);
|
|
714
|
+
bool isjsonstr = json_type(json) == JSON_STRING;
|
|
715
|
+
bool isesc = false;
|
|
716
|
+
if (isjsonstr) {
|
|
717
|
+
raw++;
|
|
718
|
+
len = len < 2 ? 0 : len - 2;
|
|
719
|
+
isesc = (jinfo(json)&IESC) == IESC;
|
|
720
|
+
}
|
|
721
|
+
if (!isesc) {
|
|
722
|
+
if (n == 0) return len;
|
|
723
|
+
n = n-1 < len ? n-1 : len;
|
|
724
|
+
memcpy(str, raw, n);
|
|
725
|
+
str[n] = '\0';
|
|
726
|
+
return len;
|
|
727
|
+
}
|
|
728
|
+
size_t count = 0;
|
|
729
|
+
for_each_utf8(raw, len, {
|
|
730
|
+
if (count < n) str[count] = ch;
|
|
731
|
+
count++;
|
|
732
|
+
});
|
|
733
|
+
if (n > count) str[count] = '\0';
|
|
734
|
+
else if (n > 0) str[n-1] = '\0';
|
|
735
|
+
return count;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
JSON_EXTERN size_t json_array_count(struct json json) {
|
|
739
|
+
size_t count = 0;
|
|
740
|
+
if (json_type(json) == JSON_ARRAY) {
|
|
741
|
+
json = json_first(json);
|
|
742
|
+
while (json_exists(json)) {
|
|
743
|
+
count++;
|
|
744
|
+
json = json_next(json);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return count;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
JSON_EXTERN struct json json_array_get(struct json json, size_t index) {
|
|
751
|
+
if (json_type(json) == JSON_ARRAY) {
|
|
752
|
+
json = json_first(json);
|
|
753
|
+
while (json_exists(json)) {
|
|
754
|
+
if (index == 0) return json;
|
|
755
|
+
json = json_next(json);
|
|
756
|
+
index--;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return (struct json) { 0 };
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
JSON_EXTERN
|
|
763
|
+
struct json json_object_getn(struct json json, const char *key, size_t len) {
|
|
764
|
+
if (json_type(json) == JSON_OBJECT) {
|
|
765
|
+
json = json_first(json);
|
|
766
|
+
while (json_exists(json)) {
|
|
767
|
+
if (json_string_comparen(json, key, len) == 0) {
|
|
768
|
+
return json_next(json);
|
|
769
|
+
}
|
|
770
|
+
json = json_next(json_next(json));
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
return (struct json) { 0 };
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
JSON_EXTERN struct json json_object_get(struct json json, const char *key) {
|
|
777
|
+
return json_object_getn(json, key, key?strlen(key):0);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
static double stod(const uint8_t *str, size_t len, char *buf) {
|
|
781
|
+
memcpy(buf, str, len);
|
|
782
|
+
buf[len] = '\0';
|
|
783
|
+
char *ptr;
|
|
784
|
+
double x = strtod(buf, &ptr);
|
|
785
|
+
return (size_t)(ptr-buf) == len ? x : 0;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
static double parse_double_big(const uint8_t *str, size_t len) {
|
|
789
|
+
char buf[512];
|
|
790
|
+
if (len >= sizeof(buf)) return 0;
|
|
791
|
+
return stod(str, len, buf);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
static double parse_double(const uint8_t *str, size_t len) {
|
|
795
|
+
char buf[32];
|
|
796
|
+
if (len >= sizeof(buf)) return parse_double_big(str, len);
|
|
797
|
+
return stod(str, len, buf);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
static int64_t parse_int64(const uint8_t *s, size_t len) {
|
|
801
|
+
char buf[21];
|
|
802
|
+
double y;
|
|
803
|
+
if (len == 0) return 0;
|
|
804
|
+
if (len < sizeof(buf) && sizeof(long long) == sizeof(int64_t)) {
|
|
805
|
+
memcpy(buf, s, len);
|
|
806
|
+
buf[len] = '\0';
|
|
807
|
+
char *ptr = NULL;
|
|
808
|
+
int64_t x = strtoll(buf, &ptr, 10);
|
|
809
|
+
if ((size_t)(ptr-buf) == len) return x;
|
|
810
|
+
y = strtod(buf, &ptr);
|
|
811
|
+
if ((size_t)(ptr-buf) == len) goto clamp;
|
|
812
|
+
}
|
|
813
|
+
y = parse_double(s, len);
|
|
814
|
+
clamp:
|
|
815
|
+
if (y < (double)INT64_MIN) return INT64_MIN;
|
|
816
|
+
if (y > (double)INT64_MAX) return INT64_MAX;
|
|
817
|
+
return y;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
static uint64_t parse_uint64(const uint8_t *s, size_t len) {
|
|
821
|
+
char buf[21];
|
|
822
|
+
double y;
|
|
823
|
+
if (len == 0) return 0;
|
|
824
|
+
if (len < sizeof(buf) && sizeof(long long) == sizeof(uint64_t) &&
|
|
825
|
+
s[0] != '-')
|
|
826
|
+
{
|
|
827
|
+
memcpy(buf, s, len);
|
|
828
|
+
buf[len] = '\0';
|
|
829
|
+
char *ptr = NULL;
|
|
830
|
+
uint64_t x = strtoull(buf, &ptr, 10);
|
|
831
|
+
if ((size_t)(ptr-buf) == len) return x;
|
|
832
|
+
y = strtod(buf, &ptr);
|
|
833
|
+
if ((size_t)(ptr-buf) == len) goto clamp;
|
|
834
|
+
}
|
|
835
|
+
y = parse_double(s, len);
|
|
836
|
+
clamp:
|
|
837
|
+
if (y < 0) return 0;
|
|
838
|
+
if (y > (double)UINT64_MAX) return UINT64_MAX;
|
|
839
|
+
return y;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
JSON_EXTERN double json_double(struct json json) {
|
|
843
|
+
switch (json_type(json)) {
|
|
844
|
+
case JSON_TRUE:
|
|
845
|
+
return 1;
|
|
846
|
+
case JSON_STRING:
|
|
847
|
+
if (jlen(json) < 3) return 0.0;
|
|
848
|
+
return parse_double(jraw(json)+1, jlen(json)-2);
|
|
849
|
+
case JSON_NUMBER:
|
|
850
|
+
return parse_double(jraw(json), jlen(json));
|
|
851
|
+
default:
|
|
852
|
+
return 0.0;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
JSON_EXTERN int64_t json_int64(struct json json) {
|
|
857
|
+
switch (json_type(json)) {
|
|
858
|
+
case JSON_TRUE:
|
|
859
|
+
return 1;
|
|
860
|
+
case JSON_STRING:
|
|
861
|
+
if (jlen(json) < 2) return 0;
|
|
862
|
+
return parse_int64(jraw(json)+1, jlen(json)-2);
|
|
863
|
+
case JSON_NUMBER:
|
|
864
|
+
return parse_int64(jraw(json), jlen(json));
|
|
865
|
+
default:
|
|
866
|
+
return 0;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
JSON_EXTERN int json_int(struct json json) {
|
|
871
|
+
int64_t x = json_int64(json);
|
|
872
|
+
if (x < (int64_t)INT_MIN) return INT_MIN;
|
|
873
|
+
if (x > (int64_t)INT_MAX) return INT_MAX;
|
|
874
|
+
return x;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
JSON_EXTERN uint64_t json_uint64(struct json json) {
|
|
878
|
+
switch (json_type(json)) {
|
|
879
|
+
case JSON_TRUE:
|
|
880
|
+
return 1;
|
|
881
|
+
case JSON_STRING:
|
|
882
|
+
if (jlen(json) < 2) return 0;
|
|
883
|
+
return parse_uint64(jraw(json)+1, jlen(json)-2);
|
|
884
|
+
case JSON_NUMBER:
|
|
885
|
+
return parse_uint64(jraw(json), jlen(json));
|
|
886
|
+
default:
|
|
887
|
+
return 0;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
JSON_EXTERN bool json_bool(struct json json) {
|
|
892
|
+
switch (json_type(json)) {
|
|
893
|
+
case JSON_TRUE:
|
|
894
|
+
return true;
|
|
895
|
+
case JSON_NUMBER:
|
|
896
|
+
return json_double(json) != 0.0;
|
|
897
|
+
case JSON_STRING: {
|
|
898
|
+
char *trues[] = { "1", "t", "T", "true", "TRUE", "True" };
|
|
899
|
+
for (size_t i = 0; i < sizeof(trues)/sizeof(char*); i++) {
|
|
900
|
+
if (json_string_compare(json, trues[i]) == 0) return true;
|
|
901
|
+
}
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
default:
|
|
905
|
+
return false;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
struct jesc_buf {
|
|
910
|
+
uint8_t *esc;
|
|
911
|
+
size_t esclen;
|
|
912
|
+
size_t count;
|
|
913
|
+
};
|
|
914
|
+
|
|
915
|
+
static void jesc_append(struct jesc_buf *buf, uint8_t ch) {
|
|
916
|
+
if (buf->esclen > 1) {
|
|
917
|
+
*(buf->esc++) = ch;
|
|
918
|
+
buf->esclen--;
|
|
919
|
+
}
|
|
920
|
+
buf->count++;
|
|
921
|
+
}
|
|
922
|
+
static void jesc_append2(struct jesc_buf *buf, uint8_t c1, uint8_t c2) {
|
|
923
|
+
jesc_append(buf, c1);
|
|
924
|
+
jesc_append(buf, c2);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
static const uint8_t hexchars[] = "0123456789abcdef";
|
|
928
|
+
|
|
929
|
+
static void
|
|
930
|
+
jesc_append_ux(struct jesc_buf *buf, uint8_t c1, uint8_t c2, uint16_t x) {
|
|
931
|
+
jesc_append2(buf, c1, c2);
|
|
932
|
+
jesc_append2(buf, hexchars[x>>12&0xF], hexchars[x>>8&0xF]);
|
|
933
|
+
jesc_append2(buf, hexchars[x>>4&0xF], hexchars[x>>0&0xF]);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
JSON_EXTERN
|
|
937
|
+
size_t json_escapen(const char *str, size_t len, char *esc, size_t n) {
|
|
938
|
+
uint8_t cpbuf[4];
|
|
939
|
+
struct jesc_buf buf = { .esc = (uint8_t*)esc, .esclen = n };
|
|
940
|
+
jesc_append(&buf, '"');
|
|
941
|
+
for (size_t i = 0; i < len; i++) {
|
|
942
|
+
uint32_t c = (uint8_t)str[i];
|
|
943
|
+
if (c < ' ') {
|
|
944
|
+
switch (c) {
|
|
945
|
+
case '\n': jesc_append2(&buf, '\\', 'n'); break;
|
|
946
|
+
case '\b': jesc_append2(&buf, '\\', 'b'); break;
|
|
947
|
+
case '\f': jesc_append2(&buf, '\\', 'f'); break;
|
|
948
|
+
case '\r': jesc_append2(&buf, '\\', 'r'); break;
|
|
949
|
+
case '\t': jesc_append2(&buf, '\\', 't'); break;
|
|
950
|
+
default: jesc_append_ux(&buf, '\\', 'u', c);
|
|
951
|
+
}
|
|
952
|
+
} else if (c == '>' || c == '<' || c == '&') {
|
|
953
|
+
// make web safe
|
|
954
|
+
jesc_append_ux(&buf, '\\', 'u', c);
|
|
955
|
+
} else if (c == '\\') {
|
|
956
|
+
jesc_append2(&buf, '\\', '\\');
|
|
957
|
+
} else if (c == '"') {
|
|
958
|
+
jesc_append2(&buf, '\\', '"');
|
|
959
|
+
} else if (c > 127) {
|
|
960
|
+
struct vutf8res res = vutf8((uint8_t*)(str+i), len-i);
|
|
961
|
+
if (res.n == 0) {
|
|
962
|
+
res.n = 1;
|
|
963
|
+
res.cp = 0xfffd;
|
|
964
|
+
}
|
|
965
|
+
int cpn = encode_codepoint(cpbuf, res.cp);
|
|
966
|
+
for (int j = 0; j < cpn; j++) {
|
|
967
|
+
jesc_append(&buf, cpbuf[j]);
|
|
968
|
+
}
|
|
969
|
+
i = i + res.n - 1;
|
|
970
|
+
} else {
|
|
971
|
+
jesc_append(&buf, str[i]);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
jesc_append(&buf, '"');
|
|
975
|
+
if (buf.esclen > 0) {
|
|
976
|
+
// add to null terminator
|
|
977
|
+
*(buf.esc++) = '\0';
|
|
978
|
+
buf.esclen--;
|
|
979
|
+
}
|
|
980
|
+
return buf.count;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
JSON_EXTERN size_t json_escape(const char *str, char *esc, size_t n) {
|
|
984
|
+
return json_escapen(str, str?strlen(str):0, esc, n);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
JSON_EXTERN
|
|
988
|
+
struct json json_getn(const char *json_str, size_t len, const char *path) {
|
|
989
|
+
if (!path) return (struct json) { 0 };
|
|
990
|
+
struct json json = json_parsen(json_str, len);
|
|
991
|
+
int i = 0;
|
|
992
|
+
bool end = false;
|
|
993
|
+
char *p = (char*)path;
|
|
994
|
+
for (; !end && json_exists(json); i++) {
|
|
995
|
+
// get the next component
|
|
996
|
+
const char *key = p;
|
|
997
|
+
while (*p && *p != '.') p++;
|
|
998
|
+
size_t klen = p-key;
|
|
999
|
+
if (*p == '.') p++;
|
|
1000
|
+
else if (!*p) end = true;
|
|
1001
|
+
enum json_type type = json_type(json);
|
|
1002
|
+
if (type == JSON_OBJECT) {
|
|
1003
|
+
json = json_object_getn(json, key, klen);
|
|
1004
|
+
} else if (type == JSON_ARRAY) {
|
|
1005
|
+
if (klen == 0) { i = 0; break; }
|
|
1006
|
+
char *end;
|
|
1007
|
+
size_t index = strtol(key, &end, 10);
|
|
1008
|
+
if (*end && *end != '.') { i = 0; break; }
|
|
1009
|
+
json = json_array_get(json, index);
|
|
1010
|
+
} else {
|
|
1011
|
+
i = 0;
|
|
1012
|
+
break;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
return i == 0 ? (struct json) { 0 } : json;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
JSON_EXTERN struct json json_get(const char *json_str, const char *path) {
|
|
1019
|
+
return json_getn(json_str, json_str?strlen(json_str):0, path);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
JSON_EXTERN bool json_string_is_escaped(struct json json) {
|
|
1023
|
+
return (jinfo(json)&IESC) == IESC;
|
|
1024
|
+
}
|