… | |
… | |
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 |
… | |
… | |
80 | } JSON; |
87 | } JSON; |
81 | |
88 | |
82 | ///////////////////////////////////////////////////////////////////////////// |
89 | ///////////////////////////////////////////////////////////////////////////// |
83 | // utility functions |
90 | // utility functions |
84 | |
91 | |
85 | inline void |
92 | INLINE void |
86 | shrink (SV *sv) |
93 | shrink (SV *sv) |
87 | { |
94 | { |
88 | sv_utf8_downgrade (sv, 1); |
95 | sv_utf8_downgrade (sv, 1); |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
96 | if (SvLEN (sv) > SvCUR (sv) + 1) |
90 | { |
97 | { |
… | |
… | |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
106 | // decode an utf-8 character and return it, or (UV)-1 in |
100 | // case of an error. |
107 | // case of an error. |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
108 | // we special-case "safe" characters from U+80 .. U+7FF, |
102 | // but use the very good perl function to parse anything else. |
109 | // but use the very good perl function to parse anything else. |
103 | // note that we never call this function for a ascii codepoints |
110 | // note that we never call this function for a ascii codepoints |
104 | inline UV |
111 | INLINE UV |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
112 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
106 | { |
113 | { |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
114 | if (expect_true (len >= 2 |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
115 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
116 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
110 | { |
117 | { |
111 | *clen = 2; |
118 | *clen = 2; |
112 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
119 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
113 | } |
120 | } |
114 | else |
121 | else |
115 | { |
122 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
116 | *clen = (STRLEN)-1; |
123 | } |
117 | return (UV)-1; |
124 | |
118 | } |
125 | // likewise for encoding, also never called for ascii codepoints |
|
|
126 | // this function takes advantage of this fact, although current gccs |
|
|
127 | // seem to optimise the check for >= 0x80 away anyways |
|
|
128 | INLINE unsigned char * |
|
|
129 | encode_utf8 (unsigned char *s, UV ch) |
|
|
130 | { |
|
|
131 | if (expect_false (ch < 0x000080)) |
|
|
132 | *s++ = ch; |
|
|
133 | else if (expect_true (ch < 0x000800)) |
|
|
134 | *s++ = 0xc0 | ( ch >> 6), |
|
|
135 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
136 | else if ( ch < 0x010000) |
|
|
137 | *s++ = 0xe0 | ( ch >> 12), |
|
|
138 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
139 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
140 | else if ( ch < 0x110000) |
|
|
141 | *s++ = 0xf0 | ( ch >> 18), |
|
|
142 | *s++ = 0x80 | ((ch >> 12) & 0x3f), |
|
|
143 | *s++ = 0x80 | ((ch >> 6) & 0x3f), |
|
|
144 | *s++ = 0x80 | ( ch & 0x3f); |
|
|
145 | |
|
|
146 | return s; |
119 | } |
147 | } |
120 | |
148 | |
121 | ///////////////////////////////////////////////////////////////////////////// |
149 | ///////////////////////////////////////////////////////////////////////////// |
122 | // encoder |
150 | // encoder |
123 | |
151 | |
… | |
… | |
128 | char *end; // SvEND (sv) |
156 | char *end; // SvEND (sv) |
129 | SV *sv; // result scalar |
157 | SV *sv; // result scalar |
130 | JSON json; |
158 | JSON json; |
131 | U32 indent; // indentation level |
159 | U32 indent; // indentation level |
132 | U32 maxdepth; // max. indentation/recursion level |
160 | U32 maxdepth; // max. indentation/recursion level |
|
|
161 | UV limit; // escape character values >= this value when encoding |
133 | } enc_t; |
162 | } enc_t; |
134 | |
163 | |
135 | inline void |
164 | INLINE void |
136 | need (enc_t *enc, STRLEN len) |
165 | need (enc_t *enc, STRLEN len) |
137 | { |
166 | { |
138 | if (expect_false (enc->cur + len >= enc->end)) |
167 | if (expect_false (enc->cur + len >= enc->end)) |
139 | { |
168 | { |
140 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
169 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
142 | enc->cur = SvPVX (enc->sv) + cur; |
171 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
172 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | } |
173 | } |
145 | } |
174 | } |
146 | |
175 | |
147 | inline void |
176 | INLINE void |
148 | encode_ch (enc_t *enc, char ch) |
177 | encode_ch (enc_t *enc, char ch) |
149 | { |
178 | { |
150 | need (enc, 1); |
179 | need (enc, 1); |
151 | *enc->cur++ = ch; |
180 | *enc->cur++ = ch; |
152 | } |
181 | } |
… | |
… | |
206 | { |
235 | { |
207 | uch = ch; |
236 | uch = ch; |
208 | clen = 1; |
237 | clen = 1; |
209 | } |
238 | } |
210 | |
239 | |
211 | if (uch > 0x10FFFFUL) |
240 | 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 | { |
241 | { |
216 | if (uch > 0xFFFFUL) |
242 | if (uch >= 0x10000UL) |
217 | { |
243 | { |
|
|
244 | if (uch >= 0x110000UL) |
|
|
245 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
246 | |
218 | need (enc, len += 11); |
247 | need (enc, len += 11); |
219 | sprintf (enc->cur, "\\u%04x\\u%04x", |
248 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
249 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
250 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | enc->cur += 12; |
251 | enc->cur += 12; |
… | |
… | |
250 | while (--clen); |
279 | while (--clen); |
251 | } |
280 | } |
252 | else |
281 | else |
253 | { |
282 | { |
254 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
283 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
255 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
284 | enc->cur = encode_utf8 (enc->cur, uch); |
256 | ++str; |
285 | ++str; |
257 | } |
286 | } |
258 | } |
287 | } |
259 | } |
288 | } |
260 | } |
289 | } |
261 | |
290 | |
262 | --len; |
291 | --len; |
263 | } |
292 | } |
264 | } |
293 | } |
265 | |
294 | |
266 | inline void |
295 | INLINE void |
267 | encode_indent (enc_t *enc) |
296 | encode_indent (enc_t *enc) |
268 | { |
297 | { |
269 | if (enc->json.flags & F_INDENT) |
298 | if (enc->json.flags & F_INDENT) |
270 | { |
299 | { |
271 | int spaces = enc->indent * INDENT_STEP; |
300 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
274 | memset (enc->cur, ' ', spaces); |
303 | memset (enc->cur, ' ', spaces); |
275 | enc->cur += spaces; |
304 | enc->cur += spaces; |
276 | } |
305 | } |
277 | } |
306 | } |
278 | |
307 | |
279 | inline void |
308 | INLINE void |
280 | encode_space (enc_t *enc) |
309 | encode_space (enc_t *enc) |
281 | { |
310 | { |
282 | need (enc, 1); |
311 | need (enc, 1); |
283 | encode_ch (enc, ' '); |
312 | encode_ch (enc, ' '); |
284 | } |
313 | } |
285 | |
314 | |
286 | inline void |
315 | INLINE void |
287 | encode_nl (enc_t *enc) |
316 | encode_nl (enc_t *enc) |
288 | { |
317 | { |
289 | if (enc->json.flags & F_INDENT) |
318 | if (enc->json.flags & F_INDENT) |
290 | { |
319 | { |
291 | need (enc, 1); |
320 | need (enc, 1); |
292 | encode_ch (enc, '\n'); |
321 | encode_ch (enc, '\n'); |
293 | } |
322 | } |
294 | } |
323 | } |
295 | |
324 | |
296 | inline void |
325 | INLINE void |
297 | encode_comma (enc_t *enc) |
326 | encode_comma (enc_t *enc) |
298 | { |
327 | { |
299 | encode_ch (enc, ','); |
328 | encode_ch (enc, ','); |
300 | |
329 | |
301 | if (enc->json.flags & F_INDENT) |
330 | if (enc->json.flags & F_INDENT) |
… | |
… | |
619 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
648 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
620 | enc->cur += strlen (enc->cur); |
649 | enc->cur += strlen (enc->cur); |
621 | } |
650 | } |
622 | else if (SvIOKp (sv)) |
651 | else if (SvIOKp (sv)) |
623 | { |
652 | { |
624 | // we assume we can always read an IV as a UV |
653 | // we assume we can always read an IV as a UV and vice versa |
625 | if (SvUV (sv) & ~(UV)0x7fff) |
654 | // we assume two's complement |
626 | { |
655 | // we assume no aliasing issues in the union |
627 | // large integer, use the (rather slow) snprintf way. |
656 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
628 | need (enc, sizeof (UV) * 3); |
657 | : 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 | { |
658 | { |
636 | // optimise the "small number case" |
659 | // optimise the "small number case" |
637 | // code will likely be branchless and use only a single multiplication |
660 | // code will likely be branchless and use only a single multiplication |
|
|
661 | // works for numbers up to 59074 |
638 | I32 i = SvIV (sv); |
662 | I32 i = SvIVX (sv); |
639 | U32 u; |
663 | U32 u; |
640 | char digit, nz = 0; |
664 | char digit, nz = 0; |
641 | |
665 | |
642 | need (enc, 6); |
666 | need (enc, 6); |
643 | |
667 | |
… | |
… | |
649 | |
673 | |
650 | // now output digit by digit, each time masking out the integer part |
674 | // 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, |
675 | // and multiplying by 5 while moving the decimal point one to the right, |
652 | // resulting in a net multiplication by 10. |
676 | // resulting in a net multiplication by 10. |
653 | // we always write the digit to memory but conditionally increment |
677 | // we always write the digit to memory but conditionally increment |
654 | // the pointer, to ease the usage of conditional move instructions. |
678 | // 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; |
679 | 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; |
680 | 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; |
681 | 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; |
682 | 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' |
683 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
684 | } |
|
|
685 | else |
|
|
686 | { |
|
|
687 | // large integer, use the (rather slow) snprintf way. |
|
|
688 | need (enc, IVUV_MAXCHARS); |
|
|
689 | enc->cur += |
|
|
690 | SvIsUV(sv) |
|
|
691 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
|
|
692 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
660 | } |
693 | } |
661 | } |
694 | } |
662 | else if (SvROK (sv)) |
695 | else if (SvROK (sv)) |
663 | encode_rv (enc, SvRV (sv)); |
696 | encode_rv (enc, SvRV (sv)); |
664 | else if (!SvOK (sv)) |
697 | else if (!SvOK (sv)) |
… | |
… | |
680 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
713 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
681 | enc.cur = SvPVX (enc.sv); |
714 | enc.cur = SvPVX (enc.sv); |
682 | enc.end = SvEND (enc.sv); |
715 | enc.end = SvEND (enc.sv); |
683 | enc.indent = 0; |
716 | enc.indent = 0; |
684 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
717 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
718 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
719 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
720 | : 0x110000UL; |
685 | |
721 | |
686 | SvPOK_only (enc.sv); |
722 | SvPOK_only (enc.sv); |
687 | encode_sv (&enc, scalar); |
723 | encode_sv (&enc, scalar); |
688 | |
724 | |
689 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
725 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
710 | JSON json; |
746 | JSON json; |
711 | U32 depth; // recursion depth |
747 | U32 depth; // recursion depth |
712 | U32 maxdepth; // recursion depth limit |
748 | U32 maxdepth; // recursion depth limit |
713 | } dec_t; |
749 | } dec_t; |
714 | |
750 | |
715 | inline void |
751 | INLINE void |
716 | decode_comment (dec_t *dec) |
752 | decode_comment (dec_t *dec) |
717 | { |
753 | { |
718 | // only '#'-style comments allowed a.t.m. |
754 | // only '#'-style comments allowed a.t.m. |
719 | |
755 | |
720 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
756 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
721 | ++dec->cur; |
757 | ++dec->cur; |
722 | } |
758 | } |
723 | |
759 | |
724 | inline void |
760 | INLINE void |
725 | decode_ws (dec_t *dec) |
761 | decode_ws (dec_t *dec) |
726 | { |
762 | { |
727 | for (;;) |
763 | for (;;) |
728 | { |
764 | { |
729 | char ch = *dec->cur; |
765 | char ch = *dec->cur; |
… | |
… | |
855 | |
891 | |
856 | if (hi >= 0x80) |
892 | if (hi >= 0x80) |
857 | { |
893 | { |
858 | utf8 = 1; |
894 | utf8 = 1; |
859 | |
895 | |
860 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
896 | cur = encode_utf8 (cur, hi); |
861 | } |
897 | } |
862 | else |
898 | else |
863 | *cur++ = hi; |
899 | *cur++ = hi; |
864 | } |
900 | } |
865 | break; |
901 | break; |
… | |
… | |
867 | default: |
903 | default: |
868 | --dec_cur; |
904 | --dec_cur; |
869 | ERR ("illegal backslash escape sequence in string"); |
905 | ERR ("illegal backslash escape sequence in string"); |
870 | } |
906 | } |
871 | } |
907 | } |
872 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
908 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
873 | *cur++ = ch; |
909 | *cur++ = ch; |
874 | else if (ch >= 0x80) |
910 | else if (ch >= 0x80) |
875 | { |
911 | { |
876 | STRLEN clen; |
912 | STRLEN clen; |
877 | UV uch; |
913 | UV uch; |
… | |
… | |
1000 | |
1036 | |
1001 | if (!is_nv) |
1037 | if (!is_nv) |
1002 | { |
1038 | { |
1003 | int len = dec->cur - start; |
1039 | int len = dec->cur - start; |
1004 | |
1040 | |
1005 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1041 | // special case the rather common 1..5-digit-int case |
1006 | if (*start == '-') |
1042 | if (*start == '-') |
1007 | switch (len) |
1043 | switch (len) |
1008 | { |
1044 | { |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1045 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1046 | 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)); |
1047 | 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)); |
1048 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1049 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1013 | } |
1050 | } |
1014 | else |
1051 | else |
1015 | switch (len) |
1052 | switch (len) |
1016 | { |
1053 | { |
1017 | case 1: return newSViv ( start [0] - '0' * 1); |
1054 | case 1: return newSViv ( start [0] - '0' * 1); |
1018 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1055 | 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); |
1056 | 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); |
1057 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1058 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1021 | } |
1059 | } |
1022 | |
1060 | |
1023 | { |
1061 | { |
1024 | UV uv; |
1062 | UV uv; |
1025 | int numtype = grok_number (start, len, &uv); |
1063 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1274 | |
1312 | |
1275 | static SV * |
1313 | static SV * |
1276 | decode_sv (dec_t *dec) |
1314 | decode_sv (dec_t *dec) |
1277 | { |
1315 | { |
1278 | // the beauty of JSON: you need exactly one character lookahead |
1316 | // the beauty of JSON: you need exactly one character lookahead |
1279 | // to parse anything. |
1317 | // to parse everything. |
1280 | switch (*dec->cur) |
1318 | switch (*dec->cur) |
1281 | { |
1319 | { |
1282 | case '"': ++dec->cur; return decode_str (dec); |
1320 | case '"': ++dec->cur; return decode_str (dec); |
1283 | case '[': ++dec->cur; return decode_av (dec); |
1321 | case '[': ++dec->cur; return decode_av (dec); |
1284 | case '{': ++dec->cur; return decode_hv (dec); |
1322 | case '{': ++dec->cur; return decode_hv (dec); |
1285 | |
1323 | |
1286 | case '-': |
1324 | case '-': |
1287 | case '0': case '1': case '2': case '3': case '4': |
1325 | case '0': case '1': case '2': case '3': case '4': |
1288 | case '5': case '6': case '7': case '8': case '9': |
1326 | case '5': case '6': case '7': case '8': case '9': |
1289 | return decode_num (dec); |
1327 | return decode_num (dec); |
… | |
… | |
1457 | { |
1495 | { |
1458 | SV *pv = NEWSV (0, sizeof (JSON)); |
1496 | SV *pv = NEWSV (0, sizeof (JSON)); |
1459 | SvPOK_only (pv); |
1497 | SvPOK_only (pv); |
1460 | Zero (SvPVX (pv), 1, JSON); |
1498 | Zero (SvPVX (pv), 1, JSON); |
1461 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1499 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1462 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1500 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1501 | newRV_noinc (pv), |
|
|
1502 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1503 | ))); |
1463 | } |
1504 | } |
1464 | |
1505 | |
1465 | void ascii (JSON *self, int enable = 1) |
1506 | void ascii (JSON *self, int enable = 1) |
1466 | ALIAS: |
1507 | ALIAS: |
1467 | ascii = F_ASCII |
1508 | ascii = F_ASCII |
… | |
… | |
1494 | get_utf8 = F_UTF8 |
1535 | get_utf8 = F_UTF8 |
1495 | get_indent = F_INDENT |
1536 | get_indent = F_INDENT |
1496 | get_canonical = F_CANONICAL |
1537 | get_canonical = F_CANONICAL |
1497 | get_space_before = F_SPACE_BEFORE |
1538 | get_space_before = F_SPACE_BEFORE |
1498 | get_space_after = F_SPACE_AFTER |
1539 | get_space_after = F_SPACE_AFTER |
1499 | get_pretty = F_PRETTY |
|
|
1500 | get_allow_nonref = F_ALLOW_NONREF |
1540 | get_allow_nonref = F_ALLOW_NONREF |
1501 | get_shrink = F_SHRINK |
1541 | get_shrink = F_SHRINK |
1502 | get_allow_blessed = F_ALLOW_BLESSED |
1542 | get_allow_blessed = F_ALLOW_BLESSED |
1503 | get_convert_blessed = F_CONV_BLESSED |
1543 | get_convert_blessed = F_CONV_BLESSED |
1504 | get_relaxed = F_RELAXED |
1544 | get_relaxed = F_RELAXED |
… | |
… | |
1601 | SvREFCNT_dec (self->cb_sk_object); |
1641 | SvREFCNT_dec (self->cb_sk_object); |
1602 | SvREFCNT_dec (self->cb_object); |
1642 | SvREFCNT_dec (self->cb_object); |
1603 | |
1643 | |
1604 | PROTOTYPES: ENABLE |
1644 | PROTOTYPES: ENABLE |
1605 | |
1645 | |
1606 | void to_json (SV *scalar) |
1646 | void encode_json (SV *scalar) |
1607 | PPCODE: |
1647 | PPCODE: |
1608 | { |
1648 | { |
1609 | JSON json = { F_DEFAULT | F_UTF8 }; |
1649 | JSON json = { F_DEFAULT | F_UTF8 }; |
1610 | XPUSHs (encode_json (scalar, &json)); |
1650 | XPUSHs (encode_json (scalar, &json)); |
1611 | } |
1651 | } |
1612 | |
1652 | |
1613 | void from_json (SV *jsonstr) |
1653 | void decode_json (SV *jsonstr) |
1614 | PPCODE: |
1654 | PPCODE: |
1615 | { |
1655 | { |
1616 | JSON json = { F_DEFAULT | F_UTF8 }; |
1656 | JSON json = { F_DEFAULT | F_UTF8 }; |
1617 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1657 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1618 | } |
1658 | } |