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.39 by root, Mon Jun 11 03:42:57 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
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
631decode_4hex (dec_t *dec) 637decode_4hex (dec_t *dec)
632{ 638{
633 signed char d1, d2, d3, d4; 639 signed char d1, d2, d3, d4;
634 unsigned char *cur = (unsigned char *)dec->cur; 640 unsigned char *cur = (unsigned char *)dec->cur;
635 641
636 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");
637 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");
638 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");
639 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");
640 646
641 dec->cur += 4; 647 dec->cur += 4;
642 648
643 return ((UV)d1) << 12 649 return ((UV)d1) << 12
644 | ((UV)d2) << 8 650 | ((UV)d2) << 8
867 { 873 {
868 // 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
869 if (*start == '-') 875 if (*start == '-')
870 switch (dec->cur - start) 876 switch (dec->cur - start)
871 { 877 {
872 case 2: return newSViv (-( start [1] - '0' )); 878 case 2: return newSViv (-( start [1] - '0' * 1));
873 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 879 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
874 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));
875 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));
876 } 882 }
877 else 883 else
878 switch (dec->cur - start) 884 switch (dec->cur - start)
879 { 885 {
880 case 1: return newSViv ( start [0] - '0' ); 886 case 1: return newSViv ( start [0] - '0' * 1);
881 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 887 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
882 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);
883 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);
884 } 890 }
885 891
892 if (uv < (UV)IV_MIN) 898 if (uv < (UV)IV_MIN)
893 return newSViv (-(IV)uv); 899 return newSViv (-(IV)uv);
894 } 900 }
895 else 901 else
896 return newSVuv (uv); 902 return newSVuv (uv);
903
904 // here would likely be the place for bigint support
897 } 905 }
898 } 906 }
899 907
908 // if we ever support bigint or bigfloat, this is the place for bigfloat
900 return newSVnv (Atof (start)); 909 return newSVnv (Atof (start));
901 910
902fail: 911fail:
903 return 0; 912 return 0;
904} 913}
1005 1014
1006static SV * 1015static SV *
1007decode_sv (dec_t *dec) 1016decode_sv (dec_t *dec)
1008{ 1017{
1009 decode_ws (dec); 1018 decode_ws (dec);
1019
1020 // the beauty of JSON: you need exactly one character lookahead
1021 // to parse anything.
1010 switch (*dec->cur) 1022 switch (*dec->cur)
1011 { 1023 {
1012 case '"': ++dec->cur; return decode_str (dec); 1024 case '"': ++dec->cur; return decode_str (dec);
1013 case '[': ++dec->cur; return decode_av (dec); 1025 case '[': ++dec->cur; return decode_av (dec);
1014 case '{': ++dec->cur; return decode_hv (dec); 1026 case '{': ++dec->cur; return decode_hv (dec);
1020 1032
1021 case 't': 1033 case 't':
1022 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1034 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1023 { 1035 {
1024 dec->cur += 4; 1036 dec->cur += 4;
1025 return newSViv (1); 1037 return SvREFCNT_inc (json_true);
1026 } 1038 }
1027 else 1039 else
1028 ERR ("'true' expected"); 1040 ERR ("'true' expected");
1029 1041
1030 break; 1042 break;
1031 1043
1032 case 'f': 1044 case 'f':
1033 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1045 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1034 { 1046 {
1035 dec->cur += 5; 1047 dec->cur += 5;
1036 return newSViv (0); 1048 return SvREFCNT_inc (json_false);
1037 } 1049 }
1038 else 1050 else
1039 ERR ("'false' expected"); 1051 ERR ("'false' expected");
1040 1052
1041 break; 1053 break;
1144 1156
1145BOOT: 1157BOOT:
1146{ 1158{
1147 int i; 1159 int i;
1148 1160
1149 memset (decode_hexdigit, 0xff, 256);
1150
1151 for (i = 0; i < 256; ++i) 1161 for (i = 0; i < 256; ++i)
1152 decode_hexdigit [i] = 1162 decode_hexdigit [i] =
1153 i >= '0' && i <= '9' ? i - '0' 1163 i >= '0' && i <= '9' ? i - '0'
1154 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1164 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1155 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1165 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1156 : -1; 1166 : -1;
1157 1167
1158 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);
1159} 1172}
1160 1173
1161PROTOTYPES: DISABLE 1174PROTOTYPES: DISABLE
1162 1175
1163SV *new (char *dummy) 1176SV *new (char *dummy)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines