… | |
… | |
50 | |
50 | |
51 | #define SB do { |
51 | #define SB do { |
52 | #define SE } while (0) |
52 | #define SE } while (0) |
53 | |
53 | |
54 | #if __GNUC__ >= 3 |
54 | #if __GNUC__ >= 3 |
55 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
55 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
56 | # define inline inline |
56 | # define INLINE static inline |
57 | #else |
57 | #else |
58 | # define expect(expr,value) (expr) |
58 | # define expect(expr,value) (expr) |
59 | # define inline static |
59 | # define INLINE static |
60 | #endif |
60 | #endif |
61 | |
61 | |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
64 | |
|
|
65 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
66 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
67 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
64 | |
68 | |
65 | #ifdef USE_ITHREADS |
69 | #ifdef USE_ITHREADS |
66 | # define JSON_SLOW 1 |
70 | # define JSON_SLOW 1 |
67 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
68 | #else |
72 | #else |
… | |
… | |
80 | } JSON; |
84 | } JSON; |
81 | |
85 | |
82 | ///////////////////////////////////////////////////////////////////////////// |
86 | ///////////////////////////////////////////////////////////////////////////// |
83 | // utility functions |
87 | // utility functions |
84 | |
88 | |
85 | inline void |
89 | INLINE void |
86 | shrink (SV *sv) |
90 | shrink (SV *sv) |
87 | { |
91 | { |
88 | sv_utf8_downgrade (sv, 1); |
92 | sv_utf8_downgrade (sv, 1); |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
93 | if (SvLEN (sv) > SvCUR (sv) + 1) |
90 | { |
94 | { |
… | |
… | |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
103 | // decode an utf-8 character and return it, or (UV)-1 in |
100 | // case of an error. |
104 | // case of an error. |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
105 | // we special-case "safe" characters from U+80 .. U+7FF, |
102 | // but use the very good perl function to parse anything else. |
106 | // but use the very good perl function to parse anything else. |
103 | // note that we never call this function for a ascii codepoints |
107 | // note that we never call this function for a ascii codepoints |
104 | inline UV |
108 | INLINE UV |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
109 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
106 | { |
110 | { |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
111 | if (expect_true (len >= 2 |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
112 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
113 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
110 | { |
114 | { |
111 | *clen = 2; |
115 | *clen = 2; |
112 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
116 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
113 | } |
117 | } |
114 | else |
118 | else |
115 | { |
119 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
116 | *clen = (STRLEN)-1; |
120 | } |
117 | return (UV)-1; |
121 | |
118 | } |
122 | // likewise for encoding, also never called for ascii codepoints |
|
|
123 | // this function takes advantage of this fact, although current gccs |
|
|
124 | // seem to optimise the check for >= 0x80 away anyways |
|
|
125 | INLINE unsigned char * |
|
|
126 | encode_utf8 (unsigned char *s, UV ch) |
|
|
127 | { |
|
|
128 | if (expect_false (ch < 0x000080)) |
|
|
129 | *s++ = ch; |
|
|
130 | else if (expect_true (ch < 0x000800)) |
|
|
131 | *s++ = 0xc0 | ( ch >> 6), |
|
|
132 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
133 | else if ( ch < 0x010000) |
|
|
134 | *s++ = 0xe0 | ( ch >> 12), |
|
|
135 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
136 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
137 | else if ( ch < 0x110000) |
|
|
138 | *s++ = 0xf0 | ( ch >> 18), |
|
|
139 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
140 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
141 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
142 | |
|
|
143 | return s; |
119 | } |
144 | } |
120 | |
145 | |
121 | ///////////////////////////////////////////////////////////////////////////// |
146 | ///////////////////////////////////////////////////////////////////////////// |
122 | // encoder |
147 | // encoder |
123 | |
148 | |
… | |
… | |
128 | char *end; // SvEND (sv) |
153 | char *end; // SvEND (sv) |
129 | SV *sv; // result scalar |
154 | SV *sv; // result scalar |
130 | JSON json; |
155 | JSON json; |
131 | U32 indent; // indentation level |
156 | U32 indent; // indentation level |
132 | U32 maxdepth; // max. indentation/recursion level |
157 | U32 maxdepth; // max. indentation/recursion level |
|
|
158 | UV limit; // escape character values >= this value when encoding |
133 | } enc_t; |
159 | } enc_t; |
134 | |
160 | |
135 | inline void |
161 | INLINE void |
136 | need (enc_t *enc, STRLEN len) |
162 | need (enc_t *enc, STRLEN len) |
137 | { |
163 | { |
138 | if (expect_false (enc->cur + len >= enc->end)) |
164 | if (expect_false (enc->cur + len >= enc->end)) |
139 | { |
165 | { |
140 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
166 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
142 | enc->cur = SvPVX (enc->sv) + cur; |
168 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
169 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | } |
170 | } |
145 | } |
171 | } |
146 | |
172 | |
147 | inline void |
173 | INLINE void |
148 | encode_ch (enc_t *enc, char ch) |
174 | encode_ch (enc_t *enc, char ch) |
149 | { |
175 | { |
150 | need (enc, 1); |
176 | need (enc, 1); |
151 | *enc->cur++ = ch; |
177 | *enc->cur++ = ch; |
152 | } |
178 | } |
… | |
… | |
206 | { |
232 | { |
207 | uch = ch; |
233 | uch = ch; |
208 | clen = 1; |
234 | clen = 1; |
209 | } |
235 | } |
210 | |
236 | |
211 | if (uch > 0x10FFFFUL) |
237 | 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 | { |
238 | { |
216 | if (uch > 0xFFFFUL) |
239 | if (uch >= 0x10000UL) |
217 | { |
240 | { |
|
|
241 | if (uch >= 0x110000UL) |
|
|
242 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
243 | |
218 | need (enc, len += 11); |
244 | need (enc, len += 11); |
219 | sprintf (enc->cur, "\\u%04x\\u%04x", |
245 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
246 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
247 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | enc->cur += 12; |
248 | enc->cur += 12; |
… | |
… | |
250 | while (--clen); |
276 | while (--clen); |
251 | } |
277 | } |
252 | else |
278 | else |
253 | { |
279 | { |
254 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
280 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
255 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
281 | enc->cur = encode_utf8 (enc->cur, uch); |
256 | ++str; |
282 | ++str; |
257 | } |
283 | } |
258 | } |
284 | } |
259 | } |
285 | } |
260 | } |
286 | } |
261 | |
287 | |
262 | --len; |
288 | --len; |
263 | } |
289 | } |
264 | } |
290 | } |
265 | |
291 | |
266 | inline void |
292 | INLINE void |
267 | encode_indent (enc_t *enc) |
293 | encode_indent (enc_t *enc) |
268 | { |
294 | { |
269 | if (enc->json.flags & F_INDENT) |
295 | if (enc->json.flags & F_INDENT) |
270 | { |
296 | { |
271 | int spaces = enc->indent * INDENT_STEP; |
297 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
274 | memset (enc->cur, ' ', spaces); |
300 | memset (enc->cur, ' ', spaces); |
275 | enc->cur += spaces; |
301 | enc->cur += spaces; |
276 | } |
302 | } |
277 | } |
303 | } |
278 | |
304 | |
279 | inline void |
305 | INLINE void |
280 | encode_space (enc_t *enc) |
306 | encode_space (enc_t *enc) |
281 | { |
307 | { |
282 | need (enc, 1); |
308 | need (enc, 1); |
283 | encode_ch (enc, ' '); |
309 | encode_ch (enc, ' '); |
284 | } |
310 | } |
285 | |
311 | |
286 | inline void |
312 | INLINE void |
287 | encode_nl (enc_t *enc) |
313 | encode_nl (enc_t *enc) |
288 | { |
314 | { |
289 | if (enc->json.flags & F_INDENT) |
315 | if (enc->json.flags & F_INDENT) |
290 | { |
316 | { |
291 | need (enc, 1); |
317 | need (enc, 1); |
292 | encode_ch (enc, '\n'); |
318 | encode_ch (enc, '\n'); |
293 | } |
319 | } |
294 | } |
320 | } |
295 | |
321 | |
296 | inline void |
322 | INLINE void |
297 | encode_comma (enc_t *enc) |
323 | encode_comma (enc_t *enc) |
298 | { |
324 | { |
299 | encode_ch (enc, ','); |
325 | encode_ch (enc, ','); |
300 | |
326 | |
301 | if (enc->json.flags & F_INDENT) |
327 | if (enc->json.flags & F_INDENT) |
… | |
… | |
312 | int i, len = av_len (av); |
338 | int i, len = av_len (av); |
313 | |
339 | |
314 | if (enc->indent >= enc->maxdepth) |
340 | if (enc->indent >= enc->maxdepth) |
315 | croak ("data structure too deep (hit recursion limit)"); |
341 | croak ("data structure too deep (hit recursion limit)"); |
316 | |
342 | |
317 | encode_ch (enc, '['); encode_nl (enc); |
343 | encode_ch (enc, '['); |
318 | ++enc->indent; |
344 | |
|
|
345 | if (len >= 0) |
|
|
346 | { |
|
|
347 | encode_nl (enc); ++enc->indent; |
319 | |
348 | |
320 | for (i = 0; i <= len; ++i) |
349 | for (i = 0; i <= len; ++i) |
321 | { |
350 | { |
322 | SV **svp = av_fetch (av, i, 0); |
351 | SV **svp = av_fetch (av, i, 0); |
323 | |
352 | |
324 | encode_indent (enc); |
353 | encode_indent (enc); |
325 | |
354 | |
326 | if (svp) |
355 | if (svp) |
327 | encode_sv (enc, *svp); |
356 | encode_sv (enc, *svp); |
328 | else |
357 | else |
329 | encode_str (enc, "null", 4, 0); |
358 | encode_str (enc, "null", 4, 0); |
330 | |
359 | |
331 | if (i < len) |
360 | if (i < len) |
332 | encode_comma (enc); |
361 | encode_comma (enc); |
333 | } |
362 | } |
334 | |
363 | |
|
|
364 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
365 | } |
|
|
366 | |
335 | encode_nl (enc); |
367 | encode_ch (enc, ']'); |
336 | |
|
|
337 | --enc->indent; |
|
|
338 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
339 | } |
368 | } |
340 | |
369 | |
341 | static void |
370 | static void |
342 | encode_hk (enc_t *enc, HE *he) |
371 | encode_hk (enc_t *enc, HE *he) |
343 | { |
372 | { |
… | |
… | |
396 | int count; |
425 | int count; |
397 | |
426 | |
398 | if (enc->indent >= enc->maxdepth) |
427 | if (enc->indent >= enc->maxdepth) |
399 | croak ("data structure too deep (hit recursion limit)"); |
428 | croak ("data structure too deep (hit recursion limit)"); |
400 | |
429 | |
401 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
430 | encode_ch (enc, '{'); |
402 | |
431 | |
403 | // for canonical output we have to sort by keys first |
432 | // for canonical output we have to sort by keys first |
404 | // actually, this is mostly due to the stupid so-called |
433 | // actually, this is mostly due to the stupid so-called |
405 | // security workaround added somewhere in 5.8.x. |
434 | // security workaround added somewhere in 5.8.x. |
406 | // that randomises hash orderings |
435 | // that randomises hash orderings |
… | |
… | |
459 | |
488 | |
460 | FREETMPS; |
489 | FREETMPS; |
461 | LEAVE; |
490 | LEAVE; |
462 | } |
491 | } |
463 | |
492 | |
|
|
493 | encode_nl (enc); ++enc->indent; |
|
|
494 | |
464 | while (count--) |
495 | while (count--) |
465 | { |
496 | { |
466 | encode_indent (enc); |
497 | encode_indent (enc); |
467 | he = hes [count]; |
498 | he = hes [count]; |
468 | encode_hk (enc, he); |
499 | encode_hk (enc, he); |
469 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
500 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
470 | |
501 | |
471 | if (count) |
502 | if (count) |
472 | encode_comma (enc); |
503 | encode_comma (enc); |
473 | } |
504 | } |
|
|
505 | |
|
|
506 | encode_nl (enc); --enc->indent; encode_indent (enc); |
474 | } |
507 | } |
475 | } |
508 | } |
476 | else |
509 | else |
477 | { |
510 | { |
478 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
511 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
479 | if ((he = hv_iternext (hv))) |
512 | if ((he = hv_iternext (hv))) |
|
|
513 | { |
|
|
514 | encode_nl (enc); ++enc->indent; |
|
|
515 | |
480 | for (;;) |
516 | for (;;) |
481 | { |
517 | { |
482 | encode_indent (enc); |
518 | encode_indent (enc); |
483 | encode_hk (enc, he); |
519 | encode_hk (enc, he); |
484 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
520 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
485 | |
521 | |
486 | if (!(he = hv_iternext (hv))) |
522 | if (!(he = hv_iternext (hv))) |
487 | break; |
523 | break; |
488 | |
524 | |
489 | encode_comma (enc); |
525 | encode_comma (enc); |
490 | } |
526 | } |
491 | } |
|
|
492 | |
527 | |
|
|
528 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
529 | } |
|
|
530 | } |
|
|
531 | |
493 | encode_nl (enc); |
532 | encode_ch (enc, '}'); |
494 | |
|
|
495 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
496 | } |
533 | } |
497 | |
534 | |
498 | // encode objects, arrays and special \0=false and \1=true values. |
535 | // encode objects, arrays and special \0=false and \1=true values. |
499 | static void |
536 | static void |
500 | encode_rv (enc_t *enc, SV *sv) |
537 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
608 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
645 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
609 | enc->cur += strlen (enc->cur); |
646 | enc->cur += strlen (enc->cur); |
610 | } |
647 | } |
611 | else if (SvIOKp (sv)) |
648 | else if (SvIOKp (sv)) |
612 | { |
649 | { |
613 | // we assume we can always read an IV as a UV |
650 | // we assume we can always read an IV as a UV and vice versa |
614 | if (SvUV (sv) & ~(UV)0x7fff) |
651 | // we assume two's complement |
|
|
652 | // we assume no aliasing issues in the union |
|
|
653 | if (SvIsUV (sv) ? SvUVX (sv) > 59000 |
|
|
654 | : SvIVX (sv) > 59000 || SvIVX (sv) < -59000) |
615 | { |
655 | { |
616 | // large integer, use the (rather slow) snprintf way. |
656 | // large integer, use the (rather slow) snprintf way. |
617 | need (enc, sizeof (UV) * 3); |
657 | need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8 |
618 | enc->cur += |
658 | enc->cur += |
619 | SvIsUV(sv) |
659 | SvIsUV(sv) |
620 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
660 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
621 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
661 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
622 | } |
662 | } |
623 | else |
663 | else |
624 | { |
664 | { |
625 | // optimise the "small number case" |
665 | // optimise the "small number case" |
626 | // code will likely be branchless and use only a single multiplication |
666 | // code will likely be branchless and use only a single multiplication |
|
|
667 | // works for numbers up to 59074 |
627 | I32 i = SvIV (sv); |
668 | I32 i = SvIVX (sv); |
628 | U32 u; |
669 | U32 u; |
629 | char digit, nz = 0; |
670 | char digit, nz = 0; |
630 | |
671 | |
631 | need (enc, 6); |
672 | need (enc, 6); |
632 | |
673 | |
… | |
… | |
669 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
710 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
670 | enc.cur = SvPVX (enc.sv); |
711 | enc.cur = SvPVX (enc.sv); |
671 | enc.end = SvEND (enc.sv); |
712 | enc.end = SvEND (enc.sv); |
672 | enc.indent = 0; |
713 | enc.indent = 0; |
673 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
714 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
715 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
716 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
717 | : 0x110000UL; |
674 | |
718 | |
675 | SvPOK_only (enc.sv); |
719 | SvPOK_only (enc.sv); |
676 | encode_sv (&enc, scalar); |
720 | encode_sv (&enc, scalar); |
677 | |
721 | |
678 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
722 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
699 | JSON json; |
743 | JSON json; |
700 | U32 depth; // recursion depth |
744 | U32 depth; // recursion depth |
701 | U32 maxdepth; // recursion depth limit |
745 | U32 maxdepth; // recursion depth limit |
702 | } dec_t; |
746 | } dec_t; |
703 | |
747 | |
704 | inline void |
748 | INLINE void |
705 | decode_comment (dec_t *dec) |
749 | decode_comment (dec_t *dec) |
706 | { |
750 | { |
707 | // only '#'-style comments allowed a.t.m. |
751 | // only '#'-style comments allowed a.t.m. |
708 | |
752 | |
709 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
753 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
710 | ++dec->cur; |
754 | ++dec->cur; |
711 | } |
755 | } |
712 | |
756 | |
713 | inline void |
757 | INLINE void |
714 | decode_ws (dec_t *dec) |
758 | decode_ws (dec_t *dec) |
715 | { |
759 | { |
716 | for (;;) |
760 | for (;;) |
717 | { |
761 | { |
718 | char ch = *dec->cur; |
762 | char ch = *dec->cur; |
… | |
… | |
844 | |
888 | |
845 | if (hi >= 0x80) |
889 | if (hi >= 0x80) |
846 | { |
890 | { |
847 | utf8 = 1; |
891 | utf8 = 1; |
848 | |
892 | |
849 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
893 | cur = encode_utf8 (cur, hi); |
850 | } |
894 | } |
851 | else |
895 | else |
852 | *cur++ = hi; |
896 | *cur++ = hi; |
853 | } |
897 | } |
854 | break; |
898 | break; |
… | |
… | |
856 | default: |
900 | default: |
857 | --dec_cur; |
901 | --dec_cur; |
858 | ERR ("illegal backslash escape sequence in string"); |
902 | ERR ("illegal backslash escape sequence in string"); |
859 | } |
903 | } |
860 | } |
904 | } |
861 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
905 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
862 | *cur++ = ch; |
906 | *cur++ = ch; |
863 | else if (ch >= 0x80) |
907 | else if (ch >= 0x80) |
864 | { |
908 | { |
865 | STRLEN clen; |
909 | STRLEN clen; |
866 | UV uch; |
910 | UV uch; |
… | |
… | |
989 | |
1033 | |
990 | if (!is_nv) |
1034 | if (!is_nv) |
991 | { |
1035 | { |
992 | int len = dec->cur - start; |
1036 | int len = dec->cur - start; |
993 | |
1037 | |
994 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1038 | // special case the rather common 1..5-digit-int case |
995 | if (*start == '-') |
1039 | if (*start == '-') |
996 | switch (len) |
1040 | switch (len) |
997 | { |
1041 | { |
998 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1042 | case 2: return newSViv (-( start [1] - '0' * 1)); |
999 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1043 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1000 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1044 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1001 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1045 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1046 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1002 | } |
1047 | } |
1003 | else |
1048 | else |
1004 | switch (len) |
1049 | switch (len) |
1005 | { |
1050 | { |
1006 | case 1: return newSViv ( start [0] - '0' * 1); |
1051 | case 1: return newSViv ( start [0] - '0' * 1); |
1007 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1052 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1008 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1053 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1009 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1054 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1055 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1010 | } |
1056 | } |
1011 | |
1057 | |
1012 | { |
1058 | { |
1013 | UV uv; |
1059 | UV uv; |
1014 | int numtype = grok_number (start, len, &uv); |
1060 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1263 | |
1309 | |
1264 | static SV * |
1310 | static SV * |
1265 | decode_sv (dec_t *dec) |
1311 | decode_sv (dec_t *dec) |
1266 | { |
1312 | { |
1267 | // the beauty of JSON: you need exactly one character lookahead |
1313 | // the beauty of JSON: you need exactly one character lookahead |
1268 | // to parse anything. |
1314 | // to parse everything. |
1269 | switch (*dec->cur) |
1315 | switch (*dec->cur) |
1270 | { |
1316 | { |
1271 | case '"': ++dec->cur; return decode_str (dec); |
1317 | case '"': ++dec->cur; return decode_str (dec); |
1272 | case '[': ++dec->cur; return decode_av (dec); |
1318 | case '[': ++dec->cur; return decode_av (dec); |
1273 | case '{': ++dec->cur; return decode_hv (dec); |
1319 | case '{': ++dec->cur; return decode_hv (dec); |
1274 | |
1320 | |
1275 | case '-': |
1321 | case '-': |
1276 | case '0': case '1': case '2': case '3': case '4': |
1322 | case '0': case '1': case '2': case '3': case '4': |
1277 | case '5': case '6': case '7': case '8': case '9': |
1323 | case '5': case '6': case '7': case '8': case '9': |
1278 | return decode_num (dec); |
1324 | return decode_num (dec); |
… | |
… | |
1446 | { |
1492 | { |
1447 | SV *pv = NEWSV (0, sizeof (JSON)); |
1493 | SV *pv = NEWSV (0, sizeof (JSON)); |
1448 | SvPOK_only (pv); |
1494 | SvPOK_only (pv); |
1449 | Zero (SvPVX (pv), 1, JSON); |
1495 | Zero (SvPVX (pv), 1, JSON); |
1450 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1496 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1451 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1497 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1498 | newRV_noinc (pv), |
|
|
1499 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1500 | ))); |
1452 | } |
1501 | } |
1453 | |
1502 | |
1454 | void ascii (JSON *self, int enable = 1) |
1503 | void ascii (JSON *self, int enable = 1) |
1455 | ALIAS: |
1504 | ALIAS: |
1456 | ascii = F_ASCII |
1505 | ascii = F_ASCII |
… | |
… | |
1474 | self->flags &= ~ix; |
1523 | self->flags &= ~ix; |
1475 | |
1524 | |
1476 | XPUSHs (ST (0)); |
1525 | XPUSHs (ST (0)); |
1477 | } |
1526 | } |
1478 | |
1527 | |
|
|
1528 | void get_ascii (JSON *self) |
|
|
1529 | ALIAS: |
|
|
1530 | get_ascii = F_ASCII |
|
|
1531 | get_latin1 = F_LATIN1 |
|
|
1532 | get_utf8 = F_UTF8 |
|
|
1533 | get_indent = F_INDENT |
|
|
1534 | get_canonical = F_CANONICAL |
|
|
1535 | get_space_before = F_SPACE_BEFORE |
|
|
1536 | get_space_after = F_SPACE_AFTER |
|
|
1537 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1538 | get_shrink = F_SHRINK |
|
|
1539 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1540 | get_convert_blessed = F_CONV_BLESSED |
|
|
1541 | get_relaxed = F_RELAXED |
|
|
1542 | PPCODE: |
|
|
1543 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1544 | |
1479 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1545 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1480 | PPCODE: |
1546 | PPCODE: |
1481 | { |
1547 | { |
1482 | UV log2 = 0; |
1548 | UV log2 = 0; |
1483 | |
1549 | |
… | |
… | |
1489 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1555 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1490 | |
1556 | |
1491 | XPUSHs (ST (0)); |
1557 | XPUSHs (ST (0)); |
1492 | } |
1558 | } |
1493 | |
1559 | |
|
|
1560 | U32 get_max_depth (JSON *self) |
|
|
1561 | CODE: |
|
|
1562 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1563 | OUTPUT: |
|
|
1564 | RETVAL |
|
|
1565 | |
1494 | void max_size (JSON *self, UV max_size = 0) |
1566 | void max_size (JSON *self, UV max_size = 0) |
1495 | PPCODE: |
1567 | PPCODE: |
1496 | { |
1568 | { |
1497 | UV log2 = 0; |
1569 | UV log2 = 0; |
1498 | |
1570 | |
… | |
… | |
1504 | |
1576 | |
1505 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1577 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1506 | |
1578 | |
1507 | XPUSHs (ST (0)); |
1579 | XPUSHs (ST (0)); |
1508 | } |
1580 | } |
|
|
1581 | |
|
|
1582 | int get_max_size (JSON *self) |
|
|
1583 | CODE: |
|
|
1584 | RETVAL = DEC_SIZE (self->flags); |
|
|
1585 | OUTPUT: |
|
|
1586 | RETVAL |
1509 | |
1587 | |
1510 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1588 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1511 | PPCODE: |
1589 | PPCODE: |
1512 | { |
1590 | { |
1513 | SvREFCNT_dec (self->cb_object); |
1591 | SvREFCNT_dec (self->cb_object); |
… | |
… | |
1560 | SvREFCNT_dec (self->cb_sk_object); |
1638 | SvREFCNT_dec (self->cb_sk_object); |
1561 | SvREFCNT_dec (self->cb_object); |
1639 | SvREFCNT_dec (self->cb_object); |
1562 | |
1640 | |
1563 | PROTOTYPES: ENABLE |
1641 | PROTOTYPES: ENABLE |
1564 | |
1642 | |
1565 | void to_json (SV *scalar) |
1643 | void encode_json (SV *scalar) |
1566 | PPCODE: |
1644 | PPCODE: |
1567 | { |
1645 | { |
1568 | JSON json = { F_DEFAULT | F_UTF8 }; |
1646 | JSON json = { F_DEFAULT | F_UTF8 }; |
1569 | XPUSHs (encode_json (scalar, &json)); |
1647 | XPUSHs (encode_json (scalar, &json)); |
1570 | } |
1648 | } |
1571 | |
1649 | |
1572 | void from_json (SV *jsonstr) |
1650 | void decode_json (SV *jsonstr) |
1573 | PPCODE: |
1651 | PPCODE: |
1574 | { |
1652 | { |
1575 | JSON json = { F_DEFAULT | F_UTF8 }; |
1653 | JSON json = { F_DEFAULT | F_UTF8 }; |
1576 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1654 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1577 | } |
1655 | } |