ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.68 by root, Thu Nov 29 08:37:11 2007 UTC vs.
Revision 1.74 by root, Wed Mar 19 15:17:53 2008 UTC

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
85inline void 89INLINE void
86shrink (SV *sv) 90shrink (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
104inline UV 108INLINE UV
105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_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
125INLINE unsigned char *
126encode_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
135inline void 161INLINE void
136need (enc_t *enc, STRLEN len) 162need (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
147inline void 173INLINE void
148encode_ch (enc_t *enc, char ch) 174encode_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
266inline void 292INLINE void
267encode_indent (enc_t *enc) 293encode_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
279inline void 305INLINE void
280encode_space (enc_t *enc) 306encode_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
286inline void 312INLINE void
287encode_nl (enc_t *enc) 313encode_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
296inline void 322INLINE void
297encode_comma (enc_t *enc) 323encode_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)
619 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
620 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
621 } 647 }
622 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
623 { 649 {
624 // 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
625 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)
626 { 655 {
627 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
628 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
629 enc->cur += 658 enc->cur +=
630 SvIsUV(sv) 659 SvIsUV(sv)
631 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
632 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
633 } 662 }
634 else 663 else
635 { 664 {
636 // optimise the "small number case" 665 // optimise the "small number case"
637 // 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
638 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
639 U32 u; 669 U32 u;
640 char digit, nz = 0; 670 char digit, nz = 0;
641 671
642 need (enc, 6); 672 need (enc, 6);
643 673
680 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
681 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
682 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
683 enc.indent = 0; 713 enc.indent = 0;
684 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;
685 718
686 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
687 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
688 721
689 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
710 JSON json; 743 JSON json;
711 U32 depth; // recursion depth 744 U32 depth; // recursion depth
712 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
713} dec_t; 746} dec_t;
714 747
715inline void 748INLINE void
716decode_comment (dec_t *dec) 749decode_comment (dec_t *dec)
717{ 750{
718 // only '#'-style comments allowed a.t.m. 751 // only '#'-style comments allowed a.t.m.
719 752
720 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 753 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
721 ++dec->cur; 754 ++dec->cur;
722} 755}
723 756
724inline void 757INLINE void
725decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
726{ 759{
727 for (;;) 760 for (;;)
728 { 761 {
729 char ch = *dec->cur; 762 char ch = *dec->cur;
855 888
856 if (hi >= 0x80) 889 if (hi >= 0x80)
857 { 890 {
858 utf8 = 1; 891 utf8 = 1;
859 892
860 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
861 } 894 }
862 else 895 else
863 *cur++ = hi; 896 *cur++ = hi;
864 } 897 }
865 break; 898 break;
867 default: 900 default:
868 --dec_cur; 901 --dec_cur;
869 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
870 } 903 }
871 } 904 }
872 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
873 *cur++ = ch; 906 *cur++ = ch;
874 else if (ch >= 0x80) 907 else if (ch >= 0x80)
875 { 908 {
876 STRLEN clen; 909 STRLEN clen;
877 UV uch; 910 UV uch;
1000 1033
1001 if (!is_nv) 1034 if (!is_nv)
1002 { 1035 {
1003 int len = dec->cur - start; 1036 int len = dec->cur - start;
1004 1037
1005 // 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
1006 if (*start == '-') 1039 if (*start == '-')
1007 switch (len) 1040 switch (len)
1008 { 1041 {
1009 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 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)); 1044 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)); 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));
1013 } 1047 }
1014 else 1048 else
1015 switch (len) 1049 switch (len)
1016 { 1050 {
1017 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
1018 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 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); 1053 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); 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);
1021 } 1056 }
1022 1057
1023 { 1058 {
1024 UV uv; 1059 UV uv;
1025 int numtype = grok_number (start, len, &uv); 1060 int numtype = grok_number (start, len, &uv);
1274 1309
1275static SV * 1310static SV *
1276decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1277{ 1312{
1278 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1279 // to parse anything. 1314 // to parse everything.
1280 switch (*dec->cur) 1315 switch (*dec->cur)
1281 { 1316 {
1282 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1283 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1284 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1285 1320
1286 case '-': 1321 case '-':
1287 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1288 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1289 return decode_num (dec); 1324 return decode_num (dec);
1603 SvREFCNT_dec (self->cb_sk_object); 1638 SvREFCNT_dec (self->cb_sk_object);
1604 SvREFCNT_dec (self->cb_object); 1639 SvREFCNT_dec (self->cb_object);
1605 1640
1606PROTOTYPES: ENABLE 1641PROTOTYPES: ENABLE
1607 1642
1608void to_json (SV *scalar) 1643void encode_json (SV *scalar)
1609 PPCODE: 1644 PPCODE:
1610{ 1645{
1611 JSON json = { F_DEFAULT | F_UTF8 }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1612 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1613} 1648}
1614 1649
1615void from_json (SV *jsonstr) 1650void decode_json (SV *jsonstr)
1616 PPCODE: 1651 PPCODE:
1617{ 1652{
1618 JSON json = { F_DEFAULT | F_UTF8 }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1619 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1620} 1655}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines