… | |
… | |
4 | |
4 | |
5 | #include <assert.h> |
5 | #include <assert.h> |
6 | #include <string.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
|
|
9 | #include <limits.h> |
9 | #include <float.h> |
10 | #include <float.h> |
10 | |
11 | |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | #endif |
14 | #endif |
… | |
… | |
15 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
18 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
19 | #endif |
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
20 | |
23 | |
21 | #define F_ASCII 0x00000001UL |
24 | #define F_ASCII 0x00000001UL |
22 | #define F_LATIN1 0x00000002UL |
25 | #define F_LATIN1 0x00000002UL |
23 | #define F_UTF8 0x00000004UL |
26 | #define F_UTF8 0x00000004UL |
24 | #define F_INDENT 0x00000008UL |
27 | #define F_INDENT 0x00000008UL |
… | |
… | |
50 | |
53 | |
51 | #define SB do { |
54 | #define SB do { |
52 | #define SE } while (0) |
55 | #define SE } while (0) |
53 | |
56 | |
54 | #if __GNUC__ >= 3 |
57 | #if __GNUC__ >= 3 |
55 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
58 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
56 | # define inline inline |
59 | # define INLINE static inline |
57 | #else |
60 | #else |
58 | # define expect(expr,value) (expr) |
61 | # define expect(expr,value) (expr) |
59 | # define inline static |
62 | # define INLINE static |
60 | #endif |
63 | #endif |
61 | |
64 | |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
65 | #define expect_false(expr) expect ((expr) != 0, 0) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
66 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
67 | |
|
|
68 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
69 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
70 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
64 | |
71 | |
65 | #ifdef USE_ITHREADS |
72 | #ifdef USE_ITHREADS |
66 | # define JSON_SLOW 1 |
73 | # define JSON_SLOW 1 |
67 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
74 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
68 | #else |
75 | #else |
… | |
… | |
71 | #endif |
78 | #endif |
72 | |
79 | |
73 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
74 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
75 | |
82 | |
|
|
83 | enum { |
|
|
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
|
|
85 | INCR_M_STR, // inside string |
|
|
86 | INCR_M_BS, // inside backslash |
|
|
87 | INCR_M_JSON // outside anything, count nesting |
|
|
88 | }; |
|
|
89 | |
|
|
90 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
|
|
91 | |
76 | typedef struct { |
92 | typedef struct { |
77 | U32 flags; |
93 | U32 flags; |
78 | SV *cb_object; |
94 | SV *cb_object; |
79 | HV *cb_sk_object; |
95 | HV *cb_sk_object; |
|
|
96 | |
|
|
97 | // for the incremental parser |
|
|
98 | SV *incr_text; // the source text so far |
|
|
99 | STRLEN incr_pos; // the current offset into the text |
|
|
100 | int incr_nest; // {[]}-nesting level |
|
|
101 | int incr_mode; |
80 | } JSON; |
102 | } JSON; |
81 | |
103 | |
82 | ///////////////////////////////////////////////////////////////////////////// |
104 | ///////////////////////////////////////////////////////////////////////////// |
83 | // utility functions |
105 | // utility functions |
84 | |
106 | |
85 | inline void |
107 | INLINE SV * |
|
|
108 | get_bool (const char *name) |
|
|
109 | { |
|
|
110 | SV *sv = get_sv (name, 1); |
|
|
111 | |
|
|
112 | SvREADONLY_on (sv); |
|
|
113 | SvREADONLY_on (SvRV (sv)); |
|
|
114 | |
|
|
115 | return sv; |
|
|
116 | } |
|
|
117 | |
|
|
118 | INLINE void |
86 | shrink (SV *sv) |
119 | shrink (SV *sv) |
87 | { |
120 | { |
88 | sv_utf8_downgrade (sv, 1); |
121 | sv_utf8_downgrade (sv, 1); |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
122 | if (SvLEN (sv) > SvCUR (sv) + 1) |
90 | { |
123 | { |
… | |
… | |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
132 | // decode an utf-8 character and return it, or (UV)-1 in |
100 | // case of an error. |
133 | // case of an error. |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
134 | // we special-case "safe" characters from U+80 .. U+7FF, |
102 | // but use the very good perl function to parse anything else. |
135 | // but use the very good perl function to parse anything else. |
103 | // note that we never call this function for a ascii codepoints |
136 | // note that we never call this function for a ascii codepoints |
104 | inline UV |
137 | INLINE UV |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
138 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
106 | { |
139 | { |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
140 | if (expect_true (len >= 2 |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
141 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
142 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
110 | { |
143 | { |
111 | *clen = 2; |
144 | *clen = 2; |
112 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
145 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
113 | } |
146 | } |
114 | else |
147 | else |
115 | { |
148 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
116 | *clen = (STRLEN)-1; |
149 | } |
117 | return (UV)-1; |
150 | |
118 | } |
151 | // likewise for encoding, also never called for ascii codepoints |
|
|
152 | // this function takes advantage of this fact, although current gccs |
|
|
153 | // seem to optimise the check for >= 0x80 away anyways |
|
|
154 | INLINE unsigned char * |
|
|
155 | encode_utf8 (unsigned char *s, UV ch) |
|
|
156 | { |
|
|
157 | if (expect_false (ch < 0x000080)) |
|
|
158 | *s++ = ch; |
|
|
159 | else if (expect_true (ch < 0x000800)) |
|
|
160 | *s++ = 0xc0 | ( ch >> 6), |
|
|
161 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
162 | else if ( ch < 0x010000) |
|
|
163 | *s++ = 0xe0 | ( ch >> 12), |
|
|
164 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
165 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
166 | else if ( ch < 0x110000) |
|
|
167 | *s++ = 0xf0 | ( ch >> 18), |
|
|
168 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
169 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
170 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
171 | |
|
|
172 | return s; |
119 | } |
173 | } |
120 | |
174 | |
121 | ///////////////////////////////////////////////////////////////////////////// |
175 | ///////////////////////////////////////////////////////////////////////////// |
122 | // encoder |
176 | // encoder |
123 | |
177 | |
… | |
… | |
128 | char *end; // SvEND (sv) |
182 | char *end; // SvEND (sv) |
129 | SV *sv; // result scalar |
183 | SV *sv; // result scalar |
130 | JSON json; |
184 | JSON json; |
131 | U32 indent; // indentation level |
185 | U32 indent; // indentation level |
132 | U32 maxdepth; // max. indentation/recursion level |
186 | U32 maxdepth; // max. indentation/recursion level |
|
|
187 | UV limit; // escape character values >= this value when encoding |
133 | } enc_t; |
188 | } enc_t; |
134 | |
189 | |
135 | inline void |
190 | INLINE void |
136 | need (enc_t *enc, STRLEN len) |
191 | need (enc_t *enc, STRLEN len) |
137 | { |
192 | { |
138 | if (expect_false (enc->cur + len >= enc->end)) |
193 | if (expect_false (enc->cur + len >= enc->end)) |
139 | { |
194 | { |
140 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
195 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
142 | enc->cur = SvPVX (enc->sv) + cur; |
197 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
198 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | } |
199 | } |
145 | } |
200 | } |
146 | |
201 | |
147 | inline void |
202 | INLINE void |
148 | encode_ch (enc_t *enc, char ch) |
203 | encode_ch (enc_t *enc, char ch) |
149 | { |
204 | { |
150 | need (enc, 1); |
205 | need (enc, 1); |
151 | *enc->cur++ = ch; |
206 | *enc->cur++ = ch; |
152 | } |
207 | } |
… | |
… | |
206 | { |
261 | { |
207 | uch = ch; |
262 | uch = ch; |
208 | clen = 1; |
263 | clen = 1; |
209 | } |
264 | } |
210 | |
265 | |
211 | if (uch > 0x10FFFFUL) |
266 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
212 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
213 | |
|
|
214 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
215 | { |
267 | { |
216 | if (uch > 0xFFFFUL) |
268 | if (uch >= 0x10000UL) |
217 | { |
269 | { |
|
|
270 | if (uch >= 0x110000UL) |
|
|
271 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
272 | |
218 | need (enc, len += 11); |
273 | need (enc, len += 11); |
219 | sprintf (enc->cur, "\\u%04x\\u%04x", |
274 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
275 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
276 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | enc->cur += 12; |
277 | enc->cur += 12; |
… | |
… | |
250 | while (--clen); |
305 | while (--clen); |
251 | } |
306 | } |
252 | else |
307 | else |
253 | { |
308 | { |
254 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
309 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
255 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
310 | enc->cur = encode_utf8 (enc->cur, uch); |
256 | ++str; |
311 | ++str; |
257 | } |
312 | } |
258 | } |
313 | } |
259 | } |
314 | } |
260 | } |
315 | } |
261 | |
316 | |
262 | --len; |
317 | --len; |
263 | } |
318 | } |
264 | } |
319 | } |
265 | |
320 | |
266 | inline void |
321 | INLINE void |
267 | encode_indent (enc_t *enc) |
322 | encode_indent (enc_t *enc) |
268 | { |
323 | { |
269 | if (enc->json.flags & F_INDENT) |
324 | if (enc->json.flags & F_INDENT) |
270 | { |
325 | { |
271 | int spaces = enc->indent * INDENT_STEP; |
326 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
274 | memset (enc->cur, ' ', spaces); |
329 | memset (enc->cur, ' ', spaces); |
275 | enc->cur += spaces; |
330 | enc->cur += spaces; |
276 | } |
331 | } |
277 | } |
332 | } |
278 | |
333 | |
279 | inline void |
334 | INLINE void |
280 | encode_space (enc_t *enc) |
335 | encode_space (enc_t *enc) |
281 | { |
336 | { |
282 | need (enc, 1); |
337 | need (enc, 1); |
283 | encode_ch (enc, ' '); |
338 | encode_ch (enc, ' '); |
284 | } |
339 | } |
285 | |
340 | |
286 | inline void |
341 | INLINE void |
287 | encode_nl (enc_t *enc) |
342 | encode_nl (enc_t *enc) |
288 | { |
343 | { |
289 | if (enc->json.flags & F_INDENT) |
344 | if (enc->json.flags & F_INDENT) |
290 | { |
345 | { |
291 | need (enc, 1); |
346 | need (enc, 1); |
292 | encode_ch (enc, '\n'); |
347 | encode_ch (enc, '\n'); |
293 | } |
348 | } |
294 | } |
349 | } |
295 | |
350 | |
296 | inline void |
351 | INLINE void |
297 | encode_comma (enc_t *enc) |
352 | encode_comma (enc_t *enc) |
298 | { |
353 | { |
299 | encode_ch (enc, ','); |
354 | encode_ch (enc, ','); |
300 | |
355 | |
301 | if (enc->json.flags & F_INDENT) |
356 | if (enc->json.flags & F_INDENT) |
… | |
… | |
394 | |
449 | |
395 | static void |
450 | static void |
396 | encode_hv (enc_t *enc, HV *hv) |
451 | encode_hv (enc_t *enc, HV *hv) |
397 | { |
452 | { |
398 | HE *he; |
453 | HE *he; |
399 | int count; |
|
|
400 | |
454 | |
401 | if (enc->indent >= enc->maxdepth) |
455 | if (enc->indent >= enc->maxdepth) |
402 | croak ("data structure too deep (hit recursion limit)"); |
456 | croak ("data structure too deep (hit recursion limit)"); |
403 | |
457 | |
404 | encode_ch (enc, '{'); |
458 | encode_ch (enc, '{'); |
… | |
… | |
619 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
673 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
620 | enc->cur += strlen (enc->cur); |
674 | enc->cur += strlen (enc->cur); |
621 | } |
675 | } |
622 | else if (SvIOKp (sv)) |
676 | else if (SvIOKp (sv)) |
623 | { |
677 | { |
624 | // we assume we can always read an IV as a UV |
678 | // we assume we can always read an IV as a UV and vice versa |
625 | if (SvUV (sv) & ~(UV)0x7fff) |
679 | // we assume two's complement |
626 | { |
680 | // we assume no aliasing issues in the union |
627 | // large integer, use the (rather slow) snprintf way. |
681 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
628 | need (enc, sizeof (UV) * 3); |
682 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
629 | enc->cur += |
|
|
630 | SvIsUV(sv) |
|
|
631 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
|
|
632 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
633 | } |
|
|
634 | else |
|
|
635 | { |
683 | { |
636 | // optimise the "small number case" |
684 | // optimise the "small number case" |
637 | // code will likely be branchless and use only a single multiplication |
685 | // code will likely be branchless and use only a single multiplication |
|
|
686 | // works for numbers up to 59074 |
638 | I32 i = SvIV (sv); |
687 | I32 i = SvIVX (sv); |
639 | U32 u; |
688 | U32 u; |
640 | char digit, nz = 0; |
689 | char digit, nz = 0; |
641 | |
690 | |
642 | need (enc, 6); |
691 | need (enc, 6); |
643 | |
692 | |
… | |
… | |
649 | |
698 | |
650 | // now output digit by digit, each time masking out the integer part |
699 | // now output digit by digit, each time masking out the integer part |
651 | // and multiplying by 5 while moving the decimal point one to the right, |
700 | // and multiplying by 5 while moving the decimal point one to the right, |
652 | // resulting in a net multiplication by 10. |
701 | // resulting in a net multiplication by 10. |
653 | // we always write the digit to memory but conditionally increment |
702 | // we always write the digit to memory but conditionally increment |
654 | // the pointer, to ease the usage of conditional move instructions. |
703 | // the pointer, to enable the use of conditional move instructions. |
655 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
704 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
656 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
705 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
657 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
706 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
658 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
707 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
659 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
708 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
709 | } |
|
|
710 | else |
|
|
711 | { |
|
|
712 | // large integer, use the (rather slow) snprintf way. |
|
|
713 | need (enc, IVUV_MAXCHARS); |
|
|
714 | enc->cur += |
|
|
715 | SvIsUV(sv) |
|
|
716 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
|
|
717 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
660 | } |
718 | } |
661 | } |
719 | } |
662 | else if (SvROK (sv)) |
720 | else if (SvROK (sv)) |
663 | encode_rv (enc, SvRV (sv)); |
721 | encode_rv (enc, SvRV (sv)); |
664 | else if (!SvOK (sv)) |
722 | else if (!SvOK (sv)) |
… | |
… | |
680 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
738 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
681 | enc.cur = SvPVX (enc.sv); |
739 | enc.cur = SvPVX (enc.sv); |
682 | enc.end = SvEND (enc.sv); |
740 | enc.end = SvEND (enc.sv); |
683 | enc.indent = 0; |
741 | enc.indent = 0; |
684 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
742 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
743 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
744 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
745 | : 0x110000UL; |
685 | |
746 | |
686 | SvPOK_only (enc.sv); |
747 | SvPOK_only (enc.sv); |
687 | encode_sv (&enc, scalar); |
748 | encode_sv (&enc, scalar); |
688 | |
749 | |
689 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
750 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
710 | JSON json; |
771 | JSON json; |
711 | U32 depth; // recursion depth |
772 | U32 depth; // recursion depth |
712 | U32 maxdepth; // recursion depth limit |
773 | U32 maxdepth; // recursion depth limit |
713 | } dec_t; |
774 | } dec_t; |
714 | |
775 | |
715 | inline void |
776 | INLINE void |
716 | decode_comment (dec_t *dec) |
777 | decode_comment (dec_t *dec) |
717 | { |
778 | { |
718 | // only '#'-style comments allowed a.t.m. |
779 | // only '#'-style comments allowed a.t.m. |
719 | |
780 | |
720 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
781 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
721 | ++dec->cur; |
782 | ++dec->cur; |
722 | } |
783 | } |
723 | |
784 | |
724 | inline void |
785 | INLINE void |
725 | decode_ws (dec_t *dec) |
786 | decode_ws (dec_t *dec) |
726 | { |
787 | { |
727 | for (;;) |
788 | for (;;) |
728 | { |
789 | { |
729 | char ch = *dec->cur; |
790 | char ch = *dec->cur; |
… | |
… | |
855 | |
916 | |
856 | if (hi >= 0x80) |
917 | if (hi >= 0x80) |
857 | { |
918 | { |
858 | utf8 = 1; |
919 | utf8 = 1; |
859 | |
920 | |
860 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
921 | cur = encode_utf8 (cur, hi); |
861 | } |
922 | } |
862 | else |
923 | else |
863 | *cur++ = hi; |
924 | *cur++ = hi; |
864 | } |
925 | } |
865 | break; |
926 | break; |
… | |
… | |
867 | default: |
928 | default: |
868 | --dec_cur; |
929 | --dec_cur; |
869 | ERR ("illegal backslash escape sequence in string"); |
930 | ERR ("illegal backslash escape sequence in string"); |
870 | } |
931 | } |
871 | } |
932 | } |
872 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
933 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
873 | *cur++ = ch; |
934 | *cur++ = ch; |
874 | else if (ch >= 0x80) |
935 | else if (ch >= 0x80) |
875 | { |
936 | { |
876 | STRLEN clen; |
937 | STRLEN clen; |
877 | UV uch; |
938 | UV uch; |
… | |
… | |
1000 | |
1061 | |
1001 | if (!is_nv) |
1062 | if (!is_nv) |
1002 | { |
1063 | { |
1003 | int len = dec->cur - start; |
1064 | int len = dec->cur - start; |
1004 | |
1065 | |
1005 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1066 | // special case the rather common 1..5-digit-int case |
1006 | if (*start == '-') |
1067 | if (*start == '-') |
1007 | switch (len) |
1068 | switch (len) |
1008 | { |
1069 | { |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1070 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1071 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1011 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1072 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1012 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1073 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1074 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1013 | } |
1075 | } |
1014 | else |
1076 | else |
1015 | switch (len) |
1077 | switch (len) |
1016 | { |
1078 | { |
1017 | case 1: return newSViv ( start [0] - '0' * 1); |
1079 | case 1: return newSViv ( start [0] - '0' * 1); |
1018 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1080 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1019 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1081 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1020 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1082 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1083 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1021 | } |
1084 | } |
1022 | |
1085 | |
1023 | { |
1086 | { |
1024 | UV uv; |
1087 | UV uv; |
1025 | int numtype = grok_number (start, len, &uv); |
1088 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1133 | char *p = dec->cur; |
1196 | char *p = dec->cur; |
1134 | char *e = p + 24; // only try up to 24 bytes |
1197 | char *e = p + 24; // only try up to 24 bytes |
1135 | |
1198 | |
1136 | for (;;) |
1199 | for (;;) |
1137 | { |
1200 | { |
1138 | // the >= 0x80 is true on most architectures |
1201 | // the >= 0x80 is false on most architectures |
1139 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1202 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1140 | { |
1203 | { |
1141 | // slow path, back up and use decode_str |
1204 | // slow path, back up and use decode_str |
1142 | SV *key = decode_str (dec); |
1205 | SV *key = decode_str (dec); |
1143 | if (!key) |
1206 | if (!key) |
… | |
… | |
1274 | |
1337 | |
1275 | static SV * |
1338 | static SV * |
1276 | decode_sv (dec_t *dec) |
1339 | decode_sv (dec_t *dec) |
1277 | { |
1340 | { |
1278 | // the beauty of JSON: you need exactly one character lookahead |
1341 | // the beauty of JSON: you need exactly one character lookahead |
1279 | // to parse anything. |
1342 | // to parse everything. |
1280 | switch (*dec->cur) |
1343 | switch (*dec->cur) |
1281 | { |
1344 | { |
1282 | case '"': ++dec->cur; return decode_str (dec); |
1345 | case '"': ++dec->cur; return decode_str (dec); |
1283 | case '[': ++dec->cur; return decode_av (dec); |
1346 | case '[': ++dec->cur; return decode_av (dec); |
1284 | case '{': ++dec->cur; return decode_hv (dec); |
1347 | case '{': ++dec->cur; return decode_hv (dec); |
1285 | |
1348 | |
1286 | case '-': |
1349 | case '-': |
1287 | case '0': case '1': case '2': case '3': case '4': |
1350 | case '0': case '1': case '2': case '3': case '4': |
1288 | case '5': case '6': case '7': case '8': case '9': |
1351 | case '5': case '6': case '7': case '8': case '9': |
1289 | return decode_num (dec); |
1352 | return decode_num (dec); |
… | |
… | |
1291 | case 't': |
1354 | case 't': |
1292 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1355 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1293 | { |
1356 | { |
1294 | dec->cur += 4; |
1357 | dec->cur += 4; |
1295 | #if JSON_SLOW |
1358 | #if JSON_SLOW |
1296 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
1359 | json_true = get_bool ("JSON::XS::true"); |
1297 | #endif |
1360 | #endif |
1298 | return SvREFCNT_inc (json_true); |
1361 | return newSVsv (json_true); |
1299 | } |
1362 | } |
1300 | else |
1363 | else |
1301 | ERR ("'true' expected"); |
1364 | ERR ("'true' expected"); |
1302 | |
1365 | |
1303 | break; |
1366 | break; |
… | |
… | |
1305 | case 'f': |
1368 | case 'f': |
1306 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1369 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1307 | { |
1370 | { |
1308 | dec->cur += 5; |
1371 | dec->cur += 5; |
1309 | #if JSON_SLOW |
1372 | #if JSON_SLOW |
1310 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1373 | json_false = get_bool ("JSON::XS::false"); |
1311 | #endif |
1374 | #endif |
1312 | return SvREFCNT_inc (json_false); |
1375 | return newSVsv (json_false); |
1313 | } |
1376 | } |
1314 | else |
1377 | else |
1315 | ERR ("'false' expected"); |
1378 | ERR ("'false' expected"); |
1316 | |
1379 | |
1317 | break; |
1380 | break; |
… | |
… | |
1335 | fail: |
1398 | fail: |
1336 | return 0; |
1399 | return 0; |
1337 | } |
1400 | } |
1338 | |
1401 | |
1339 | static SV * |
1402 | static SV * |
1340 | decode_json (SV *string, JSON *json, UV *offset_return) |
1403 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1341 | { |
1404 | { |
1342 | dec_t dec; |
1405 | dec_t dec; |
1343 | UV offset; |
1406 | STRLEN offset; |
1344 | SV *sv; |
1407 | SV *sv; |
1345 | |
1408 | |
1346 | SvGETMAGIC (string); |
1409 | SvGETMAGIC (string); |
1347 | SvUPGRADE (string, SVt_PV); |
1410 | SvUPGRADE (string, SVt_PV); |
1348 | |
1411 | |
… | |
… | |
1421 | |
1484 | |
1422 | return sv; |
1485 | return sv; |
1423 | } |
1486 | } |
1424 | |
1487 | |
1425 | ///////////////////////////////////////////////////////////////////////////// |
1488 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1489 | // incremental parser |
|
|
1490 | |
|
|
1491 | static void |
|
|
1492 | incr_parse (JSON *self) |
|
|
1493 | { |
|
|
1494 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1495 | |
|
|
1496 | for (;;) |
|
|
1497 | { |
|
|
1498 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
|
|
1499 | switch (self->incr_mode) |
|
|
1500 | { |
|
|
1501 | // only used for intiial whitespace skipping |
|
|
1502 | case INCR_M_WS: |
|
|
1503 | for (;;) |
|
|
1504 | { |
|
|
1505 | if (*p > 0x20) |
|
|
1506 | { |
|
|
1507 | self->incr_mode = INCR_M_JSON; |
|
|
1508 | goto incr_m_json; |
|
|
1509 | } |
|
|
1510 | else if (!*p) |
|
|
1511 | goto interrupt; |
|
|
1512 | |
|
|
1513 | ++p; |
|
|
1514 | } |
|
|
1515 | |
|
|
1516 | // skip a single char inside a string (for \\-processing) |
|
|
1517 | case INCR_M_BS: |
|
|
1518 | if (!*p) |
|
|
1519 | goto interrupt; |
|
|
1520 | |
|
|
1521 | ++p; |
|
|
1522 | self->incr_mode = INCR_M_STR; |
|
|
1523 | goto incr_m_str; |
|
|
1524 | |
|
|
1525 | // inside a string |
|
|
1526 | case INCR_M_STR: |
|
|
1527 | incr_m_str: |
|
|
1528 | for (;;) |
|
|
1529 | { |
|
|
1530 | if (*p == '"') |
|
|
1531 | { |
|
|
1532 | ++p; |
|
|
1533 | self->incr_mode = INCR_M_JSON; |
|
|
1534 | |
|
|
1535 | if (!self->incr_nest) |
|
|
1536 | goto interrupt; |
|
|
1537 | |
|
|
1538 | goto incr_m_json; |
|
|
1539 | } |
|
|
1540 | else if (*p == '\\') |
|
|
1541 | { |
|
|
1542 | ++p; // "virtually" consumes character after \ |
|
|
1543 | |
|
|
1544 | if (!*p) // if at end of string we have to switch modes |
|
|
1545 | { |
|
|
1546 | self->incr_mode = INCR_M_BS; |
|
|
1547 | goto interrupt; |
|
|
1548 | } |
|
|
1549 | } |
|
|
1550 | else if (!*p) |
|
|
1551 | goto interrupt; |
|
|
1552 | |
|
|
1553 | ++p; |
|
|
1554 | } |
|
|
1555 | |
|
|
1556 | // after initial ws, outside string |
|
|
1557 | case INCR_M_JSON: |
|
|
1558 | incr_m_json: |
|
|
1559 | for (;;) |
|
|
1560 | { |
|
|
1561 | switch (*p++) |
|
|
1562 | { |
|
|
1563 | case 0: |
|
|
1564 | --p; |
|
|
1565 | goto interrupt; |
|
|
1566 | |
|
|
1567 | case 0x09: |
|
|
1568 | case 0x0a: |
|
|
1569 | case 0x0d: |
|
|
1570 | case 0x20: |
|
|
1571 | if (!self->incr_nest) |
|
|
1572 | { |
|
|
1573 | --p; // do not eat the whitespace, let the next round do it |
|
|
1574 | goto interrupt; |
|
|
1575 | } |
|
|
1576 | break; |
|
|
1577 | |
|
|
1578 | case '"': |
|
|
1579 | self->incr_mode = INCR_M_STR; |
|
|
1580 | goto incr_m_str; |
|
|
1581 | |
|
|
1582 | case '[': |
|
|
1583 | case '{': |
|
|
1584 | ++self->incr_nest; |
|
|
1585 | break; |
|
|
1586 | |
|
|
1587 | case ']': |
|
|
1588 | case '}': |
|
|
1589 | if (!--self->incr_nest) |
|
|
1590 | goto interrupt; |
|
|
1591 | } |
|
|
1592 | } |
|
|
1593 | } |
|
|
1594 | |
|
|
1595 | modechange: |
|
|
1596 | ; |
|
|
1597 | } |
|
|
1598 | |
|
|
1599 | interrupt: |
|
|
1600 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1601 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
|
|
1602 | } |
|
|
1603 | |
|
|
1604 | ///////////////////////////////////////////////////////////////////////////// |
1426 | // XS interface functions |
1605 | // XS interface functions |
1427 | |
1606 | |
1428 | MODULE = JSON::XS PACKAGE = JSON::XS |
1607 | MODULE = JSON::XS PACKAGE = JSON::XS |
1429 | |
1608 | |
1430 | BOOT: |
1609 | BOOT: |
… | |
… | |
1439 | : -1; |
1618 | : -1; |
1440 | |
1619 | |
1441 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1620 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1442 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1621 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1443 | |
1622 | |
1444 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
1623 | json_true = get_bool ("JSON::XS::true"); |
1445 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1624 | json_false = get_bool ("JSON::XS::false"); |
1446 | } |
1625 | } |
1447 | |
1626 | |
1448 | PROTOTYPES: DISABLE |
1627 | PROTOTYPES: DISABLE |
1449 | |
1628 | |
1450 | void CLONE (...) |
1629 | void CLONE (...) |
… | |
… | |
1590 | XPUSHs (decode_json (jsonstr, self, 0)); |
1769 | XPUSHs (decode_json (jsonstr, self, 0)); |
1591 | |
1770 | |
1592 | void decode_prefix (JSON *self, SV *jsonstr) |
1771 | void decode_prefix (JSON *self, SV *jsonstr) |
1593 | PPCODE: |
1772 | PPCODE: |
1594 | { |
1773 | { |
1595 | UV offset; |
1774 | STRLEN offset; |
1596 | EXTEND (SP, 2); |
1775 | EXTEND (SP, 2); |
1597 | PUSHs (decode_json (jsonstr, self, &offset)); |
1776 | PUSHs (decode_json (jsonstr, self, &offset)); |
1598 | PUSHs (sv_2mortal (newSVuv (offset))); |
1777 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1778 | } |
|
|
1779 | |
|
|
1780 | void incr_parse (JSON *self, SV *jsonstr = 0) |
|
|
1781 | PPCODE: |
|
|
1782 | { |
|
|
1783 | if (!self->incr_text) |
|
|
1784 | self->incr_text = newSVpvn ("", 0); |
|
|
1785 | |
|
|
1786 | // append data, if any |
|
|
1787 | if (jsonstr) |
|
|
1788 | { |
|
|
1789 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
|
|
1790 | { |
|
|
1791 | /* utf-8-ness differs, need to upgrade */ |
|
|
1792 | sv_utf8_upgrade (self->incr_text); |
|
|
1793 | |
|
|
1794 | if (self->incr_pos) |
|
|
1795 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1796 | - (U8 *)SvPVX (self->incr_text); |
|
|
1797 | } |
|
|
1798 | |
|
|
1799 | { |
|
|
1800 | STRLEN len; |
|
|
1801 | const char *str = SvPV (jsonstr, len); |
|
|
1802 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
|
|
1803 | Move (str, SvEND (self->incr_text), len, char); |
|
|
1804 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
|
|
1805 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1806 | } |
|
|
1807 | } |
|
|
1808 | |
|
|
1809 | if (GIMME_V != G_VOID) |
|
|
1810 | do |
|
|
1811 | { |
|
|
1812 | STRLEN offset; |
|
|
1813 | |
|
|
1814 | if (!INCR_DONE (self)) |
|
|
1815 | { |
|
|
1816 | incr_parse (self); |
|
|
1817 | if (!INCR_DONE (self)) |
|
|
1818 | break; |
|
|
1819 | } |
|
|
1820 | |
|
|
1821 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
|
|
1822 | |
|
|
1823 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1824 | self->incr_pos -= offset; |
|
|
1825 | self->incr_nest = 0; |
|
|
1826 | self->incr_mode = 0; |
|
|
1827 | } |
|
|
1828 | while (GIMME_V == G_ARRAY); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | SV *incr_text (JSON *self) |
|
|
1832 | ATTRS: lvalue |
|
|
1833 | CODE: |
|
|
1834 | { |
|
|
1835 | if (self->incr_pos) |
|
|
1836 | croak ("incr_text can not be called when the incremental parser already started parsing"); |
|
|
1837 | |
|
|
1838 | RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
|
|
1839 | } |
|
|
1840 | OUTPUT: |
|
|
1841 | RETVAL |
|
|
1842 | |
|
|
1843 | void incr_skip (JSON *self) |
|
|
1844 | CODE: |
|
|
1845 | { |
|
|
1846 | if (self->incr_pos) |
|
|
1847 | { |
|
|
1848 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos); |
|
|
1849 | self->incr_pos = 0; |
|
|
1850 | self->incr_nest = 0; |
|
|
1851 | self->incr_mode = 0; |
|
|
1852 | } |
1599 | } |
1853 | } |
1600 | |
1854 | |
1601 | void DESTROY (JSON *self) |
1855 | void DESTROY (JSON *self) |
1602 | CODE: |
1856 | CODE: |
1603 | SvREFCNT_dec (self->cb_sk_object); |
1857 | SvREFCNT_dec (self->cb_sk_object); |
1604 | SvREFCNT_dec (self->cb_object); |
1858 | SvREFCNT_dec (self->cb_object); |
|
|
1859 | SvREFCNT_dec (self->incr_text); |
1605 | |
1860 | |
1606 | PROTOTYPES: ENABLE |
1861 | PROTOTYPES: ENABLE |
1607 | |
1862 | |
1608 | void to_json (SV *scalar) |
1863 | void encode_json (SV *scalar) |
|
|
1864 | ALIAS: |
|
|
1865 | to_json_ = 0 |
|
|
1866 | encode_json = F_UTF8 |
1609 | PPCODE: |
1867 | PPCODE: |
1610 | { |
1868 | { |
1611 | JSON json = { F_DEFAULT | F_UTF8 }; |
1869 | JSON json = { F_DEFAULT | ix }; |
1612 | XPUSHs (encode_json (scalar, &json)); |
1870 | XPUSHs (encode_json (scalar, &json)); |
1613 | } |
1871 | } |
1614 | |
1872 | |
1615 | void from_json (SV *jsonstr) |
1873 | void decode_json (SV *jsonstr) |
|
|
1874 | ALIAS: |
|
|
1875 | from_json_ = 0 |
|
|
1876 | decode_json = F_UTF8 |
1616 | PPCODE: |
1877 | PPCODE: |
1617 | { |
1878 | { |
1618 | JSON json = { F_DEFAULT | F_UTF8 }; |
1879 | JSON json = { F_DEFAULT | ix }; |
1619 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1880 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1620 | } |
1881 | } |
1621 | |
1882 | |
|
|
1883 | |