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.28 by root, Wed Apr 11 12:23:02 2007 UTC vs.
Revision 1.35 by root, Wed Jun 6 14:52:49 2007 UTC

10#if defined(__BORLANDC__) || defined(_MSC_VER) 10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 12#endif
13 13
14#define F_ASCII 0x00000001UL 14#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL
15#define F_UTF8 0x00000002UL 16#define F_UTF8 0x00000004UL
16#define F_INDENT 0x00000004UL 17#define F_INDENT 0x00000008UL
17#define F_CANONICAL 0x00000008UL 18#define F_CANONICAL 0x00000010UL
18#define F_SPACE_BEFORE 0x00000010UL 19#define F_SPACE_BEFORE 0x00000020UL
19#define F_SPACE_AFTER 0x00000020UL 20#define F_SPACE_AFTER 0x00000040UL
20#define F_ALLOW_NONREF 0x00000080UL 21#define F_ALLOW_NONREF 0x00000100UL
21#define F_SHRINK 0x00000100UL 22#define F_SHRINK 0x00000200UL
22#define F_MAXDEPTH 0xf8000000UL 23#define F_MAXDEPTH 0xf8000000UL
23#define S_MAXDEPTH 27 24#define S_MAXDEPTH 27
24 25
25#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
26 27
35 36
36#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
37 38
38#define SB do { 39#define SB do {
39#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)
40 52
41static HV *json_stash; // JSON::XS:: 53static HV *json_stash; // JSON::XS::
42 54
43///////////////////////////////////////////////////////////////////////////// 55/////////////////////////////////////////////////////////////////////////////
44// utility functions 56// utility functions
69// 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
70// case of an error. 82// case of an error.
71// we special-case "safe" characters from U+80 .. U+7FF, 83// we special-case "safe" characters from U+80 .. U+7FF,
72// but use the very good perl function to parse anything else. 84// but use the very good perl function to parse anything else.
73// note that we never call this function for a ascii codepoints 85// note that we never call this function for a ascii codepoints
74static UV 86inline UV
75decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 87decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
76{ 88{
77 if (s[0] > 0xdf || s[0] < 0xc2) 89 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
78 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 90 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
79 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 91 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
80 { 92 {
81 *clen = 2; 93 *clen = 2;
82 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 94 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
100 U32 flags; // F_* 112 U32 flags; // F_*
101 U32 indent; // indentation level 113 U32 indent; // indentation level
102 U32 maxdepth; // max. indentation/recursion level 114 U32 maxdepth; // max. indentation/recursion level
103} enc_t; 115} enc_t;
104 116
105static void 117inline void
106need (enc_t *enc, STRLEN len) 118need (enc_t *enc, STRLEN len)
107{ 119{
108 if (enc->cur + len >= enc->end) 120 if (expect_false (enc->cur + len >= enc->end))
109 { 121 {
110 STRLEN cur = enc->cur - SvPVX (enc->sv); 122 STRLEN cur = enc->cur - SvPVX (enc->sv);
111 SvGROW (enc->sv, cur + len + 1); 123 SvGROW (enc->sv, cur + len + 1);
112 enc->cur = SvPVX (enc->sv) + cur; 124 enc->cur = SvPVX (enc->sv) + cur;
113 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 125 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
114 } 126 }
115} 127}
116 128
117static void 129inline void
118encode_ch (enc_t *enc, char ch) 130encode_ch (enc_t *enc, char ch)
119{ 131{
120 need (enc, 1); 132 need (enc, 1);
121 *enc->cur++ = ch; 133 *enc->cur++ = ch;
122} 134}
130 142
131 while (str < end) 143 while (str < end)
132 { 144 {
133 unsigned char ch = *(unsigned char *)str; 145 unsigned char ch = *(unsigned char *)str;
134 146
135 if (ch >= 0x20 && ch < 0x80) // most common case 147 if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case
136 { 148 {
137 if (ch == '"') // but with slow exceptions 149 if (expect_false (ch == '"')) // but with slow exceptions
138 { 150 {
139 need (enc, len += 1); 151 need (enc, len += 1);
140 *enc->cur++ = '\\'; 152 *enc->cur++ = '\\';
141 *enc->cur++ = '"'; 153 *enc->cur++ = '"';
142 } 154 }
143 else if (ch == '\\') 155 else if (expect_false (ch == '\\'))
144 { 156 {
145 need (enc, len += 1); 157 need (enc, len += 1);
146 *enc->cur++ = '\\'; 158 *enc->cur++ = '\\';
147 *enc->cur++ = '\\'; 159 *enc->cur++ = '\\';
148 } 160 }
180 } 192 }
181 193
182 if (uch > 0x10FFFFUL) 194 if (uch > 0x10FFFFUL)
183 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 195 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
184 196
185 if (uch < 0x80 || enc->flags & F_ASCII) 197 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
186 { 198 {
187 if (uch > 0xFFFFUL) 199 if (uch > 0xFFFFUL)
188 { 200 {
189 need (enc, len += 11); 201 need (enc, len += 11);
190 sprintf (enc->cur, "\\u%04x\\u%04x", 202 sprintf (enc->cur, "\\u%04x\\u%04x",
204 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 216 *enc->cur++ = hexdigit [(uch >> 0) & 15];
205 } 217 }
206 218
207 str += clen; 219 str += clen;
208 } 220 }
221 else if (enc->flags & F_LATIN1)
222 {
223 *enc->cur++ = uch;
224 str += clen;
225 }
209 else if (is_utf8) 226 else if (is_utf8)
210 { 227 {
211 need (enc, len += clen); 228 need (enc, len += clen);
212 do 229 do
213 { 230 {
227 244
228 --len; 245 --len;
229 } 246 }
230} 247}
231 248
232static void 249inline void
233encode_indent (enc_t *enc) 250encode_indent (enc_t *enc)
234{ 251{
235 if (enc->flags & F_INDENT) 252 if (enc->flags & F_INDENT)
236 { 253 {
237 int spaces = enc->indent * INDENT_STEP; 254 int spaces = enc->indent * INDENT_STEP;
240 memset (enc->cur, ' ', spaces); 257 memset (enc->cur, ' ', spaces);
241 enc->cur += spaces; 258 enc->cur += spaces;
242 } 259 }
243} 260}
244 261
245static void 262inline void
246encode_space (enc_t *enc) 263encode_space (enc_t *enc)
247{ 264{
248 need (enc, 1); 265 need (enc, 1);
249 encode_ch (enc, ' '); 266 encode_ch (enc, ' ');
250} 267}
251 268
252static void 269inline void
253encode_nl (enc_t *enc) 270encode_nl (enc_t *enc)
254{ 271{
255 if (enc->flags & F_INDENT) 272 if (enc->flags & F_INDENT)
256 { 273 {
257 need (enc, 1); 274 need (enc, 1);
258 encode_ch (enc, '\n'); 275 encode_ch (enc, '\n');
259 } 276 }
260} 277}
261 278
262static void 279inline void
263encode_comma (enc_t *enc) 280encode_comma (enc_t *enc)
264{ 281{
265 encode_ch (enc, ','); 282 encode_ch (enc, ',');
266 283
267 if (enc->flags & F_INDENT) 284 if (enc->flags & F_INDENT)
366 // actually, this is mostly due to the stupid so-called 383 // actually, this is mostly due to the stupid so-called
367 // security workaround added somewhere in 5.8.x. 384 // security workaround added somewhere in 5.8.x.
368 // that randomises hash orderings 385 // that randomises hash orderings
369 if (enc->flags & F_CANONICAL) 386 if (enc->flags & F_CANONICAL)
370 { 387 {
371 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
372 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
373 395
374 i = 0; 396 i = 0;
375 while ((he = hv_iternext (hv))) 397 while ((he = hv_iternext (hv)))
376 { 398 {
377 hes [i++] = he; 399 hes [i++] = he;
481 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 503 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
482 enc->cur += strlen (enc->cur); 504 enc->cur += strlen (enc->cur);
483 } 505 }
484 else if (SvIOKp (sv)) 506 else if (SvIOKp (sv))
485 { 507 {
508 // we assume we can always read an IV as a UV
509 if (SvUV (sv) & ~(UV)0x7fff)
510 {
486 need (enc, 64); 511 need (enc, 32);
487 enc->cur += 512 enc->cur +=
488 SvIsUV(sv) 513 SvIsUV(sv)
489 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 514 ? snprintf (enc->cur, 32, "%"UVuf, (UV)SvUVX (sv))
490 : 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 }
491 } 540 }
492 else if (SvROK (sv)) 541 else if (SvROK (sv))
493 encode_rv (enc, SvRV (sv)); 542 encode_rv (enc, SvRV (sv));
494 else if (!SvOK (sv)) 543 else if (!SvOK (sv))
495 encode_str (enc, "null", 4, 0); 544 encode_str (enc, "null", 4, 0);
514 enc.maxdepth = DEC_DEPTH (flags); 563 enc.maxdepth = DEC_DEPTH (flags);
515 564
516 SvPOK_only (enc.sv); 565 SvPOK_only (enc.sv);
517 encode_sv (&enc, scalar); 566 encode_sv (&enc, scalar);
518 567
519 if (!(flags & (F_ASCII | F_UTF8)))
520 SvUTF8_on (enc.sv);
521
522 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 568 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
523 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 569 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
570
571 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
572 SvUTF8_on (enc.sv);
524 573
525 if (enc.flags & F_SHRINK) 574 if (enc.flags & F_SHRINK)
526 shrink (enc.sv); 575 shrink (enc.sv);
527 576
528 return enc.sv; 577 return enc.sv;
540 U32 flags; // F_* 589 U32 flags; // F_*
541 U32 depth; // recursion depth 590 U32 depth; // recursion depth
542 U32 maxdepth; // recursion depth limit 591 U32 maxdepth; // recursion depth limit
543} dec_t; 592} dec_t;
544 593
545static void 594inline void
546decode_ws (dec_t *dec) 595decode_ws (dec_t *dec)
547{ 596{
548 for (;;) 597 for (;;)
549 { 598 {
550 char ch = *dec->cur; 599 char ch = *dec->cur;
576decode_4hex (dec_t *dec) 625decode_4hex (dec_t *dec)
577{ 626{
578 signed char d1, d2, d3, d4; 627 signed char d1, d2, d3, d4;
579 unsigned char *cur = (unsigned char *)dec->cur; 628 unsigned char *cur = (unsigned char *)dec->cur;
580 629
581 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");
582 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");
583 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");
584 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");
585 634
586 dec->cur += 4; 635 dec->cur += 4;
587 636
588 return ((UV)d1) << 12 637 return ((UV)d1) << 12
589 | ((UV)d2) << 8 638 | ((UV)d2) << 8
607 656
608 do 657 do
609 { 658 {
610 unsigned char ch = *(unsigned char *)dec->cur++; 659 unsigned char ch = *(unsigned char *)dec->cur++;
611 660
612 if (ch == '"') 661 if (expect_false (ch == '"'))
613 { 662 {
614 --dec->cur; 663 --dec->cur;
615 break; 664 break;
616 } 665 }
617 else if (ch == '\\') 666 else if (expect_false (ch == '\\'))
618 { 667 {
619 switch (*dec->cur) 668 switch (*dec->cur)
620 { 669 {
621 case '\\': 670 case '\\':
622 case '/': 671 case '/':
672 default: 721 default:
673 --dec->cur; 722 --dec->cur;
674 ERR ("illegal backslash escape sequence in string"); 723 ERR ("illegal backslash escape sequence in string");
675 } 724 }
676 } 725 }
677 else if (ch >= 0x20 && ch <= 0x7f) 726 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
678 *cur++ = ch; 727 *cur++ = ch;
679 else if (ch >= 0x80) 728 else if (ch >= 0x80)
680 { 729 {
681 STRLEN clen; 730 STRLEN clen;
682 UV uch; 731 UV uch;
801 is_nv = 1; 850 is_nv = 1;
802 } 851 }
803 852
804 if (!is_nv) 853 if (!is_nv)
805 { 854 {
806 UV uv; 855 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
807 int numtype = grok_number (start, dec->cur - start, &uv); 856 if (*start == '-')
808 if (numtype & IS_NUMBER_IN_UV) 857 switch (dec->cur - start)
809 if (numtype & IS_NUMBER_NEG)
810 { 858 {
811 if (uv < (UV)IV_MIN) 859 case 2: return newSViv (-( start [1] - '0' ));
812 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));
813 } 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 }
814 else 882 else
815 return newSVuv (uv); 883 return newSVuv (uv);
884 }
816 } 885 }
817 886
818 return newSVnv (Atof (start)); 887 return newSVnv (Atof (start));
819 888
820fail: 889fail:
977fail: 1046fail:
978 return 0; 1047 return 0;
979} 1048}
980 1049
981static SV * 1050static SV *
982decode_json (SV *string, U32 flags) 1051decode_json (SV *string, U32 flags, UV *offset_return)
983{ 1052{
984 dec_t dec; 1053 dec_t dec;
1054 UV offset;
985 SV *sv; 1055 SV *sv;
986 1056
1057 SvGETMAGIC (string);
987 SvUPGRADE (string, SVt_PV); 1058 SvUPGRADE (string, SVt_PV);
988 1059
989 if (flags & F_UTF8) 1060 if (flags & F_UTF8)
990 sv_utf8_downgrade (string, 0); 1061 sv_utf8_downgrade (string, 0);
991 else 1062 else
998 dec.end = SvEND (string); 1069 dec.end = SvEND (string);
999 dec.err = 0; 1070 dec.err = 0;
1000 dec.depth = 0; 1071 dec.depth = 0;
1001 dec.maxdepth = DEC_DEPTH (dec.flags); 1072 dec.maxdepth = DEC_DEPTH (dec.flags);
1002 1073
1003 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1074 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1004 sv = decode_sv (&dec); 1075 sv = decode_sv (&dec);
1005 1076
1077 if (!(offset_return || !sv))
1078 {
1079 // check for trailing garbage
1080 decode_ws (&dec);
1081
1082 if (*dec.cur)
1083 {
1084 dec.err = "garbage after JSON object";
1085 SvREFCNT_dec (sv);
1086 sv = 0;
1087 }
1088 }
1089
1090 if (offset_return || !sv)
1091 {
1092 offset = dec.flags & F_UTF8
1093 ? dec.cur - SvPVX (string)
1094 : utf8_distance (dec.cur, SvPVX (string));
1095
1096 if (offset_return)
1097 *offset_return = offset;
1098 }
1099
1006 if (!sv) 1100 if (!sv)
1007 { 1101 {
1008 IV offset = dec.flags & F_UTF8
1009 ? dec.cur - SvPVX (string)
1010 : utf8_distance (dec.cur, SvPVX (string));
1011 SV *uni = sv_newmortal (); 1102 SV *uni = sv_newmortal ();
1012 1103
1013 // horrible hack to silence warning inside pv_uni_display 1104 // horrible hack to silence warning inside pv_uni_display
1014 COP cop = *PL_curcop; 1105 COP cop = *PL_curcop;
1015 cop.cop_warnings = pWARN_NONE; 1106 cop.cop_warnings = pWARN_NONE;
1063 RETVAL 1154 RETVAL
1064 1155
1065SV *ascii (SV *self, int enable = 1) 1156SV *ascii (SV *self, int enable = 1)
1066 ALIAS: 1157 ALIAS:
1067 ascii = F_ASCII 1158 ascii = F_ASCII
1159 latin1 = F_LATIN1
1068 utf8 = F_UTF8 1160 utf8 = F_UTF8
1069 indent = F_INDENT 1161 indent = F_INDENT
1070 canonical = F_CANONICAL 1162 canonical = F_CANONICAL
1071 space_before = F_SPACE_BEFORE 1163 space_before = F_SPACE_BEFORE
1072 space_after = F_SPACE_AFTER 1164 space_after = F_SPACE_AFTER
1108 PPCODE: 1200 PPCODE:
1109 XPUSHs (encode_json (scalar, *SvJSON (self))); 1201 XPUSHs (encode_json (scalar, *SvJSON (self)));
1110 1202
1111void decode (SV *self, SV *jsonstr) 1203void decode (SV *self, SV *jsonstr)
1112 PPCODE: 1204 PPCODE:
1113 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1205 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1206
1207void decode_prefix (SV *self, SV *jsonstr)
1208 PPCODE:
1209{
1210 UV offset;
1211 EXTEND (SP, 2);
1212 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1213 PUSHs (sv_2mortal (newSVuv (offset)));
1214}
1114 1215
1115PROTOTYPES: ENABLE 1216PROTOTYPES: ENABLE
1116 1217
1117void to_json (SV *scalar) 1218void to_json (SV *scalar)
1118 ALIAS: 1219 ALIAS:
1122 1223
1123void from_json (SV *jsonstr) 1224void from_json (SV *jsonstr)
1124 ALIAS: 1225 ALIAS:
1125 jsonToObj = 0 1226 jsonToObj = 0
1126 PPCODE: 1227 PPCODE:
1127 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1228 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1128 1229

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines