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.33 by root, Wed May 23 20:26:40 2007 UTC vs.
Revision 1.38 by root, Mon Jun 11 03:18:07 2007 UTC

32#define F_DEFAULT (9UL << S_MAXDEPTH) 32#define F_DEFAULT (9UL << S_MAXDEPTH)
33 33
34#define INIT_SIZE 32 // initial scalar size to be allocated 34#define INIT_SIZE 32 // initial scalar size to be allocated
35#define INDENT_STEP 3 // spaces per indentation level 35#define INDENT_STEP 3 // spaces per indentation level
36 36
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 }
233 244
234 --len; 245 --len;
235 } 246 }
236} 247}
237 248
238static void 249inline void
239encode_indent (enc_t *enc) 250encode_indent (enc_t *enc)
240{ 251{
241 if (enc->flags & F_INDENT) 252 if (enc->flags & F_INDENT)
242 { 253 {
243 int spaces = enc->indent * INDENT_STEP; 254 int spaces = enc->indent * INDENT_STEP;
246 memset (enc->cur, ' ', spaces); 257 memset (enc->cur, ' ', spaces);
247 enc->cur += spaces; 258 enc->cur += spaces;
248 } 259 }
249} 260}
250 261
251static void 262inline void
252encode_space (enc_t *enc) 263encode_space (enc_t *enc)
253{ 264{
254 need (enc, 1); 265 need (enc, 1);
255 encode_ch (enc, ' '); 266 encode_ch (enc, ' ');
256} 267}
257 268
258static void 269inline void
259encode_nl (enc_t *enc) 270encode_nl (enc_t *enc)
260{ 271{
261 if (enc->flags & F_INDENT) 272 if (enc->flags & F_INDENT)
262 { 273 {
263 need (enc, 1); 274 need (enc, 1);
264 encode_ch (enc, '\n'); 275 encode_ch (enc, '\n');
265 } 276 }
266} 277}
267 278
268static void 279inline void
269encode_comma (enc_t *enc) 280encode_comma (enc_t *enc)
270{ 281{
271 encode_ch (enc, ','); 282 encode_ch (enc, ',');
272 283
273 if (enc->flags & F_INDENT) 284 if (enc->flags & F_INDENT)
374 // that randomises hash orderings 385 // that randomises hash orderings
375 if (enc->flags & F_CANONICAL) 386 if (enc->flags & F_CANONICAL)
376 { 387 {
377 int fast = 1; 388 int fast = 1;
378 HE *he; 389 HE *he;
379#if WIN32 390#if defined(__BORLANDC__) || defined(_MSC_VER)
380 HE **hes = _alloca (count * sizeof (HE)); 391 HE **hes = _alloca (count * sizeof (HE));
381#else 392#else
382 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 393 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
383#endif 394#endif
384 395
492 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 503 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
493 enc->cur += strlen (enc->cur); 504 enc->cur += strlen (enc->cur);
494 } 505 }
495 else if (SvIOKp (sv)) 506 else if (SvIOKp (sv))
496 { 507 {
497 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);
498 enc->cur += 512 enc->cur +=
499 SvIsUV(sv) 513 SvIsUV(sv)
500 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 514 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
501 : 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 }
502 } 540 }
503 else if (SvROK (sv)) 541 else if (SvROK (sv))
504 encode_rv (enc, SvRV (sv)); 542 encode_rv (enc, SvRV (sv));
505 else if (!SvOK (sv)) 543 else if (!SvOK (sv))
506 encode_str (enc, "null", 4, 0); 544 encode_str (enc, "null", 4, 0);
551 U32 flags; // F_* 589 U32 flags; // F_*
552 U32 depth; // recursion depth 590 U32 depth; // recursion depth
553 U32 maxdepth; // recursion depth limit 591 U32 maxdepth; // recursion depth limit
554} dec_t; 592} dec_t;
555 593
556static void 594inline void
557decode_ws (dec_t *dec) 595decode_ws (dec_t *dec)
558{ 596{
559 for (;;) 597 for (;;)
560 { 598 {
561 char ch = *dec->cur; 599 char ch = *dec->cur;
587decode_4hex (dec_t *dec) 625decode_4hex (dec_t *dec)
588{ 626{
589 signed char d1, d2, d3, d4; 627 signed char d1, d2, d3, d4;
590 unsigned char *cur = (unsigned char *)dec->cur; 628 unsigned char *cur = (unsigned char *)dec->cur;
591 629
592 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");
593 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");
594 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");
595 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");
596 634
597 dec->cur += 4; 635 dec->cur += 4;
598 636
599 return ((UV)d1) << 12 637 return ((UV)d1) << 12
600 | ((UV)d2) << 8 638 | ((UV)d2) << 8
608static SV * 646static SV *
609decode_str (dec_t *dec) 647decode_str (dec_t *dec)
610{ 648{
611 SV *sv = 0; 649 SV *sv = 0;
612 int utf8 = 0; 650 int utf8 = 0;
651 char *dec_cur = dec->cur;
613 652
614 do 653 do
615 { 654 {
616 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; 655 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
617 char *cur = buf; 656 char *cur = buf;
618 657
619 do 658 do
620 { 659 {
621 unsigned char ch = *(unsigned char *)dec->cur++; 660 unsigned char ch = *(unsigned char *)dec_cur++;
622 661
623 if (ch == '"') 662 if (expect_false (ch == '"'))
624 { 663 {
625 --dec->cur; 664 --dec_cur;
626 break; 665 break;
627 } 666 }
628 else if (ch == '\\') 667 else if (expect_false (ch == '\\'))
629 { 668 {
630 switch (*dec->cur) 669 switch (*dec_cur)
631 { 670 {
632 case '\\': 671 case '\\':
633 case '/': 672 case '/':
634 case '"': *cur++ = *dec->cur++; break; 673 case '"': *cur++ = *dec_cur++; break;
635 674
636 case 'b': ++dec->cur; *cur++ = '\010'; break; 675 case 'b': ++dec_cur; *cur++ = '\010'; break;
637 case 't': ++dec->cur; *cur++ = '\011'; break; 676 case 't': ++dec_cur; *cur++ = '\011'; break;
638 case 'n': ++dec->cur; *cur++ = '\012'; break; 677 case 'n': ++dec_cur; *cur++ = '\012'; break;
639 case 'f': ++dec->cur; *cur++ = '\014'; break; 678 case 'f': ++dec_cur; *cur++ = '\014'; break;
640 case 'r': ++dec->cur; *cur++ = '\015'; break; 679 case 'r': ++dec_cur; *cur++ = '\015'; break;
641 680
642 case 'u': 681 case 'u':
643 { 682 {
644 UV lo, hi; 683 UV lo, hi;
645 ++dec->cur; 684 ++dec_cur;
646 685
686 dec->cur = dec_cur;
647 hi = decode_4hex (dec); 687 hi = decode_4hex (dec);
688 dec_cur = dec->cur;
648 if (hi == (UV)-1) 689 if (hi == (UV)-1)
649 goto fail; 690 goto fail;
650 691
651 // possibly a surrogate pair 692 // possibly a surrogate pair
652 if (hi >= 0xd800) 693 if (hi >= 0xd800)
653 if (hi < 0xdc00) 694 if (hi < 0xdc00)
654 { 695 {
655 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 696 if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
656 ERR ("missing low surrogate character in surrogate pair"); 697 ERR ("missing low surrogate character in surrogate pair");
657 698
658 dec->cur += 2; 699 dec_cur += 2;
659 700
701 dec->cur = dec_cur;
660 lo = decode_4hex (dec); 702 lo = decode_4hex (dec);
703 dec_cur = dec->cur;
661 if (lo == (UV)-1) 704 if (lo == (UV)-1)
662 goto fail; 705 goto fail;
663 706
664 if (lo < 0xdc00 || lo >= 0xe000) 707 if (lo < 0xdc00 || lo >= 0xe000)
665 ERR ("surrogate pair expected"); 708 ERR ("surrogate pair expected");
679 *cur++ = hi; 722 *cur++ = hi;
680 } 723 }
681 break; 724 break;
682 725
683 default: 726 default:
684 --dec->cur; 727 --dec_cur;
685 ERR ("illegal backslash escape sequence in string"); 728 ERR ("illegal backslash escape sequence in string");
686 } 729 }
687 } 730 }
688 else if (ch >= 0x20 && ch <= 0x7f) 731 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
689 *cur++ = ch; 732 *cur++ = ch;
690 else if (ch >= 0x80) 733 else if (ch >= 0x80)
691 { 734 {
692 STRLEN clen; 735 STRLEN clen;
693 UV uch; 736 UV uch;
694 737
695 --dec->cur; 738 --dec_cur;
696 739
697 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 740 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
698 if (clen == (STRLEN)-1) 741 if (clen == (STRLEN)-1)
699 ERR ("malformed UTF-8 character in JSON string"); 742 ERR ("malformed UTF-8 character in JSON string");
700 743
701 do 744 do
702 *cur++ = *dec->cur++; 745 *cur++ = *dec_cur++;
703 while (--clen); 746 while (--clen);
704 747
705 utf8 = 1; 748 utf8 = 1;
706 } 749 }
707 else 750 else
708 { 751 {
709 --dec->cur; 752 --dec_cur;
710 753
711 if (!ch) 754 if (!ch)
712 ERR ("unexpected end of string while parsing JSON string"); 755 ERR ("unexpected end of string while parsing JSON string");
713 else 756 else
714 ERR ("invalid character encountered while parsing JSON string"); 757 ERR ("invalid character encountered while parsing JSON string");
727 } 770 }
728 else 771 else
729 sv = newSVpvn (buf, len); 772 sv = newSVpvn (buf, len);
730 } 773 }
731 } 774 }
732 while (*dec->cur != '"'); 775 while (*dec_cur != '"');
733 776
734 ++dec->cur; 777 ++dec_cur;
735 778
736 if (sv) 779 if (sv)
737 { 780 {
738 SvPOK_only (sv); 781 SvPOK_only (sv);
739 *SvEND (sv) = 0; 782 *SvEND (sv) = 0;
742 SvUTF8_on (sv); 785 SvUTF8_on (sv);
743 } 786 }
744 else 787 else
745 sv = newSVpvn ("", 0); 788 sv = newSVpvn ("", 0);
746 789
790 dec->cur = dec_cur;
747 return sv; 791 return sv;
748 792
749fail: 793fail:
794 dec->cur = dec_cur;
750 return 0; 795 return 0;
751} 796}
752 797
753static SV * 798static SV *
754decode_num (dec_t *dec) 799decode_num (dec_t *dec)
812 is_nv = 1; 857 is_nv = 1;
813 } 858 }
814 859
815 if (!is_nv) 860 if (!is_nv)
816 { 861 {
817 UV uv; 862 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
818 int numtype = grok_number (start, dec->cur - start, &uv); 863 if (*start == '-')
819 if (numtype & IS_NUMBER_IN_UV) 864 switch (dec->cur - start)
820 if (numtype & IS_NUMBER_NEG)
821 { 865 {
822 if (uv < (UV)IV_MIN) 866 case 2: return newSViv (-( start [1] - '0' ));
823 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));
824 } 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 }
825 else 889 else
826 return newSVuv (uv); 890 return newSVuv (uv);
891 }
827 } 892 }
828 893
829 return newSVnv (Atof (start)); 894 return newSVnv (Atof (start));
830 895
831fail: 896fail:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines