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.27 by root, Mon Apr 9 05:09:57 2007 UTC vs.
Revision 1.43 by root, Sat Jun 23 23:49:29 2007 UTC

9 9
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// some old perls do not have this, try to make it work, no
15// guarentees, though. if it breaks, you get to keep the pieces.
16#ifndef UTF8_MAXBYTES
17# define UTF8_MAXBYTES 13
18#endif
19
14#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
21#define F_LATIN1 0x00000002UL
15#define F_UTF8 0x00000002UL 22#define F_UTF8 0x00000004UL
16#define F_INDENT 0x00000004UL 23#define F_INDENT 0x00000008UL
17#define F_CANONICAL 0x00000008UL 24#define F_CANONICAL 0x00000010UL
18#define F_SPACE_BEFORE 0x00000010UL 25#define F_SPACE_BEFORE 0x00000020UL
19#define F_SPACE_AFTER 0x00000020UL 26#define F_SPACE_AFTER 0x00000040UL
20#define F_ALLOW_NONREF 0x00000080UL 27#define F_ALLOW_NONREF 0x00000100UL
21#define F_SHRINK 0x00000100UL 28#define F_SHRINK 0x00000200UL
22#define F_MAXDEPTH 0xf8000000UL 29#define F_MAXDEPTH 0xf8000000UL
23#define S_MAXDEPTH 27 30#define S_MAXDEPTH 27
24 31
25#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 32#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
26 33
31#define F_DEFAULT (9UL << S_MAXDEPTH) 38#define F_DEFAULT (9UL << S_MAXDEPTH)
32 39
33#define INIT_SIZE 32 // initial scalar size to be allocated 40#define INIT_SIZE 32 // initial scalar size to be allocated
34#define INDENT_STEP 3 // spaces per indentation level 41#define INDENT_STEP 3 // spaces per indentation level
35 42
36#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
37#define SHORT_STRING_LEN 512 // special-case strings of up to this size 43#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
38 44
39#define SB do { 45#define SB do {
40#define SE } while (0) 46#define SE } while (0)
41 47
48#if __GNUC__ >= 3
49# define expect(expr,value) __builtin_expect ((expr),(value))
50# define inline inline
51#else
52# define expect(expr,value) (expr)
53# define inline static
54#endif
55
56#define expect_false(expr) expect ((expr) != 0, 0)
57#define expect_true(expr) expect ((expr) != 0, 1)
58
42static HV *json_stash; // JSON::XS:: 59static HV *json_stash; // JSON::XS::
60static SV *json_true, *json_false;
43 61
44///////////////////////////////////////////////////////////////////////////// 62/////////////////////////////////////////////////////////////////////////////
45// utility functions 63// utility functions
46 64
47static UV * 65static UV *
70// decode an utf-8 character and return it, or (UV)-1 in 88// decode an utf-8 character and return it, or (UV)-1 in
71// case of an error. 89// case of an error.
72// we special-case "safe" characters from U+80 .. U+7FF, 90// we special-case "safe" characters from U+80 .. U+7FF,
73// but use the very good perl function to parse anything else. 91// but use the very good perl function to parse anything else.
74// note that we never call this function for a ascii codepoints 92// note that we never call this function for a ascii codepoints
75static UV 93inline UV
76decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 94decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
77{ 95{
78 if (s[0] > 0xdf || s[0] < 0xc2) 96 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
79 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 97 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
80 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 98 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
81 { 99 {
82 *clen = 2; 100 *clen = 2;
83 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 101 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
101 U32 flags; // F_* 119 U32 flags; // F_*
102 U32 indent; // indentation level 120 U32 indent; // indentation level
103 U32 maxdepth; // max. indentation/recursion level 121 U32 maxdepth; // max. indentation/recursion level
104} enc_t; 122} enc_t;
105 123
106static void 124inline void
107need (enc_t *enc, STRLEN len) 125need (enc_t *enc, STRLEN len)
108{ 126{
109 if (enc->cur + len >= enc->end) 127 if (expect_false (enc->cur + len >= enc->end))
110 { 128 {
111 STRLEN cur = enc->cur - SvPVX (enc->sv); 129 STRLEN cur = enc->cur - SvPVX (enc->sv);
112 SvGROW (enc->sv, cur + len + 1); 130 SvGROW (enc->sv, cur + len + 1);
113 enc->cur = SvPVX (enc->sv) + cur; 131 enc->cur = SvPVX (enc->sv) + cur;
114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 132 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
115 } 133 }
116} 134}
117 135
118static void 136inline void
119encode_ch (enc_t *enc, char ch) 137encode_ch (enc_t *enc, char ch)
120{ 138{
121 need (enc, 1); 139 need (enc, 1);
122 *enc->cur++ = ch; 140 *enc->cur++ = ch;
123} 141}
131 149
132 while (str < end) 150 while (str < end)
133 { 151 {
134 unsigned char ch = *(unsigned char *)str; 152 unsigned char ch = *(unsigned char *)str;
135 153
136 if (ch >= 0x20 && ch < 0x80) // most common case 154 if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case
137 { 155 {
138 if (ch == '"') // but with slow exceptions 156 if (expect_false (ch == '"')) // but with slow exceptions
139 { 157 {
140 need (enc, len += 1); 158 need (enc, len += 1);
141 *enc->cur++ = '\\'; 159 *enc->cur++ = '\\';
142 *enc->cur++ = '"'; 160 *enc->cur++ = '"';
143 } 161 }
144 else if (ch == '\\') 162 else if (expect_false (ch == '\\'))
145 { 163 {
146 need (enc, len += 1); 164 need (enc, len += 1);
147 *enc->cur++ = '\\'; 165 *enc->cur++ = '\\';
148 *enc->cur++ = '\\'; 166 *enc->cur++ = '\\';
149 } 167 }
167 STRLEN clen; 185 STRLEN clen;
168 UV uch; 186 UV uch;
169 187
170 if (is_utf8) 188 if (is_utf8)
171 { 189 {
172 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
173 uch = decode_utf8 (str, end - str, &clen); 190 uch = decode_utf8 (str, end - str, &clen);
174 if (clen == (STRLEN)-1) 191 if (clen == (STRLEN)-1)
175 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 192 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
176 } 193 }
177 else 194 else
181 } 198 }
182 199
183 if (uch > 0x10FFFFUL) 200 if (uch > 0x10FFFFUL)
184 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 201 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
185 202
186 if (uch < 0x80 || enc->flags & F_ASCII) 203 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
187 { 204 {
188 if (uch > 0xFFFFUL) 205 if (uch > 0xFFFFUL)
189 { 206 {
190 need (enc, len += 11); 207 need (enc, len += 11);
191 sprintf (enc->cur, "\\u%04x\\u%04x", 208 sprintf (enc->cur, "\\u%04x\\u%04x",
205 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 222 *enc->cur++ = hexdigit [(uch >> 0) & 15];
206 } 223 }
207 224
208 str += clen; 225 str += clen;
209 } 226 }
227 else if (enc->flags & F_LATIN1)
228 {
229 *enc->cur++ = uch;
230 str += clen;
231 }
210 else if (is_utf8) 232 else if (is_utf8)
211 { 233 {
212 need (enc, len += clen); 234 need (enc, len += clen);
213 do 235 do
214 { 236 {
216 } 238 }
217 while (--clen); 239 while (--clen);
218 } 240 }
219 else 241 else
220 { 242 {
221 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 243 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
222 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 244 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
223 ++str; 245 ++str;
224 } 246 }
225 } 247 }
226 } 248 }
228 250
229 --len; 251 --len;
230 } 252 }
231} 253}
232 254
233static void 255inline void
234encode_indent (enc_t *enc) 256encode_indent (enc_t *enc)
235{ 257{
236 if (enc->flags & F_INDENT) 258 if (enc->flags & F_INDENT)
237 { 259 {
238 int spaces = enc->indent * INDENT_STEP; 260 int spaces = enc->indent * INDENT_STEP;
241 memset (enc->cur, ' ', spaces); 263 memset (enc->cur, ' ', spaces);
242 enc->cur += spaces; 264 enc->cur += spaces;
243 } 265 }
244} 266}
245 267
246static void 268inline void
247encode_space (enc_t *enc) 269encode_space (enc_t *enc)
248{ 270{
249 need (enc, 1); 271 need (enc, 1);
250 encode_ch (enc, ' '); 272 encode_ch (enc, ' ');
251} 273}
252 274
253static void 275inline void
254encode_nl (enc_t *enc) 276encode_nl (enc_t *enc)
255{ 277{
256 if (enc->flags & F_INDENT) 278 if (enc->flags & F_INDENT)
257 { 279 {
258 need (enc, 1); 280 need (enc, 1);
259 encode_ch (enc, '\n'); 281 encode_ch (enc, '\n');
260 } 282 }
261} 283}
262 284
263static void 285inline void
264encode_comma (enc_t *enc) 286encode_comma (enc_t *enc)
265{ 287{
266 encode_ch (enc, ','); 288 encode_ch (enc, ',');
267 289
268 if (enc->flags & F_INDENT) 290 if (enc->flags & F_INDENT)
367 // actually, this is mostly due to the stupid so-called 389 // actually, this is mostly due to the stupid so-called
368 // security workaround added somewhere in 5.8.x. 390 // security workaround added somewhere in 5.8.x.
369 // that randomises hash orderings 391 // that randomises hash orderings
370 if (enc->flags & F_CANONICAL) 392 if (enc->flags & F_CANONICAL)
371 { 393 {
372 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
373 int fast = 1; 394 int fast = 1;
395 HE *he;
396#if defined(__BORLANDC__) || defined(_MSC_VER)
397 HE **hes = _alloca (count * sizeof (HE));
398#else
399 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
400#endif
374 401
375 i = 0; 402 i = 0;
376 while ((he = hv_iternext (hv))) 403 while ((he = hv_iternext (hv)))
377 { 404 {
378 hes [i++] = he; 405 hes [i++] = he;
476 encode_str (enc, str, len, SvUTF8 (sv)); 503 encode_str (enc, str, len, SvUTF8 (sv));
477 encode_ch (enc, '"'); 504 encode_ch (enc, '"');
478 } 505 }
479 else if (SvNOKp (sv)) 506 else if (SvNOKp (sv))
480 { 507 {
508 // trust that perl will do the right thing w.r.t. JSON syntax.
481 need (enc, NV_DIG + 32); 509 need (enc, NV_DIG + 32);
482 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 510 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
483 enc->cur += strlen (enc->cur); 511 enc->cur += strlen (enc->cur);
484 } 512 }
485 else if (SvIOKp (sv)) 513 else if (SvIOKp (sv))
486 { 514 {
487 need (enc, 64); 515 // we assume we can always read an IV as a UV
516 if (SvUV (sv) & ~(UV)0x7fff)
517 {
518 // large integer, use the (rather slow) snprintf way.
519 need (enc, sizeof (UV) * 3);
488 enc->cur += 520 enc->cur +=
489 SvIsUV(sv) 521 SvIsUV(sv)
490 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 522 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
491 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 523 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
524 }
525 else
526 {
527 // optimise the "small number case"
528 // code will likely be branchless and use only a single multiplication
529 I32 i = SvIV (sv);
530 U32 u;
531 char digit, nz = 0;
532
533 need (enc, 6);
534
535 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
536 u = i < 0 ? -i : i;
537
538 // convert to 4.28 fixed-point representation
539 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits
540
541 // now output digit by digit, each time masking out the integer part
542 // and multiplying by 5 while moving the decimal point one to the right,
543 // resulting in a net multiplication by 10.
544 // we always write the digit to memory but conditionally increment
545 // the pointer, to ease the usage of conditional move instructions.
546 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5;
547 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5;
548 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5;
549 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5;
550 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
551 }
492 } 552 }
493 else if (SvROK (sv)) 553 else if (SvROK (sv))
494 encode_rv (enc, SvRV (sv)); 554 encode_rv (enc, SvRV (sv));
495 else if (!SvOK (sv)) 555 else if (!SvOK (sv))
496 encode_str (enc, "null", 4, 0); 556 encode_str (enc, "null", 4, 0);
515 enc.maxdepth = DEC_DEPTH (flags); 575 enc.maxdepth = DEC_DEPTH (flags);
516 576
517 SvPOK_only (enc.sv); 577 SvPOK_only (enc.sv);
518 encode_sv (&enc, scalar); 578 encode_sv (&enc, scalar);
519 579
520 if (!(flags & (F_ASCII | F_UTF8)))
521 SvUTF8_on (enc.sv);
522
523 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 580 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
524 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 581 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
582
583 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
584 SvUTF8_on (enc.sv);
525 585
526 if (enc.flags & F_SHRINK) 586 if (enc.flags & F_SHRINK)
527 shrink (enc.sv); 587 shrink (enc.sv);
528 588
529 return enc.sv; 589 return enc.sv;
541 U32 flags; // F_* 601 U32 flags; // F_*
542 U32 depth; // recursion depth 602 U32 depth; // recursion depth
543 U32 maxdepth; // recursion depth limit 603 U32 maxdepth; // recursion depth limit
544} dec_t; 604} dec_t;
545 605
546static void 606inline void
547decode_ws (dec_t *dec) 607decode_ws (dec_t *dec)
548{ 608{
549 for (;;) 609 for (;;)
550 { 610 {
551 char ch = *dec->cur; 611 char ch = *dec->cur;
577decode_4hex (dec_t *dec) 637decode_4hex (dec_t *dec)
578{ 638{
579 signed char d1, d2, d3, d4; 639 signed char d1, d2, d3, d4;
580 unsigned char *cur = (unsigned char *)dec->cur; 640 unsigned char *cur = (unsigned char *)dec->cur;
581 641
582 d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); 642 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
583 d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); 643 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
584 d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); 644 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
585 d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); 645 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
586 646
587 dec->cur += 4; 647 dec->cur += 4;
588 648
589 return ((UV)d1) << 12 649 return ((UV)d1) << 12
590 | ((UV)d2) << 8 650 | ((UV)d2) << 8
598static SV * 658static SV *
599decode_str (dec_t *dec) 659decode_str (dec_t *dec)
600{ 660{
601 SV *sv = 0; 661 SV *sv = 0;
602 int utf8 = 0; 662 int utf8 = 0;
663 char *dec_cur = dec->cur;
603 664
604 do 665 do
605 { 666 {
606 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 667 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
607 char *cur = buf; 668 char *cur = buf;
608 669
609 do 670 do
610 { 671 {
611 unsigned char ch = *(unsigned char *)dec->cur++; 672 unsigned char ch = *(unsigned char *)dec_cur++;
612 673
613 if (ch == '"') 674 if (expect_false (ch == '"'))
614 { 675 {
615 --dec->cur; 676 --dec_cur;
616 break; 677 break;
617 } 678 }
618 else if (ch == '\\') 679 else if (expect_false (ch == '\\'))
619 { 680 {
620 switch (*dec->cur) 681 switch (*dec_cur)
621 { 682 {
622 case '\\': 683 case '\\':
623 case '/': 684 case '/':
624 case '"': *cur++ = *dec->cur++; break; 685 case '"': *cur++ = *dec_cur++; break;
625 686
626 case 'b': ++dec->cur; *cur++ = '\010'; break; 687 case 'b': ++dec_cur; *cur++ = '\010'; break;
627 case 't': ++dec->cur; *cur++ = '\011'; break; 688 case 't': ++dec_cur; *cur++ = '\011'; break;
628 case 'n': ++dec->cur; *cur++ = '\012'; break; 689 case 'n': ++dec_cur; *cur++ = '\012'; break;
629 case 'f': ++dec->cur; *cur++ = '\014'; break; 690 case 'f': ++dec_cur; *cur++ = '\014'; break;
630 case 'r': ++dec->cur; *cur++ = '\015'; break; 691 case 'r': ++dec_cur; *cur++ = '\015'; break;
631 692
632 case 'u': 693 case 'u':
633 { 694 {
634 UV lo, hi; 695 UV lo, hi;
635 ++dec->cur; 696 ++dec_cur;
636 697
698 dec->cur = dec_cur;
637 hi = decode_4hex (dec); 699 hi = decode_4hex (dec);
700 dec_cur = dec->cur;
638 if (hi == (UV)-1) 701 if (hi == (UV)-1)
639 goto fail; 702 goto fail;
640 703
641 // possibly a surrogate pair 704 // possibly a surrogate pair
642 if (hi >= 0xd800) 705 if (hi >= 0xd800)
643 if (hi < 0xdc00) 706 if (hi < 0xdc00)
644 { 707 {
645 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 708 if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
646 ERR ("missing low surrogate character in surrogate pair"); 709 ERR ("missing low surrogate character in surrogate pair");
647 710
648 dec->cur += 2; 711 dec_cur += 2;
649 712
713 dec->cur = dec_cur;
650 lo = decode_4hex (dec); 714 lo = decode_4hex (dec);
715 dec_cur = dec->cur;
651 if (lo == (UV)-1) 716 if (lo == (UV)-1)
652 goto fail; 717 goto fail;
653 718
654 if (lo < 0xdc00 || lo >= 0xe000) 719 if (lo < 0xdc00 || lo >= 0xe000)
655 ERR ("surrogate pair expected"); 720 ERR ("surrogate pair expected");
669 *cur++ = hi; 734 *cur++ = hi;
670 } 735 }
671 break; 736 break;
672 737
673 default: 738 default:
674 --dec->cur; 739 --dec_cur;
675 ERR ("illegal backslash escape sequence in string"); 740 ERR ("illegal backslash escape sequence in string");
676 } 741 }
677 } 742 }
678 else if (ch >= 0x20 && ch <= 0x7f) 743 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
679 *cur++ = ch; 744 *cur++ = ch;
680 else if (ch >= 0x80) 745 else if (ch >= 0x80)
681 { 746 {
682 STRLEN clen; 747 STRLEN clen;
683 UV uch; 748 UV uch;
684 749
685 --dec->cur; 750 --dec_cur;
686 751
687 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 752 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
688 if (clen == (STRLEN)-1) 753 if (clen == (STRLEN)-1)
689 ERR ("malformed UTF-8 character in JSON string"); 754 ERR ("malformed UTF-8 character in JSON string");
690 755
691 do 756 do
692 *cur++ = *dec->cur++; 757 *cur++ = *dec_cur++;
693 while (--clen); 758 while (--clen);
694 759
695 utf8 = 1; 760 utf8 = 1;
696 } 761 }
697 else 762 else
698 { 763 {
699 --dec->cur; 764 --dec_cur;
700 765
701 if (!ch) 766 if (!ch)
702 ERR ("unexpected end of string while parsing JSON string"); 767 ERR ("unexpected end of string while parsing JSON string");
703 else 768 else
704 ERR ("invalid character encountered while parsing JSON string"); 769 ERR ("invalid character encountered while parsing JSON string");
717 } 782 }
718 else 783 else
719 sv = newSVpvn (buf, len); 784 sv = newSVpvn (buf, len);
720 } 785 }
721 } 786 }
722 while (*dec->cur != '"'); 787 while (*dec_cur != '"');
723 788
724 ++dec->cur; 789 ++dec_cur;
725 790
726 if (sv) 791 if (sv)
727 { 792 {
728 SvPOK_only (sv); 793 SvPOK_only (sv);
729 *SvEND (sv) = 0; 794 *SvEND (sv) = 0;
732 SvUTF8_on (sv); 797 SvUTF8_on (sv);
733 } 798 }
734 else 799 else
735 sv = newSVpvn ("", 0); 800 sv = newSVpvn ("", 0);
736 801
802 dec->cur = dec_cur;
737 return sv; 803 return sv;
738 804
739fail: 805fail:
806 dec->cur = dec_cur;
740 return 0; 807 return 0;
741} 808}
742 809
743static SV * 810static SV *
744decode_num (dec_t *dec) 811decode_num (dec_t *dec)
802 is_nv = 1; 869 is_nv = 1;
803 } 870 }
804 871
805 if (!is_nv) 872 if (!is_nv)
806 { 873 {
807 UV uv; 874 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
808 int numtype = grok_number (start, dec->cur - start, &uv); 875 if (*start == '-')
809 if (numtype & IS_NUMBER_IN_UV) 876 switch (dec->cur - start)
810 if (numtype & IS_NUMBER_NEG)
811 { 877 {
812 if (uv < (UV)IV_MIN) 878 case 2: return newSViv (-( start [1] - '0' * 1));
813 return newSViv (-(IV)uv); 879 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
880 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
881 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
814 } 882 }
883 else
884 switch (dec->cur - start)
885 {
886 case 1: return newSViv ( start [0] - '0' * 1);
887 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
888 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
889 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
890 }
891
892 {
893 UV uv;
894 int numtype = grok_number (start, dec->cur - start, &uv);
895 if (numtype & IS_NUMBER_IN_UV)
896 if (numtype & IS_NUMBER_NEG)
897 {
898 if (uv < (UV)IV_MIN)
899 return newSViv (-(IV)uv);
900 }
815 else 901 else
816 return newSVuv (uv); 902 return newSVuv (uv);
903
904 // here would likely be the place for bigint support
817 } 905 }
906 }
818 907
908 // if we ever support bigint or bigfloat, this is the place for bigfloat
819 return newSVnv (Atof (start)); 909 return newSVnv (Atof (start));
820 910
821fail: 911fail:
822 return 0; 912 return 0;
823} 913}
924 1014
925static SV * 1015static SV *
926decode_sv (dec_t *dec) 1016decode_sv (dec_t *dec)
927{ 1017{
928 decode_ws (dec); 1018 decode_ws (dec);
1019
1020 // the beauty of JSON: you need exactly one character lookahead
1021 // to parse anything.
929 switch (*dec->cur) 1022 switch (*dec->cur)
930 { 1023 {
931 case '"': ++dec->cur; return decode_str (dec); 1024 case '"': ++dec->cur; return decode_str (dec);
932 case '[': ++dec->cur; return decode_av (dec); 1025 case '[': ++dec->cur; return decode_av (dec);
933 case '{': ++dec->cur; return decode_hv (dec); 1026 case '{': ++dec->cur; return decode_hv (dec);
939 1032
940 case 't': 1033 case 't':
941 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1034 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
942 { 1035 {
943 dec->cur += 4; 1036 dec->cur += 4;
944 return newSViv (1); 1037 return SvREFCNT_inc (json_true);
945 } 1038 }
946 else 1039 else
947 ERR ("'true' expected"); 1040 ERR ("'true' expected");
948 1041
949 break; 1042 break;
950 1043
951 case 'f': 1044 case 'f':
952 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1045 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
953 { 1046 {
954 dec->cur += 5; 1047 dec->cur += 5;
955 return newSViv (0); 1048 return SvREFCNT_inc (json_false);
956 } 1049 }
957 else 1050 else
958 ERR ("'false' expected"); 1051 ERR ("'false' expected");
959 1052
960 break; 1053 break;
978fail: 1071fail:
979 return 0; 1072 return 0;
980} 1073}
981 1074
982static SV * 1075static SV *
983decode_json (SV *string, U32 flags) 1076decode_json (SV *string, U32 flags, UV *offset_return)
984{ 1077{
985 dec_t dec; 1078 dec_t dec;
1079 UV offset;
986 SV *sv; 1080 SV *sv;
987 1081
1082 SvGETMAGIC (string);
988 SvUPGRADE (string, SVt_PV); 1083 SvUPGRADE (string, SVt_PV);
989 1084
990 if (flags & F_UTF8) 1085 if (flags & F_UTF8)
991 sv_utf8_downgrade (string, 0); 1086 sv_utf8_downgrade (string, 0);
992 else 1087 else
999 dec.end = SvEND (string); 1094 dec.end = SvEND (string);
1000 dec.err = 0; 1095 dec.err = 0;
1001 dec.depth = 0; 1096 dec.depth = 0;
1002 dec.maxdepth = DEC_DEPTH (dec.flags); 1097 dec.maxdepth = DEC_DEPTH (dec.flags);
1003 1098
1004 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1099 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1005 sv = decode_sv (&dec); 1100 sv = decode_sv (&dec);
1006 1101
1102 if (!(offset_return || !sv))
1103 {
1104 // check for trailing garbage
1105 decode_ws (&dec);
1106
1107 if (*dec.cur)
1108 {
1109 dec.err = "garbage after JSON object";
1110 SvREFCNT_dec (sv);
1111 sv = 0;
1112 }
1113 }
1114
1115 if (offset_return || !sv)
1116 {
1117 offset = dec.flags & F_UTF8
1118 ? dec.cur - SvPVX (string)
1119 : utf8_distance (dec.cur, SvPVX (string));
1120
1121 if (offset_return)
1122 *offset_return = offset;
1123 }
1124
1007 if (!sv) 1125 if (!sv)
1008 { 1126 {
1009 IV offset = dec.flags & F_UTF8
1010 ? dec.cur - SvPVX (string)
1011 : utf8_distance (dec.cur, SvPVX (string));
1012 SV *uni = sv_newmortal (); 1127 SV *uni = sv_newmortal ();
1013 1128
1014 // horrible hack to silence warning inside pv_uni_display 1129 // horrible hack to silence warning inside pv_uni_display
1015 COP cop = *PL_curcop; 1130 COP cop = *PL_curcop;
1016 cop.cop_warnings = pWARN_NONE; 1131 cop.cop_warnings = pWARN_NONE;
1041 1156
1042BOOT: 1157BOOT:
1043{ 1158{
1044 int i; 1159 int i;
1045 1160
1046 memset (decode_hexdigit, 0xff, 256);
1047
1048 for (i = 0; i < 256; ++i) 1161 for (i = 0; i < 256; ++i)
1049 decode_hexdigit [i] = 1162 decode_hexdigit [i] =
1050 i >= '0' && i <= '9' ? i - '0' 1163 i >= '0' && i <= '9' ? i - '0'
1051 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1164 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1052 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1165 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1053 : -1; 1166 : -1;
1054 1167
1055 json_stash = gv_stashpv ("JSON::XS", 1); 1168 json_stash = gv_stashpv ("JSON::XS", 1);
1169
1170 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1171 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1056} 1172}
1057 1173
1058PROTOTYPES: DISABLE 1174PROTOTYPES: DISABLE
1059 1175
1060SV *new (char *dummy) 1176SV *new (char *dummy)
1064 RETVAL 1180 RETVAL
1065 1181
1066SV *ascii (SV *self, int enable = 1) 1182SV *ascii (SV *self, int enable = 1)
1067 ALIAS: 1183 ALIAS:
1068 ascii = F_ASCII 1184 ascii = F_ASCII
1185 latin1 = F_LATIN1
1069 utf8 = F_UTF8 1186 utf8 = F_UTF8
1070 indent = F_INDENT 1187 indent = F_INDENT
1071 canonical = F_CANONICAL 1188 canonical = F_CANONICAL
1072 space_before = F_SPACE_BEFORE 1189 space_before = F_SPACE_BEFORE
1073 space_after = F_SPACE_AFTER 1190 space_after = F_SPACE_AFTER
1109 PPCODE: 1226 PPCODE:
1110 XPUSHs (encode_json (scalar, *SvJSON (self))); 1227 XPUSHs (encode_json (scalar, *SvJSON (self)));
1111 1228
1112void decode (SV *self, SV *jsonstr) 1229void decode (SV *self, SV *jsonstr)
1113 PPCODE: 1230 PPCODE:
1114 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1231 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1232
1233void decode_prefix (SV *self, SV *jsonstr)
1234 PPCODE:
1235{
1236 UV offset;
1237 EXTEND (SP, 2);
1238 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1239 PUSHs (sv_2mortal (newSVuv (offset)));
1240}
1115 1241
1116PROTOTYPES: ENABLE 1242PROTOTYPES: ENABLE
1117 1243
1118void to_json (SV *scalar) 1244void to_json (SV *scalar)
1119 ALIAS: 1245 ALIAS:
1123 1249
1124void from_json (SV *jsonstr) 1250void from_json (SV *jsonstr)
1125 ALIAS: 1251 ALIAS:
1126 jsonToObj = 0 1252 jsonToObj = 0
1127 PPCODE: 1253 PPCODE:
1128 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1254 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1129 1255

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines