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.38 by root, Mon Jun 11 03:18:07 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
31#define F_DEFAULT (9UL << S_MAXDEPTH) 32#define F_DEFAULT (9UL << S_MAXDEPTH)
32 33
33#define INIT_SIZE 32 // initial scalar size to be allocated 34#define INIT_SIZE 32 // initial scalar size to be allocated
34#define INDENT_STEP 3 // spaces per indentation level 35#define INDENT_STEP 3 // spaces per indentation level
35 36
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 37#define SHORT_STRING_LEN 16384 // 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
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)
41 52
42static HV *json_stash; // JSON::XS:: 53static HV *json_stash; // JSON::XS::
43 54
44///////////////////////////////////////////////////////////////////////////// 55/////////////////////////////////////////////////////////////////////////////
45// utility functions 56// utility functions
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
75static UV 86inline UV
76decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 87decode_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
106static void 117inline void
107need (enc_t *enc, STRLEN len) 118need (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
118static void 129inline void
119encode_ch (enc_t *enc, char ch) 130encode_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 }
181 } 192 }
182 193
183 if (uch > 0x10FFFFUL) 194 if (uch > 0x10FFFFUL)
184 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);
185 196
186 if (uch < 0x80 || enc->flags & F_ASCII) 197 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
187 { 198 {
188 if (uch > 0xFFFFUL) 199 if (uch > 0xFFFFUL)
189 { 200 {
190 need (enc, len += 11); 201 need (enc, len += 11);
191 sprintf (enc->cur, "\\u%04x\\u%04x", 202 sprintf (enc->cur, "\\u%04x\\u%04x",
205 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 216 *enc->cur++ = hexdigit [(uch >> 0) & 15];
206 } 217 }
207 218
208 str += clen; 219 str += clen;
209 } 220 }
221 else if (enc->flags & F_LATIN1)
222 {
223 *enc->cur++ = uch;
224 str += clen;
225 }
210 else if (is_utf8) 226 else if (is_utf8)
211 { 227 {
212 need (enc, len += clen); 228 need (enc, len += clen);
213 do 229 do
214 { 230 {
216 } 232 }
217 while (--clen); 233 while (--clen);
218 } 234 }
219 else 235 else
220 { 236 {
221 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 237 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
222 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 238 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
223 ++str; 239 ++str;
224 } 240 }
225 } 241 }
226 } 242 }
228 244
229 --len; 245 --len;
230 } 246 }
231} 247}
232 248
233static void 249inline void
234encode_indent (enc_t *enc) 250encode_indent (enc_t *enc)
235{ 251{
236 if (enc->flags & F_INDENT) 252 if (enc->flags & F_INDENT)
237 { 253 {
238 int spaces = enc->indent * INDENT_STEP; 254 int spaces = enc->indent * INDENT_STEP;
241 memset (enc->cur, ' ', spaces); 257 memset (enc->cur, ' ', spaces);
242 enc->cur += spaces; 258 enc->cur += spaces;
243 } 259 }
244} 260}
245 261
246static void 262inline void
247encode_space (enc_t *enc) 263encode_space (enc_t *enc)
248{ 264{
249 need (enc, 1); 265 need (enc, 1);
250 encode_ch (enc, ' '); 266 encode_ch (enc, ' ');
251} 267}
252 268
253static void 269inline void
254encode_nl (enc_t *enc) 270encode_nl (enc_t *enc)
255{ 271{
256 if (enc->flags & F_INDENT) 272 if (enc->flags & F_INDENT)
257 { 273 {
258 need (enc, 1); 274 need (enc, 1);
259 encode_ch (enc, '\n'); 275 encode_ch (enc, '\n');
260 } 276 }
261} 277}
262 278
263static void 279inline void
264encode_comma (enc_t *enc) 280encode_comma (enc_t *enc)
265{ 281{
266 encode_ch (enc, ','); 282 encode_ch (enc, ',');
267 283
268 if (enc->flags & F_INDENT) 284 if (enc->flags & F_INDENT)
367 // actually, this is mostly due to the stupid so-called 383 // actually, this is mostly due to the stupid so-called
368 // security workaround added somewhere in 5.8.x. 384 // security workaround added somewhere in 5.8.x.
369 // that randomises hash orderings 385 // that randomises hash orderings
370 if (enc->flags & F_CANONICAL) 386 if (enc->flags & F_CANONICAL)
371 { 387 {
372 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
373 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
374 395
375 i = 0; 396 i = 0;
376 while ((he = hv_iternext (hv))) 397 while ((he = hv_iternext (hv)))
377 { 398 {
378 hes [i++] = he; 399 hes [i++] = he;
482 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 503 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
483 enc->cur += strlen (enc->cur); 504 enc->cur += strlen (enc->cur);
484 } 505 }
485 else if (SvIOKp (sv)) 506 else if (SvIOKp (sv))
486 { 507 {
487 need (enc, 64); 508 // we assume we can always read an IV as a UV
509 if (SvUV (sv) & ~(UV)0x7fff)
510 {
511 need (enc, sizeof (UV) * 3);
488 enc->cur += 512 enc->cur +=
489 SvIsUV(sv) 513 SvIsUV(sv)
490 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 514 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
491 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 515 : snprintf (enc->cur, sizeof (UV) * 3, "%"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'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5;
535 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5;
536 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5;
537 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5;
538 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1;
539 }
492 } 540 }
493 else if (SvROK (sv)) 541 else if (SvROK (sv))
494 encode_rv (enc, SvRV (sv)); 542 encode_rv (enc, SvRV (sv));
495 else if (!SvOK (sv)) 543 else if (!SvOK (sv))
496 encode_str (enc, "null", 4, 0); 544 encode_str (enc, "null", 4, 0);
515 enc.maxdepth = DEC_DEPTH (flags); 563 enc.maxdepth = DEC_DEPTH (flags);
516 564
517 SvPOK_only (enc.sv); 565 SvPOK_only (enc.sv);
518 encode_sv (&enc, scalar); 566 encode_sv (&enc, scalar);
519 567
520 if (!(flags & (F_ASCII | F_UTF8)))
521 SvUTF8_on (enc.sv);
522
523 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 568 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
524 *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);
525 573
526 if (enc.flags & F_SHRINK) 574 if (enc.flags & F_SHRINK)
527 shrink (enc.sv); 575 shrink (enc.sv);
528 576
529 return enc.sv; 577 return enc.sv;
541 U32 flags; // F_* 589 U32 flags; // F_*
542 U32 depth; // recursion depth 590 U32 depth; // recursion depth
543 U32 maxdepth; // recursion depth limit 591 U32 maxdepth; // recursion depth limit
544} dec_t; 592} dec_t;
545 593
546static void 594inline void
547decode_ws (dec_t *dec) 595decode_ws (dec_t *dec)
548{ 596{
549 for (;;) 597 for (;;)
550 { 598 {
551 char ch = *dec->cur; 599 char ch = *dec->cur;
577decode_4hex (dec_t *dec) 625decode_4hex (dec_t *dec)
578{ 626{
579 signed char d1, d2, d3, d4; 627 signed char d1, d2, d3, d4;
580 unsigned char *cur = (unsigned char *)dec->cur; 628 unsigned char *cur = (unsigned char *)dec->cur;
581 629
582 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");
583 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");
584 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");
585 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");
586 634
587 dec->cur += 4; 635 dec->cur += 4;
588 636
589 return ((UV)d1) << 12 637 return ((UV)d1) << 12
590 | ((UV)d2) << 8 638 | ((UV)d2) << 8
598static SV * 646static SV *
599decode_str (dec_t *dec) 647decode_str (dec_t *dec)
600{ 648{
601 SV *sv = 0; 649 SV *sv = 0;
602 int utf8 = 0; 650 int utf8 = 0;
651 char *dec_cur = dec->cur;
603 652
604 do 653 do
605 { 654 {
606 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 655 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
607 char *cur = buf; 656 char *cur = buf;
608 657
609 do 658 do
610 { 659 {
611 unsigned char ch = *(unsigned char *)dec->cur++; 660 unsigned char ch = *(unsigned char *)dec_cur++;
612 661
613 if (ch == '"') 662 if (expect_false (ch == '"'))
614 { 663 {
615 --dec->cur; 664 --dec_cur;
616 break; 665 break;
617 } 666 }
618 else if (ch == '\\') 667 else if (expect_false (ch == '\\'))
619 { 668 {
620 switch (*dec->cur) 669 switch (*dec_cur)
621 { 670 {
622 case '\\': 671 case '\\':
623 case '/': 672 case '/':
624 case '"': *cur++ = *dec->cur++; break; 673 case '"': *cur++ = *dec_cur++; break;
625 674
626 case 'b': ++dec->cur; *cur++ = '\010'; break; 675 case 'b': ++dec_cur; *cur++ = '\010'; break;
627 case 't': ++dec->cur; *cur++ = '\011'; break; 676 case 't': ++dec_cur; *cur++ = '\011'; break;
628 case 'n': ++dec->cur; *cur++ = '\012'; break; 677 case 'n': ++dec_cur; *cur++ = '\012'; break;
629 case 'f': ++dec->cur; *cur++ = '\014'; break; 678 case 'f': ++dec_cur; *cur++ = '\014'; break;
630 case 'r': ++dec->cur; *cur++ = '\015'; break; 679 case 'r': ++dec_cur; *cur++ = '\015'; break;
631 680
632 case 'u': 681 case 'u':
633 { 682 {
634 UV lo, hi; 683 UV lo, hi;
635 ++dec->cur; 684 ++dec_cur;
636 685
686 dec->cur = dec_cur;
637 hi = decode_4hex (dec); 687 hi = decode_4hex (dec);
688 dec_cur = dec->cur;
638 if (hi == (UV)-1) 689 if (hi == (UV)-1)
639 goto fail; 690 goto fail;
640 691
641 // possibly a surrogate pair 692 // possibly a surrogate pair
642 if (hi >= 0xd800) 693 if (hi >= 0xd800)
643 if (hi < 0xdc00) 694 if (hi < 0xdc00)
644 { 695 {
645 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 696 if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
646 ERR ("missing low surrogate character in surrogate pair"); 697 ERR ("missing low surrogate character in surrogate pair");
647 698
648 dec->cur += 2; 699 dec_cur += 2;
649 700
701 dec->cur = dec_cur;
650 lo = decode_4hex (dec); 702 lo = decode_4hex (dec);
703 dec_cur = dec->cur;
651 if (lo == (UV)-1) 704 if (lo == (UV)-1)
652 goto fail; 705 goto fail;
653 706
654 if (lo < 0xdc00 || lo >= 0xe000) 707 if (lo < 0xdc00 || lo >= 0xe000)
655 ERR ("surrogate pair expected"); 708 ERR ("surrogate pair expected");
669 *cur++ = hi; 722 *cur++ = hi;
670 } 723 }
671 break; 724 break;
672 725
673 default: 726 default:
674 --dec->cur; 727 --dec_cur;
675 ERR ("illegal backslash escape sequence in string"); 728 ERR ("illegal backslash escape sequence in string");
676 } 729 }
677 } 730 }
678 else if (ch >= 0x20 && ch <= 0x7f) 731 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
679 *cur++ = ch; 732 *cur++ = ch;
680 else if (ch >= 0x80) 733 else if (ch >= 0x80)
681 { 734 {
682 STRLEN clen; 735 STRLEN clen;
683 UV uch; 736 UV uch;
684 737
685 --dec->cur; 738 --dec_cur;
686 739
687 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 740 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
688 if (clen == (STRLEN)-1) 741 if (clen == (STRLEN)-1)
689 ERR ("malformed UTF-8 character in JSON string"); 742 ERR ("malformed UTF-8 character in JSON string");
690 743
691 do 744 do
692 *cur++ = *dec->cur++; 745 *cur++ = *dec_cur++;
693 while (--clen); 746 while (--clen);
694 747
695 utf8 = 1; 748 utf8 = 1;
696 } 749 }
697 else 750 else
698 { 751 {
699 --dec->cur; 752 --dec_cur;
700 753
701 if (!ch) 754 if (!ch)
702 ERR ("unexpected end of string while parsing JSON string"); 755 ERR ("unexpected end of string while parsing JSON string");
703 else 756 else
704 ERR ("invalid character encountered while parsing JSON string"); 757 ERR ("invalid character encountered while parsing JSON string");
717 } 770 }
718 else 771 else
719 sv = newSVpvn (buf, len); 772 sv = newSVpvn (buf, len);
720 } 773 }
721 } 774 }
722 while (*dec->cur != '"'); 775 while (*dec_cur != '"');
723 776
724 ++dec->cur; 777 ++dec_cur;
725 778
726 if (sv) 779 if (sv)
727 { 780 {
728 SvPOK_only (sv); 781 SvPOK_only (sv);
729 *SvEND (sv) = 0; 782 *SvEND (sv) = 0;
732 SvUTF8_on (sv); 785 SvUTF8_on (sv);
733 } 786 }
734 else 787 else
735 sv = newSVpvn ("", 0); 788 sv = newSVpvn ("", 0);
736 789
790 dec->cur = dec_cur;
737 return sv; 791 return sv;
738 792
739fail: 793fail:
794 dec->cur = dec_cur;
740 return 0; 795 return 0;
741} 796}
742 797
743static SV * 798static SV *
744decode_num (dec_t *dec) 799decode_num (dec_t *dec)
802 is_nv = 1; 857 is_nv = 1;
803 } 858 }
804 859
805 if (!is_nv) 860 if (!is_nv)
806 { 861 {
807 UV uv; 862 // 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); 863 if (*start == '-')
809 if (numtype & IS_NUMBER_IN_UV) 864 switch (dec->cur - start)
810 if (numtype & IS_NUMBER_NEG)
811 { 865 {
812 if (uv < (UV)IV_MIN) 866 case 2: return newSViv (-( start [1] - '0' ));
813 return newSViv (-(IV)uv); 867 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
868 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
869 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
814 } 870 }
871 else
872 switch (dec->cur - start)
873 {
874 case 1: return newSViv ( start [0] - '0' );
875 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
876 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
877 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
878 }
879
880 {
881 UV uv;
882 int numtype = grok_number (start, dec->cur - start, &uv);
883 if (numtype & IS_NUMBER_IN_UV)
884 if (numtype & IS_NUMBER_NEG)
885 {
886 if (uv < (UV)IV_MIN)
887 return newSViv (-(IV)uv);
888 }
815 else 889 else
816 return newSVuv (uv); 890 return newSVuv (uv);
891 }
817 } 892 }
818 893
819 return newSVnv (Atof (start)); 894 return newSVnv (Atof (start));
820 895
821fail: 896fail:
978fail: 1053fail:
979 return 0; 1054 return 0;
980} 1055}
981 1056
982static SV * 1057static SV *
983decode_json (SV *string, U32 flags) 1058decode_json (SV *string, U32 flags, UV *offset_return)
984{ 1059{
985 dec_t dec; 1060 dec_t dec;
1061 UV offset;
986 SV *sv; 1062 SV *sv;
987 1063
1064 SvGETMAGIC (string);
988 SvUPGRADE (string, SVt_PV); 1065 SvUPGRADE (string, SVt_PV);
989 1066
990 if (flags & F_UTF8) 1067 if (flags & F_UTF8)
991 sv_utf8_downgrade (string, 0); 1068 sv_utf8_downgrade (string, 0);
992 else 1069 else
999 dec.end = SvEND (string); 1076 dec.end = SvEND (string);
1000 dec.err = 0; 1077 dec.err = 0;
1001 dec.depth = 0; 1078 dec.depth = 0;
1002 dec.maxdepth = DEC_DEPTH (dec.flags); 1079 dec.maxdepth = DEC_DEPTH (dec.flags);
1003 1080
1004 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1081 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1005 sv = decode_sv (&dec); 1082 sv = decode_sv (&dec);
1006 1083
1084 if (!(offset_return || !sv))
1085 {
1086 // check for trailing garbage
1087 decode_ws (&dec);
1088
1089 if (*dec.cur)
1090 {
1091 dec.err = "garbage after JSON object";
1092 SvREFCNT_dec (sv);
1093 sv = 0;
1094 }
1095 }
1096
1097 if (offset_return || !sv)
1098 {
1099 offset = dec.flags & F_UTF8
1100 ? dec.cur - SvPVX (string)
1101 : utf8_distance (dec.cur, SvPVX (string));
1102
1103 if (offset_return)
1104 *offset_return = offset;
1105 }
1106
1007 if (!sv) 1107 if (!sv)
1008 { 1108 {
1009 IV offset = dec.flags & F_UTF8
1010 ? dec.cur - SvPVX (string)
1011 : utf8_distance (dec.cur, SvPVX (string));
1012 SV *uni = sv_newmortal (); 1109 SV *uni = sv_newmortal ();
1013 1110
1014 // horrible hack to silence warning inside pv_uni_display 1111 // horrible hack to silence warning inside pv_uni_display
1015 COP cop = *PL_curcop; 1112 COP cop = *PL_curcop;
1016 cop.cop_warnings = pWARN_NONE; 1113 cop.cop_warnings = pWARN_NONE;
1064 RETVAL 1161 RETVAL
1065 1162
1066SV *ascii (SV *self, int enable = 1) 1163SV *ascii (SV *self, int enable = 1)
1067 ALIAS: 1164 ALIAS:
1068 ascii = F_ASCII 1165 ascii = F_ASCII
1166 latin1 = F_LATIN1
1069 utf8 = F_UTF8 1167 utf8 = F_UTF8
1070 indent = F_INDENT 1168 indent = F_INDENT
1071 canonical = F_CANONICAL 1169 canonical = F_CANONICAL
1072 space_before = F_SPACE_BEFORE 1170 space_before = F_SPACE_BEFORE
1073 space_after = F_SPACE_AFTER 1171 space_after = F_SPACE_AFTER
1109 PPCODE: 1207 PPCODE:
1110 XPUSHs (encode_json (scalar, *SvJSON (self))); 1208 XPUSHs (encode_json (scalar, *SvJSON (self)));
1111 1209
1112void decode (SV *self, SV *jsonstr) 1210void decode (SV *self, SV *jsonstr)
1113 PPCODE: 1211 PPCODE:
1114 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1212 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1213
1214void decode_prefix (SV *self, SV *jsonstr)
1215 PPCODE:
1216{
1217 UV offset;
1218 EXTEND (SP, 2);
1219 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1220 PUSHs (sv_2mortal (newSVuv (offset)));
1221}
1115 1222
1116PROTOTYPES: ENABLE 1223PROTOTYPES: ENABLE
1117 1224
1118void to_json (SV *scalar) 1225void to_json (SV *scalar)
1119 ALIAS: 1226 ALIAS:
1123 1230
1124void from_json (SV *jsonstr) 1231void from_json (SV *jsonstr)
1125 ALIAS: 1232 ALIAS:
1126 jsonToObj = 0 1233 jsonToObj = 0
1127 PPCODE: 1234 PPCODE:
1128 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1235 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1129 1236

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines