… | |
… | |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
33 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
35 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
36 | |
36 | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
38 | |
38 | |
39 | #define SB do { |
39 | #define SB do { |
40 | #define SE } while (0) |
40 | #define SE } while (0) |
|
|
41 | |
|
|
42 | #if __GNUC__ >= 3 |
|
|
43 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
44 | # define inline inline |
|
|
45 | #else |
|
|
46 | # define expect(expr,value) (expr) |
|
|
47 | # define inline static |
|
|
48 | #endif |
|
|
49 | |
|
|
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
41 | |
52 | |
42 | static HV *json_stash; // JSON::XS:: |
53 | static HV *json_stash; // JSON::XS:: |
43 | |
54 | |
44 | ///////////////////////////////////////////////////////////////////////////// |
55 | ///////////////////////////////////////////////////////////////////////////// |
45 | // utility functions |
56 | // utility functions |
… | |
… | |
70 | // decode an utf-8 character and return it, or (UV)-1 in |
81 | // decode an utf-8 character and return it, or (UV)-1 in |
71 | // case of an error. |
82 | // case of an error. |
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
83 | // we special-case "safe" characters from U+80 .. U+7FF, |
73 | // but use the very good perl function to parse anything else. |
84 | // but use the very good perl function to parse anything else. |
74 | // note that we never call this function for a ascii codepoints |
85 | // note that we never call this function for a ascii codepoints |
75 | static UV |
86 | inline UV |
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
87 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
77 | { |
88 | { |
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
89 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
90 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
91 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
81 | { |
92 | { |
82 | *clen = 2; |
93 | *clen = 2; |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
94 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
… | |
… | |
101 | U32 flags; // F_* |
112 | U32 flags; // F_* |
102 | U32 indent; // indentation level |
113 | U32 indent; // indentation level |
103 | U32 maxdepth; // max. indentation/recursion level |
114 | U32 maxdepth; // max. indentation/recursion level |
104 | } enc_t; |
115 | } enc_t; |
105 | |
116 | |
106 | static void |
117 | inline void |
107 | need (enc_t *enc, STRLEN len) |
118 | need (enc_t *enc, STRLEN len) |
108 | { |
119 | { |
109 | if (enc->cur + len >= enc->end) |
120 | if (expect_false (enc->cur + len >= enc->end)) |
110 | { |
121 | { |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
122 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
112 | SvGROW (enc->sv, cur + len + 1); |
123 | SvGROW (enc->sv, cur + len + 1); |
113 | enc->cur = SvPVX (enc->sv) + cur; |
124 | enc->cur = SvPVX (enc->sv) + cur; |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
125 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
115 | } |
126 | } |
116 | } |
127 | } |
117 | |
128 | |
118 | static void |
129 | inline void |
119 | encode_ch (enc_t *enc, char ch) |
130 | encode_ch (enc_t *enc, char ch) |
120 | { |
131 | { |
121 | need (enc, 1); |
132 | need (enc, 1); |
122 | *enc->cur++ = ch; |
133 | *enc->cur++ = ch; |
123 | } |
134 | } |
… | |
… | |
131 | |
142 | |
132 | while (str < end) |
143 | while (str < end) |
133 | { |
144 | { |
134 | unsigned char ch = *(unsigned char *)str; |
145 | unsigned char ch = *(unsigned char *)str; |
135 | |
146 | |
136 | if (ch >= 0x20 && ch < 0x80) // most common case |
147 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
137 | { |
148 | { |
138 | if (ch == '"') // but with slow exceptions |
149 | if (expect_false (ch == '"')) // but with slow exceptions |
139 | { |
150 | { |
140 | need (enc, len += 1); |
151 | need (enc, len += 1); |
141 | *enc->cur++ = '\\'; |
152 | *enc->cur++ = '\\'; |
142 | *enc->cur++ = '"'; |
153 | *enc->cur++ = '"'; |
143 | } |
154 | } |
144 | else if (ch == '\\') |
155 | else if (expect_false (ch == '\\')) |
145 | { |
156 | { |
146 | need (enc, len += 1); |
157 | need (enc, len += 1); |
147 | *enc->cur++ = '\\'; |
158 | *enc->cur++ = '\\'; |
148 | *enc->cur++ = '\\'; |
159 | *enc->cur++ = '\\'; |
149 | } |
160 | } |
… | |
… | |
233 | |
244 | |
234 | --len; |
245 | --len; |
235 | } |
246 | } |
236 | } |
247 | } |
237 | |
248 | |
238 | static void |
249 | inline void |
239 | encode_indent (enc_t *enc) |
250 | encode_indent (enc_t *enc) |
240 | { |
251 | { |
241 | if (enc->flags & F_INDENT) |
252 | if (enc->flags & F_INDENT) |
242 | { |
253 | { |
243 | int spaces = enc->indent * INDENT_STEP; |
254 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
246 | memset (enc->cur, ' ', spaces); |
257 | memset (enc->cur, ' ', spaces); |
247 | enc->cur += spaces; |
258 | enc->cur += spaces; |
248 | } |
259 | } |
249 | } |
260 | } |
250 | |
261 | |
251 | static void |
262 | inline void |
252 | encode_space (enc_t *enc) |
263 | encode_space (enc_t *enc) |
253 | { |
264 | { |
254 | need (enc, 1); |
265 | need (enc, 1); |
255 | encode_ch (enc, ' '); |
266 | encode_ch (enc, ' '); |
256 | } |
267 | } |
257 | |
268 | |
258 | static void |
269 | inline void |
259 | encode_nl (enc_t *enc) |
270 | encode_nl (enc_t *enc) |
260 | { |
271 | { |
261 | if (enc->flags & F_INDENT) |
272 | if (enc->flags & F_INDENT) |
262 | { |
273 | { |
263 | need (enc, 1); |
274 | need (enc, 1); |
264 | encode_ch (enc, '\n'); |
275 | encode_ch (enc, '\n'); |
265 | } |
276 | } |
266 | } |
277 | } |
267 | |
278 | |
268 | static void |
279 | inline void |
269 | encode_comma (enc_t *enc) |
280 | encode_comma (enc_t *enc) |
270 | { |
281 | { |
271 | encode_ch (enc, ','); |
282 | encode_ch (enc, ','); |
272 | |
283 | |
273 | if (enc->flags & F_INDENT) |
284 | if (enc->flags & F_INDENT) |
… | |
… | |
372 | // actually, this is mostly due to the stupid so-called |
383 | // actually, this is mostly due to the stupid so-called |
373 | // security workaround added somewhere in 5.8.x. |
384 | // security workaround added somewhere in 5.8.x. |
374 | // that randomises hash orderings |
385 | // that randomises hash orderings |
375 | if (enc->flags & F_CANONICAL) |
386 | if (enc->flags & F_CANONICAL) |
376 | { |
387 | { |
377 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
378 | int fast = 1; |
388 | int fast = 1; |
|
|
389 | HE *he; |
|
|
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
391 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
392 | #else |
|
|
393 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
394 | #endif |
379 | |
395 | |
380 | i = 0; |
396 | i = 0; |
381 | while ((he = hv_iternext (hv))) |
397 | while ((he = hv_iternext (hv))) |
382 | { |
398 | { |
383 | hes [i++] = he; |
399 | hes [i++] = he; |
… | |
… | |
481 | encode_str (enc, str, len, SvUTF8 (sv)); |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
482 | encode_ch (enc, '"'); |
498 | encode_ch (enc, '"'); |
483 | } |
499 | } |
484 | else if (SvNOKp (sv)) |
500 | else if (SvNOKp (sv)) |
485 | { |
501 | { |
|
|
502 | // trust that perl will do the right thing w.r.t. JSON syntax. |
486 | need (enc, NV_DIG + 32); |
503 | need (enc, NV_DIG + 32); |
487 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
488 | enc->cur += strlen (enc->cur); |
505 | enc->cur += strlen (enc->cur); |
489 | } |
506 | } |
490 | else if (SvIOKp (sv)) |
507 | else if (SvIOKp (sv)) |
491 | { |
508 | { |
492 | need (enc, 64); |
509 | // we assume we can always read an IV as a UV |
|
|
510 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
511 | { |
|
|
512 | // large integer, use the (rather slow) snprintf way. |
|
|
513 | need (enc, sizeof (UV) * 3); |
493 | enc->cur += |
514 | enc->cur += |
494 | SvIsUV(sv) |
515 | SvIsUV(sv) |
495 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
516 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
496 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
517 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
518 | } |
|
|
519 | else |
|
|
520 | { |
|
|
521 | // optimise the "small number case" |
|
|
522 | // code will likely be branchless and use only a single multiplication |
|
|
523 | I32 i = SvIV (sv); |
|
|
524 | U32 u; |
|
|
525 | char digit, nz = 0; |
|
|
526 | |
|
|
527 | need (enc, 6); |
|
|
528 | |
|
|
529 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
530 | u = i < 0 ? -i : i; |
|
|
531 | |
|
|
532 | // convert to 4.28 fixed-point representation |
|
|
533 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
534 | |
|
|
535 | // now output digit by digit, each time masking out the integer part |
|
|
536 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
537 | // resulting in a net multiplication by 10. |
|
|
538 | // we always write the digit to memory but conditionally increment |
|
|
539 | // the pointer, to ease the usage of conditional move instructions. |
|
|
540 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
541 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
542 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
543 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
544 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
545 | } |
497 | } |
546 | } |
498 | else if (SvROK (sv)) |
547 | else if (SvROK (sv)) |
499 | encode_rv (enc, SvRV (sv)); |
548 | encode_rv (enc, SvRV (sv)); |
500 | else if (!SvOK (sv)) |
549 | else if (!SvOK (sv)) |
501 | encode_str (enc, "null", 4, 0); |
550 | encode_str (enc, "null", 4, 0); |
… | |
… | |
546 | U32 flags; // F_* |
595 | U32 flags; // F_* |
547 | U32 depth; // recursion depth |
596 | U32 depth; // recursion depth |
548 | U32 maxdepth; // recursion depth limit |
597 | U32 maxdepth; // recursion depth limit |
549 | } dec_t; |
598 | } dec_t; |
550 | |
599 | |
551 | static void |
600 | inline void |
552 | decode_ws (dec_t *dec) |
601 | decode_ws (dec_t *dec) |
553 | { |
602 | { |
554 | for (;;) |
603 | for (;;) |
555 | { |
604 | { |
556 | char ch = *dec->cur; |
605 | char ch = *dec->cur; |
… | |
… | |
582 | decode_4hex (dec_t *dec) |
631 | decode_4hex (dec_t *dec) |
583 | { |
632 | { |
584 | signed char d1, d2, d3, d4; |
633 | signed char d1, d2, d3, d4; |
585 | unsigned char *cur = (unsigned char *)dec->cur; |
634 | unsigned char *cur = (unsigned char *)dec->cur; |
586 | |
635 | |
587 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
636 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
588 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
637 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
589 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
638 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
590 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
639 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
591 | |
640 | |
592 | dec->cur += 4; |
641 | dec->cur += 4; |
593 | |
642 | |
594 | return ((UV)d1) << 12 |
643 | return ((UV)d1) << 12 |
595 | | ((UV)d2) << 8 |
644 | | ((UV)d2) << 8 |
… | |
… | |
603 | static SV * |
652 | static SV * |
604 | decode_str (dec_t *dec) |
653 | decode_str (dec_t *dec) |
605 | { |
654 | { |
606 | SV *sv = 0; |
655 | SV *sv = 0; |
607 | int utf8 = 0; |
656 | int utf8 = 0; |
|
|
657 | char *dec_cur = dec->cur; |
608 | |
658 | |
609 | do |
659 | do |
610 | { |
660 | { |
611 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
661 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
612 | char *cur = buf; |
662 | char *cur = buf; |
613 | |
663 | |
614 | do |
664 | do |
615 | { |
665 | { |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
666 | unsigned char ch = *(unsigned char *)dec_cur++; |
617 | |
667 | |
618 | if (ch == '"') |
668 | if (expect_false (ch == '"')) |
619 | { |
669 | { |
620 | --dec->cur; |
670 | --dec_cur; |
621 | break; |
671 | break; |
622 | } |
672 | } |
623 | else if (ch == '\\') |
673 | else if (expect_false (ch == '\\')) |
624 | { |
674 | { |
625 | switch (*dec->cur) |
675 | switch (*dec_cur) |
626 | { |
676 | { |
627 | case '\\': |
677 | case '\\': |
628 | case '/': |
678 | case '/': |
629 | case '"': *cur++ = *dec->cur++; break; |
679 | case '"': *cur++ = *dec_cur++; break; |
630 | |
680 | |
631 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
681 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
632 | case 't': ++dec->cur; *cur++ = '\011'; break; |
682 | case 't': ++dec_cur; *cur++ = '\011'; break; |
633 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
683 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
634 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
684 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
635 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
685 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
636 | |
686 | |
637 | case 'u': |
687 | case 'u': |
638 | { |
688 | { |
639 | UV lo, hi; |
689 | UV lo, hi; |
640 | ++dec->cur; |
690 | ++dec_cur; |
641 | |
691 | |
|
|
692 | dec->cur = dec_cur; |
642 | hi = decode_4hex (dec); |
693 | hi = decode_4hex (dec); |
|
|
694 | dec_cur = dec->cur; |
643 | if (hi == (UV)-1) |
695 | if (hi == (UV)-1) |
644 | goto fail; |
696 | goto fail; |
645 | |
697 | |
646 | // possibly a surrogate pair |
698 | // possibly a surrogate pair |
647 | if (hi >= 0xd800) |
699 | if (hi >= 0xd800) |
648 | if (hi < 0xdc00) |
700 | if (hi < 0xdc00) |
649 | { |
701 | { |
650 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
702 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
651 | ERR ("missing low surrogate character in surrogate pair"); |
703 | ERR ("missing low surrogate character in surrogate pair"); |
652 | |
704 | |
653 | dec->cur += 2; |
705 | dec_cur += 2; |
654 | |
706 | |
|
|
707 | dec->cur = dec_cur; |
655 | lo = decode_4hex (dec); |
708 | lo = decode_4hex (dec); |
|
|
709 | dec_cur = dec->cur; |
656 | if (lo == (UV)-1) |
710 | if (lo == (UV)-1) |
657 | goto fail; |
711 | goto fail; |
658 | |
712 | |
659 | if (lo < 0xdc00 || lo >= 0xe000) |
713 | if (lo < 0xdc00 || lo >= 0xe000) |
660 | ERR ("surrogate pair expected"); |
714 | ERR ("surrogate pair expected"); |
… | |
… | |
674 | *cur++ = hi; |
728 | *cur++ = hi; |
675 | } |
729 | } |
676 | break; |
730 | break; |
677 | |
731 | |
678 | default: |
732 | default: |
679 | --dec->cur; |
733 | --dec_cur; |
680 | ERR ("illegal backslash escape sequence in string"); |
734 | ERR ("illegal backslash escape sequence in string"); |
681 | } |
735 | } |
682 | } |
736 | } |
683 | else if (ch >= 0x20 && ch <= 0x7f) |
737 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
684 | *cur++ = ch; |
738 | *cur++ = ch; |
685 | else if (ch >= 0x80) |
739 | else if (ch >= 0x80) |
686 | { |
740 | { |
687 | STRLEN clen; |
741 | STRLEN clen; |
688 | UV uch; |
742 | UV uch; |
689 | |
743 | |
690 | --dec->cur; |
744 | --dec_cur; |
691 | |
745 | |
692 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
746 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
693 | if (clen == (STRLEN)-1) |
747 | if (clen == (STRLEN)-1) |
694 | ERR ("malformed UTF-8 character in JSON string"); |
748 | ERR ("malformed UTF-8 character in JSON string"); |
695 | |
749 | |
696 | do |
750 | do |
697 | *cur++ = *dec->cur++; |
751 | *cur++ = *dec_cur++; |
698 | while (--clen); |
752 | while (--clen); |
699 | |
753 | |
700 | utf8 = 1; |
754 | utf8 = 1; |
701 | } |
755 | } |
702 | else |
756 | else |
703 | { |
757 | { |
704 | --dec->cur; |
758 | --dec_cur; |
705 | |
759 | |
706 | if (!ch) |
760 | if (!ch) |
707 | ERR ("unexpected end of string while parsing JSON string"); |
761 | ERR ("unexpected end of string while parsing JSON string"); |
708 | else |
762 | else |
709 | ERR ("invalid character encountered while parsing JSON string"); |
763 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
722 | } |
776 | } |
723 | else |
777 | else |
724 | sv = newSVpvn (buf, len); |
778 | sv = newSVpvn (buf, len); |
725 | } |
779 | } |
726 | } |
780 | } |
727 | while (*dec->cur != '"'); |
781 | while (*dec_cur != '"'); |
728 | |
782 | |
729 | ++dec->cur; |
783 | ++dec_cur; |
730 | |
784 | |
731 | if (sv) |
785 | if (sv) |
732 | { |
786 | { |
733 | SvPOK_only (sv); |
787 | SvPOK_only (sv); |
734 | *SvEND (sv) = 0; |
788 | *SvEND (sv) = 0; |
… | |
… | |
737 | SvUTF8_on (sv); |
791 | SvUTF8_on (sv); |
738 | } |
792 | } |
739 | else |
793 | else |
740 | sv = newSVpvn ("", 0); |
794 | sv = newSVpvn ("", 0); |
741 | |
795 | |
|
|
796 | dec->cur = dec_cur; |
742 | return sv; |
797 | return sv; |
743 | |
798 | |
744 | fail: |
799 | fail: |
|
|
800 | dec->cur = dec_cur; |
745 | return 0; |
801 | return 0; |
746 | } |
802 | } |
747 | |
803 | |
748 | static SV * |
804 | static SV * |
749 | decode_num (dec_t *dec) |
805 | decode_num (dec_t *dec) |
… | |
… | |
807 | is_nv = 1; |
863 | is_nv = 1; |
808 | } |
864 | } |
809 | |
865 | |
810 | if (!is_nv) |
866 | if (!is_nv) |
811 | { |
867 | { |
812 | UV uv; |
868 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
813 | int numtype = grok_number (start, dec->cur - start, &uv); |
869 | if (*start == '-') |
814 | if (numtype & IS_NUMBER_IN_UV) |
870 | switch (dec->cur - start) |
815 | if (numtype & IS_NUMBER_NEG) |
|
|
816 | { |
871 | { |
817 | if (uv < (UV)IV_MIN) |
872 | case 2: return newSViv (-( start [1] - '0' )); |
818 | return newSViv (-(IV)uv); |
873 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
874 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
875 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
819 | } |
876 | } |
|
|
877 | else |
|
|
878 | switch (dec->cur - start) |
|
|
879 | { |
|
|
880 | case 1: return newSViv ( start [0] - '0' ); |
|
|
881 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
882 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
883 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
884 | } |
|
|
885 | |
|
|
886 | { |
|
|
887 | UV uv; |
|
|
888 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
889 | if (numtype & IS_NUMBER_IN_UV) |
|
|
890 | if (numtype & IS_NUMBER_NEG) |
|
|
891 | { |
|
|
892 | if (uv < (UV)IV_MIN) |
|
|
893 | return newSViv (-(IV)uv); |
|
|
894 | } |
820 | else |
895 | else |
821 | return newSVuv (uv); |
896 | return newSVuv (uv); |
|
|
897 | } |
822 | } |
898 | } |
823 | |
899 | |
824 | return newSVnv (Atof (start)); |
900 | return newSVnv (Atof (start)); |
825 | |
901 | |
826 | fail: |
902 | fail: |
… | |
… | |
983 | fail: |
1059 | fail: |
984 | return 0; |
1060 | return 0; |
985 | } |
1061 | } |
986 | |
1062 | |
987 | static SV * |
1063 | static SV * |
988 | decode_json (SV *string, U32 flags) |
1064 | decode_json (SV *string, U32 flags, UV *offset_return) |
989 | { |
1065 | { |
990 | dec_t dec; |
1066 | dec_t dec; |
|
|
1067 | UV offset; |
991 | SV *sv; |
1068 | SV *sv; |
992 | |
1069 | |
993 | SvGETMAGIC (string); |
1070 | SvGETMAGIC (string); |
994 | SvUPGRADE (string, SVt_PV); |
1071 | SvUPGRADE (string, SVt_PV); |
995 | |
1072 | |
… | |
… | |
1005 | dec.end = SvEND (string); |
1082 | dec.end = SvEND (string); |
1006 | dec.err = 0; |
1083 | dec.err = 0; |
1007 | dec.depth = 0; |
1084 | dec.depth = 0; |
1008 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1085 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1009 | |
1086 | |
1010 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1087 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1011 | sv = decode_sv (&dec); |
1088 | sv = decode_sv (&dec); |
1012 | |
1089 | |
|
|
1090 | if (!(offset_return || !sv)) |
|
|
1091 | { |
|
|
1092 | // check for trailing garbage |
|
|
1093 | decode_ws (&dec); |
|
|
1094 | |
|
|
1095 | if (*dec.cur) |
|
|
1096 | { |
|
|
1097 | dec.err = "garbage after JSON object"; |
|
|
1098 | SvREFCNT_dec (sv); |
|
|
1099 | sv = 0; |
|
|
1100 | } |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | if (offset_return || !sv) |
|
|
1104 | { |
|
|
1105 | offset = dec.flags & F_UTF8 |
|
|
1106 | ? dec.cur - SvPVX (string) |
|
|
1107 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1108 | |
|
|
1109 | if (offset_return) |
|
|
1110 | *offset_return = offset; |
|
|
1111 | } |
|
|
1112 | |
1013 | if (!sv) |
1113 | if (!sv) |
1014 | { |
1114 | { |
1015 | IV offset = dec.flags & F_UTF8 |
|
|
1016 | ? dec.cur - SvPVX (string) |
|
|
1017 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1018 | SV *uni = sv_newmortal (); |
1115 | SV *uni = sv_newmortal (); |
1019 | |
1116 | |
1020 | // horrible hack to silence warning inside pv_uni_display |
1117 | // horrible hack to silence warning inside pv_uni_display |
1021 | COP cop = *PL_curcop; |
1118 | COP cop = *PL_curcop; |
1022 | cop.cop_warnings = pWARN_NONE; |
1119 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1116 | PPCODE: |
1213 | PPCODE: |
1117 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1214 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1118 | |
1215 | |
1119 | void decode (SV *self, SV *jsonstr) |
1216 | void decode (SV *self, SV *jsonstr) |
1120 | PPCODE: |
1217 | PPCODE: |
1121 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1218 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1219 | |
|
|
1220 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1221 | PPCODE: |
|
|
1222 | { |
|
|
1223 | UV offset; |
|
|
1224 | EXTEND (SP, 2); |
|
|
1225 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1226 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1227 | } |
1122 | |
1228 | |
1123 | PROTOTYPES: ENABLE |
1229 | PROTOTYPES: ENABLE |
1124 | |
1230 | |
1125 | void to_json (SV *scalar) |
1231 | void to_json (SV *scalar) |
1126 | ALIAS: |
1232 | ALIAS: |
… | |
… | |
1130 | |
1236 | |
1131 | void from_json (SV *jsonstr) |
1237 | void from_json (SV *jsonstr) |
1132 | ALIAS: |
1238 | ALIAS: |
1133 | jsonToObj = 0 |
1239 | jsonToObj = 0 |
1134 | PPCODE: |
1240 | PPCODE: |
1135 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1241 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1136 | |
1242 | |