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.35 by root, Wed Jun 6 14:52:49 2007 UTC vs.
Revision 1.43 by root, Sat Jun 23 23:49:29 2007 UTC

7#include "stdlib.h" 7#include "stdlib.h"
8#include "stdio.h" 8#include "stdio.h"
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
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
12#endif 18#endif
13 19
14#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL 21#define F_LATIN1 0x00000002UL
16#define F_UTF8 0x00000004UL 22#define F_UTF8 0x00000004UL
32#define F_DEFAULT (9UL << S_MAXDEPTH) 38#define F_DEFAULT (9UL << S_MAXDEPTH)
33 39
34#define INIT_SIZE 32 // initial scalar size to be allocated 40#define INIT_SIZE 32 // initial scalar size to be allocated
35#define INDENT_STEP 3 // spaces per indentation level 41#define INDENT_STEP 3 // spaces per indentation level
36 42
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
42#if __GNUC__ >= 3 48#if __GNUC__ >= 3
49 55
50#define expect_false(expr) expect ((expr) != 0, 0) 56#define expect_false(expr) expect ((expr) != 0, 0)
51#define expect_true(expr) expect ((expr) != 0, 1) 57#define expect_true(expr) expect ((expr) != 0, 1)
52 58
53static HV *json_stash; // JSON::XS:: 59static HV *json_stash; // JSON::XS::
60static SV *json_true, *json_false;
54 61
55///////////////////////////////////////////////////////////////////////////// 62/////////////////////////////////////////////////////////////////////////////
56// utility functions 63// utility functions
57 64
58static UV * 65static UV *
178 STRLEN clen; 185 STRLEN clen;
179 UV uch; 186 UV uch;
180 187
181 if (is_utf8) 188 if (is_utf8)
182 { 189 {
183 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
184 uch = decode_utf8 (str, end - str, &clen); 190 uch = decode_utf8 (str, end - str, &clen);
185 if (clen == (STRLEN)-1) 191 if (clen == (STRLEN)-1)
186 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);
187 } 193 }
188 else 194 else
497 encode_str (enc, str, len, SvUTF8 (sv)); 503 encode_str (enc, str, len, SvUTF8 (sv));
498 encode_ch (enc, '"'); 504 encode_ch (enc, '"');
499 } 505 }
500 else if (SvNOKp (sv)) 506 else if (SvNOKp (sv))
501 { 507 {
508 // trust that perl will do the right thing w.r.t. JSON syntax.
502 need (enc, NV_DIG + 32); 509 need (enc, NV_DIG + 32);
503 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 510 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
504 enc->cur += strlen (enc->cur); 511 enc->cur += strlen (enc->cur);
505 } 512 }
506 else if (SvIOKp (sv)) 513 else if (SvIOKp (sv))
507 { 514 {
508 // we assume we can always read an IV as a UV 515 // we assume we can always read an IV as a UV
509 if (SvUV (sv) & ~(UV)0x7fff) 516 if (SvUV (sv) & ~(UV)0x7fff)
510 { 517 {
518 // large integer, use the (rather slow) snprintf way.
511 need (enc, 32); 519 need (enc, sizeof (UV) * 3);
512 enc->cur += 520 enc->cur +=
513 SvIsUV(sv) 521 SvIsUV(sv)
514 ? snprintf (enc->cur, 32, "%"UVuf, (UV)SvUVX (sv)) 522 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
515 : snprintf (enc->cur, 32, "%"IVdf, (IV)SvIVX (sv)); 523 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
516 } 524 }
517 else 525 else
518 { 526 {
519 // optimise the "small number case" 527 // optimise the "small number case"
520 // code will likely be branchless and use only a single multiplication 528 // code will likely be branchless and use only a single multiplication
521 I32 i = SvIV (sv); 529 I32 i = SvIV (sv);
522 U32 u; 530 U32 u;
531 char digit, nz = 0;
523 532
524 need (enc, 6); 533 need (enc, 6);
525 534
526 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; 535 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
527 u = i < 0 ? -i : i; 536 u = i < 0 ? -i : i;
528 537
529 // convert to 4.28 fixed-point representation 538 // convert to 4.28 fixed-point representation
530 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits 539 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits
531 540
532 char digit, nz = 0; 541 // now output digit by digit, each time masking out the integer part
533 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.
534 digit = u >> 28; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0xfffffff) * 5; 546 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5;
535 digit = u >> 27; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x7ffffff) * 5; 547 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5;
536 digit = u >> 26; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x3ffffff) * 5; 548 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5;
537 digit = u >> 25; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x1ffffff) * 5; 549 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5;
538 digit = u >> 24; *enc->cur = digit + '0'; nz |= digit; enc->cur += 1; 550 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
539 } 551 }
540 } 552 }
541 else if (SvROK (sv)) 553 else if (SvROK (sv))
542 encode_rv (enc, SvRV (sv)); 554 encode_rv (enc, SvRV (sv));
543 else if (!SvOK (sv)) 555 else if (!SvOK (sv))
625decode_4hex (dec_t *dec) 637decode_4hex (dec_t *dec)
626{ 638{
627 signed char d1, d2, d3, d4; 639 signed char d1, d2, d3, d4;
628 unsigned char *cur = (unsigned char *)dec->cur; 640 unsigned char *cur = (unsigned char *)dec->cur;
629 641
630 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); 642 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
631 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); 643 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
632 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); 644 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
633 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); 645 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
634 646
635 dec->cur += 4; 647 dec->cur += 4;
636 648
637 return ((UV)d1) << 12 649 return ((UV)d1) << 12
638 | ((UV)d2) << 8 650 | ((UV)d2) << 8
646static SV * 658static SV *
647decode_str (dec_t *dec) 659decode_str (dec_t *dec)
648{ 660{
649 SV *sv = 0; 661 SV *sv = 0;
650 int utf8 = 0; 662 int utf8 = 0;
663 char *dec_cur = dec->cur;
651 664
652 do 665 do
653 { 666 {
654 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; 667 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
655 char *cur = buf; 668 char *cur = buf;
656 669
657 do 670 do
658 { 671 {
659 unsigned char ch = *(unsigned char *)dec->cur++; 672 unsigned char ch = *(unsigned char *)dec_cur++;
660 673
661 if (expect_false (ch == '"')) 674 if (expect_false (ch == '"'))
662 { 675 {
663 --dec->cur; 676 --dec_cur;
664 break; 677 break;
665 } 678 }
666 else if (expect_false (ch == '\\')) 679 else if (expect_false (ch == '\\'))
667 { 680 {
668 switch (*dec->cur) 681 switch (*dec_cur)
669 { 682 {
670 case '\\': 683 case '\\':
671 case '/': 684 case '/':
672 case '"': *cur++ = *dec->cur++; break; 685 case '"': *cur++ = *dec_cur++; break;
673 686
674 case 'b': ++dec->cur; *cur++ = '\010'; break; 687 case 'b': ++dec_cur; *cur++ = '\010'; break;
675 case 't': ++dec->cur; *cur++ = '\011'; break; 688 case 't': ++dec_cur; *cur++ = '\011'; break;
676 case 'n': ++dec->cur; *cur++ = '\012'; break; 689 case 'n': ++dec_cur; *cur++ = '\012'; break;
677 case 'f': ++dec->cur; *cur++ = '\014'; break; 690 case 'f': ++dec_cur; *cur++ = '\014'; break;
678 case 'r': ++dec->cur; *cur++ = '\015'; break; 691 case 'r': ++dec_cur; *cur++ = '\015'; break;
679 692
680 case 'u': 693 case 'u':
681 { 694 {
682 UV lo, hi; 695 UV lo, hi;
683 ++dec->cur; 696 ++dec_cur;
684 697
698 dec->cur = dec_cur;
685 hi = decode_4hex (dec); 699 hi = decode_4hex (dec);
700 dec_cur = dec->cur;
686 if (hi == (UV)-1) 701 if (hi == (UV)-1)
687 goto fail; 702 goto fail;
688 703
689 // possibly a surrogate pair 704 // possibly a surrogate pair
690 if (hi >= 0xd800) 705 if (hi >= 0xd800)
691 if (hi < 0xdc00) 706 if (hi < 0xdc00)
692 { 707 {
693 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 708 if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
694 ERR ("missing low surrogate character in surrogate pair"); 709 ERR ("missing low surrogate character in surrogate pair");
695 710
696 dec->cur += 2; 711 dec_cur += 2;
697 712
713 dec->cur = dec_cur;
698 lo = decode_4hex (dec); 714 lo = decode_4hex (dec);
715 dec_cur = dec->cur;
699 if (lo == (UV)-1) 716 if (lo == (UV)-1)
700 goto fail; 717 goto fail;
701 718
702 if (lo < 0xdc00 || lo >= 0xe000) 719 if (lo < 0xdc00 || lo >= 0xe000)
703 ERR ("surrogate pair expected"); 720 ERR ("surrogate pair expected");
717 *cur++ = hi; 734 *cur++ = hi;
718 } 735 }
719 break; 736 break;
720 737
721 default: 738 default:
722 --dec->cur; 739 --dec_cur;
723 ERR ("illegal backslash escape sequence in string"); 740 ERR ("illegal backslash escape sequence in string");
724 } 741 }
725 } 742 }
726 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 743 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
727 *cur++ = ch; 744 *cur++ = ch;
728 else if (ch >= 0x80) 745 else if (ch >= 0x80)
729 { 746 {
730 STRLEN clen; 747 STRLEN clen;
731 UV uch; 748 UV uch;
732 749
733 --dec->cur; 750 --dec_cur;
734 751
735 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 752 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
736 if (clen == (STRLEN)-1) 753 if (clen == (STRLEN)-1)
737 ERR ("malformed UTF-8 character in JSON string"); 754 ERR ("malformed UTF-8 character in JSON string");
738 755
739 do 756 do
740 *cur++ = *dec->cur++; 757 *cur++ = *dec_cur++;
741 while (--clen); 758 while (--clen);
742 759
743 utf8 = 1; 760 utf8 = 1;
744 } 761 }
745 else 762 else
746 { 763 {
747 --dec->cur; 764 --dec_cur;
748 765
749 if (!ch) 766 if (!ch)
750 ERR ("unexpected end of string while parsing JSON string"); 767 ERR ("unexpected end of string while parsing JSON string");
751 else 768 else
752 ERR ("invalid character encountered while parsing JSON string"); 769 ERR ("invalid character encountered while parsing JSON string");
765 } 782 }
766 else 783 else
767 sv = newSVpvn (buf, len); 784 sv = newSVpvn (buf, len);
768 } 785 }
769 } 786 }
770 while (*dec->cur != '"'); 787 while (*dec_cur != '"');
771 788
772 ++dec->cur; 789 ++dec_cur;
773 790
774 if (sv) 791 if (sv)
775 { 792 {
776 SvPOK_only (sv); 793 SvPOK_only (sv);
777 *SvEND (sv) = 0; 794 *SvEND (sv) = 0;
780 SvUTF8_on (sv); 797 SvUTF8_on (sv);
781 } 798 }
782 else 799 else
783 sv = newSVpvn ("", 0); 800 sv = newSVpvn ("", 0);
784 801
802 dec->cur = dec_cur;
785 return sv; 803 return sv;
786 804
787fail: 805fail:
806 dec->cur = dec_cur;
788 return 0; 807 return 0;
789} 808}
790 809
791static SV * 810static SV *
792decode_num (dec_t *dec) 811decode_num (dec_t *dec)
854 { 873 {
855 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 874 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
856 if (*start == '-') 875 if (*start == '-')
857 switch (dec->cur - start) 876 switch (dec->cur - start)
858 { 877 {
859 case 2: return newSViv (-( start [1] - '0' )); 878 case 2: return newSViv (-( start [1] - '0' * 1));
860 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 879 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)); 880 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)); 881 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
863 } 882 }
864 else 883 else
865 switch (dec->cur - start) 884 switch (dec->cur - start)
866 { 885 {
867 case 1: return newSViv ( start [0] - '0' ); 886 case 1: return newSViv ( start [0] - '0' * 1);
868 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 887 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); 888 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); 889 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
871 } 890 }
872 891
879 if (uv < (UV)IV_MIN) 898 if (uv < (UV)IV_MIN)
880 return newSViv (-(IV)uv); 899 return newSViv (-(IV)uv);
881 } 900 }
882 else 901 else
883 return newSVuv (uv); 902 return newSVuv (uv);
903
904 // here would likely be the place for bigint support
884 } 905 }
885 } 906 }
886 907
908 // if we ever support bigint or bigfloat, this is the place for bigfloat
887 return newSVnv (Atof (start)); 909 return newSVnv (Atof (start));
888 910
889fail: 911fail:
890 return 0; 912 return 0;
891} 913}
992 1014
993static SV * 1015static SV *
994decode_sv (dec_t *dec) 1016decode_sv (dec_t *dec)
995{ 1017{
996 decode_ws (dec); 1018 decode_ws (dec);
1019
1020 // the beauty of JSON: you need exactly one character lookahead
1021 // to parse anything.
997 switch (*dec->cur) 1022 switch (*dec->cur)
998 { 1023 {
999 case '"': ++dec->cur; return decode_str (dec); 1024 case '"': ++dec->cur; return decode_str (dec);
1000 case '[': ++dec->cur; return decode_av (dec); 1025 case '[': ++dec->cur; return decode_av (dec);
1001 case '{': ++dec->cur; return decode_hv (dec); 1026 case '{': ++dec->cur; return decode_hv (dec);
1007 1032
1008 case 't': 1033 case 't':
1009 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1034 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1010 { 1035 {
1011 dec->cur += 4; 1036 dec->cur += 4;
1012 return newSViv (1); 1037 return SvREFCNT_inc (json_true);
1013 } 1038 }
1014 else 1039 else
1015 ERR ("'true' expected"); 1040 ERR ("'true' expected");
1016 1041
1017 break; 1042 break;
1018 1043
1019 case 'f': 1044 case 'f':
1020 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1045 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1021 { 1046 {
1022 dec->cur += 5; 1047 dec->cur += 5;
1023 return newSViv (0); 1048 return SvREFCNT_inc (json_false);
1024 } 1049 }
1025 else 1050 else
1026 ERR ("'false' expected"); 1051 ERR ("'false' expected");
1027 1052
1028 break; 1053 break;
1131 1156
1132BOOT: 1157BOOT:
1133{ 1158{
1134 int i; 1159 int i;
1135 1160
1136 memset (decode_hexdigit, 0xff, 256);
1137
1138 for (i = 0; i < 256; ++i) 1161 for (i = 0; i < 256; ++i)
1139 decode_hexdigit [i] = 1162 decode_hexdigit [i] =
1140 i >= '0' && i <= '9' ? i - '0' 1163 i >= '0' && i <= '9' ? i - '0'
1141 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1164 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1142 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1165 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1143 : -1; 1166 : -1;
1144 1167
1145 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);
1146} 1172}
1147 1173
1148PROTOTYPES: DISABLE 1174PROTOTYPES: DISABLE
1149 1175
1150SV *new (char *dummy) 1176SV *new (char *dummy)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines