… | |
… | |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // 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 | |
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) |
|
|
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 |
46 | |
57 | |
… | |
… | |
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; |
… | |
… | |
487 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
488 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
489 | } |
505 | } |
490 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
491 | { |
507 | { |
|
|
508 | // we assume we can always read an IV as a UV |
|
|
509 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
510 | { |
492 | need (enc, 64); |
511 | need (enc, 32); |
493 | enc->cur += |
512 | enc->cur += |
494 | SvIsUV(sv) |
513 | SvIsUV(sv) |
495 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
514 | ? snprintf (enc->cur, 32, "%"UVuf, (UV)SvUVX (sv)) |
496 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
515 | : snprintf (enc->cur, 32, "%"IVdf, (IV)SvIVX (sv)); |
|
|
516 | } |
|
|
517 | else |
|
|
518 | { |
|
|
519 | // optimise the "small number case" |
|
|
520 | // code will likely be branchless and use only a single multiplication |
|
|
521 | I32 i = SvIV (sv); |
|
|
522 | U32 u; |
|
|
523 | |
|
|
524 | need (enc, 6); |
|
|
525 | |
|
|
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
527 | u = i < 0 ? -i : i; |
|
|
528 | |
|
|
529 | // convert to 4.28 fixed-point representation |
|
|
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
531 | |
|
|
532 | char digit, nz = 0; |
|
|
533 | |
|
|
534 | digit = u >> 28; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0xfffffff) * 5; |
|
|
535 | digit = u >> 27; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x7ffffff) * 5; |
|
|
536 | digit = u >> 26; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x3ffffff) * 5; |
|
|
537 | digit = u >> 25; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x1ffffff) * 5; |
|
|
538 | digit = u >> 24; *enc->cur = digit + '0'; nz |= digit; enc->cur += 1; |
|
|
539 | } |
497 | } |
540 | } |
498 | else if (SvROK (sv)) |
541 | else if (SvROK (sv)) |
499 | encode_rv (enc, SvRV (sv)); |
542 | encode_rv (enc, SvRV (sv)); |
500 | else if (!SvOK (sv)) |
543 | else if (!SvOK (sv)) |
501 | encode_str (enc, "null", 4, 0); |
544 | encode_str (enc, "null", 4, 0); |
… | |
… | |
546 | U32 flags; // F_* |
589 | U32 flags; // F_* |
547 | U32 depth; // recursion depth |
590 | U32 depth; // recursion depth |
548 | U32 maxdepth; // recursion depth limit |
591 | U32 maxdepth; // recursion depth limit |
549 | } dec_t; |
592 | } dec_t; |
550 | |
593 | |
551 | static void |
594 | inline void |
552 | decode_ws (dec_t *dec) |
595 | decode_ws (dec_t *dec) |
553 | { |
596 | { |
554 | for (;;) |
597 | for (;;) |
555 | { |
598 | { |
556 | char ch = *dec->cur; |
599 | char ch = *dec->cur; |
… | |
… | |
582 | decode_4hex (dec_t *dec) |
625 | decode_4hex (dec_t *dec) |
583 | { |
626 | { |
584 | signed char d1, d2, d3, d4; |
627 | signed char d1, d2, d3, d4; |
585 | unsigned char *cur = (unsigned char *)dec->cur; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
586 | |
629 | |
587 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
630 | 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"); |
631 | 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"); |
632 | 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"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
591 | |
634 | |
592 | dec->cur += 4; |
635 | dec->cur += 4; |
593 | |
636 | |
594 | return ((UV)d1) << 12 |
637 | return ((UV)d1) << 12 |
595 | | ((UV)d2) << 8 |
638 | | ((UV)d2) << 8 |
… | |
… | |
613 | |
656 | |
614 | do |
657 | do |
615 | { |
658 | { |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
659 | unsigned char ch = *(unsigned char *)dec->cur++; |
617 | |
660 | |
618 | if (ch == '"') |
661 | if (expect_false (ch == '"')) |
619 | { |
662 | { |
620 | --dec->cur; |
663 | --dec->cur; |
621 | break; |
664 | break; |
622 | } |
665 | } |
623 | else if (ch == '\\') |
666 | else if (expect_false (ch == '\\')) |
624 | { |
667 | { |
625 | switch (*dec->cur) |
668 | switch (*dec->cur) |
626 | { |
669 | { |
627 | case '\\': |
670 | case '\\': |
628 | case '/': |
671 | case '/': |
… | |
… | |
678 | default: |
721 | default: |
679 | --dec->cur; |
722 | --dec->cur; |
680 | ERR ("illegal backslash escape sequence in string"); |
723 | ERR ("illegal backslash escape sequence in string"); |
681 | } |
724 | } |
682 | } |
725 | } |
683 | else if (ch >= 0x20 && ch <= 0x7f) |
726 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
684 | *cur++ = ch; |
727 | *cur++ = ch; |
685 | else if (ch >= 0x80) |
728 | else if (ch >= 0x80) |
686 | { |
729 | { |
687 | STRLEN clen; |
730 | STRLEN clen; |
688 | UV uch; |
731 | UV uch; |
… | |
… | |
807 | is_nv = 1; |
850 | is_nv = 1; |
808 | } |
851 | } |
809 | |
852 | |
810 | if (!is_nv) |
853 | if (!is_nv) |
811 | { |
854 | { |
812 | UV uv; |
855 | // 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); |
856 | if (*start == '-') |
814 | if (numtype & IS_NUMBER_IN_UV) |
857 | switch (dec->cur - start) |
815 | if (numtype & IS_NUMBER_NEG) |
|
|
816 | { |
858 | { |
817 | if (uv < (UV)IV_MIN) |
859 | case 2: return newSViv (-( start [1] - '0' )); |
818 | return newSViv (-(IV)uv); |
860 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
861 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
862 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
819 | } |
863 | } |
|
|
864 | else |
|
|
865 | switch (dec->cur - start) |
|
|
866 | { |
|
|
867 | case 1: return newSViv ( start [0] - '0' ); |
|
|
868 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
869 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
870 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
871 | } |
|
|
872 | |
|
|
873 | { |
|
|
874 | UV uv; |
|
|
875 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
876 | if (numtype & IS_NUMBER_IN_UV) |
|
|
877 | if (numtype & IS_NUMBER_NEG) |
|
|
878 | { |
|
|
879 | if (uv < (UV)IV_MIN) |
|
|
880 | return newSViv (-(IV)uv); |
|
|
881 | } |
820 | else |
882 | else |
821 | return newSVuv (uv); |
883 | return newSVuv (uv); |
|
|
884 | } |
822 | } |
885 | } |
823 | |
886 | |
824 | return newSVnv (Atof (start)); |
887 | return newSVnv (Atof (start)); |
825 | |
888 | |
826 | fail: |
889 | fail: |