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.32 by root, Wed May 9 16:41:12 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 512 // 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 41
181 } 181 }
182 182
183 if (uch > 0x10FFFFUL) 183 if (uch > 0x10FFFFUL)
184 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 184 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
185 185
186 if (uch < 0x80 || enc->flags & F_ASCII) 186 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
187 { 187 {
188 if (uch > 0xFFFFUL) 188 if (uch > 0xFFFFUL)
189 { 189 {
190 need (enc, len += 11); 190 need (enc, len += 11);
191 sprintf (enc->cur, "\\u%04x\\u%04x", 191 sprintf (enc->cur, "\\u%04x\\u%04x",
205 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 205 *enc->cur++ = hexdigit [(uch >> 0) & 15];
206 } 206 }
207 207
208 str += clen; 208 str += clen;
209 } 209 }
210 else if (enc->flags & F_LATIN1)
211 {
212 *enc->cur++ = uch;
213 str += clen;
214 }
210 else if (is_utf8) 215 else if (is_utf8)
211 { 216 {
212 need (enc, len += clen); 217 need (enc, len += clen);
213 do 218 do
214 { 219 {
216 } 221 }
217 while (--clen); 222 while (--clen);
218 } 223 }
219 else 224 else
220 { 225 {
221 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 226 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
222 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 227 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
223 ++str; 228 ++str;
224 } 229 }
225 } 230 }
226 } 231 }
515 enc.maxdepth = DEC_DEPTH (flags); 520 enc.maxdepth = DEC_DEPTH (flags);
516 521
517 SvPOK_only (enc.sv); 522 SvPOK_only (enc.sv);
518 encode_sv (&enc, scalar); 523 encode_sv (&enc, scalar);
519 524
520 if (!(flags & (F_ASCII | F_UTF8)))
521 SvUTF8_on (enc.sv);
522
523 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 525 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
524 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 526 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
527
528 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
529 SvUTF8_on (enc.sv);
525 530
526 if (enc.flags & F_SHRINK) 531 if (enc.flags & F_SHRINK)
527 shrink (enc.sv); 532 shrink (enc.sv);
528 533
529 return enc.sv; 534 return enc.sv;
601 SV *sv = 0; 606 SV *sv = 0;
602 int utf8 = 0; 607 int utf8 = 0;
603 608
604 do 609 do
605 { 610 {
606 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 611 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
607 char *cur = buf; 612 char *cur = buf;
608 613
609 do 614 do
610 { 615 {
611 unsigned char ch = *(unsigned char *)dec->cur++; 616 unsigned char ch = *(unsigned char *)dec->cur++;
978fail: 983fail:
979 return 0; 984 return 0;
980} 985}
981 986
982static SV * 987static SV *
983decode_json (SV *string, U32 flags) 988decode_json (SV *string, U32 flags, UV *offset_return)
984{ 989{
985 dec_t dec; 990 dec_t dec;
991 UV offset;
986 SV *sv; 992 SV *sv;
987 993
994 SvGETMAGIC (string);
988 SvUPGRADE (string, SVt_PV); 995 SvUPGRADE (string, SVt_PV);
989 996
990 if (flags & F_UTF8) 997 if (flags & F_UTF8)
991 sv_utf8_downgrade (string, 0); 998 sv_utf8_downgrade (string, 0);
992 else 999 else
999 dec.end = SvEND (string); 1006 dec.end = SvEND (string);
1000 dec.err = 0; 1007 dec.err = 0;
1001 dec.depth = 0; 1008 dec.depth = 0;
1002 dec.maxdepth = DEC_DEPTH (dec.flags); 1009 dec.maxdepth = DEC_DEPTH (dec.flags);
1003 1010
1004 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1011 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1005 sv = decode_sv (&dec); 1012 sv = decode_sv (&dec);
1006 1013
1014 if (!(offset_return || !sv))
1015 {
1016 // check for trailing garbage
1017 decode_ws (&dec);
1018
1019 if (*dec.cur)
1020 {
1021 dec.err = "garbage after JSON object";
1022 SvREFCNT_dec (sv);
1023 sv = 0;
1024 }
1025 }
1026
1027 if (offset_return || !sv)
1028 {
1029 offset = dec.flags & F_UTF8
1030 ? dec.cur - SvPVX (string)
1031 : utf8_distance (dec.cur, SvPVX (string));
1032
1033 if (offset_return)
1034 *offset_return = offset;
1035 }
1036
1007 if (!sv) 1037 if (!sv)
1008 { 1038 {
1009 IV offset = dec.flags & F_UTF8
1010 ? dec.cur - SvPVX (string)
1011 : utf8_distance (dec.cur, SvPVX (string));
1012 SV *uni = sv_newmortal (); 1039 SV *uni = sv_newmortal ();
1013 1040
1014 // horrible hack to silence warning inside pv_uni_display 1041 // horrible hack to silence warning inside pv_uni_display
1015 COP cop = *PL_curcop; 1042 COP cop = *PL_curcop;
1016 cop.cop_warnings = pWARN_NONE; 1043 cop.cop_warnings = pWARN_NONE;
1064 RETVAL 1091 RETVAL
1065 1092
1066SV *ascii (SV *self, int enable = 1) 1093SV *ascii (SV *self, int enable = 1)
1067 ALIAS: 1094 ALIAS:
1068 ascii = F_ASCII 1095 ascii = F_ASCII
1096 latin1 = F_LATIN1
1069 utf8 = F_UTF8 1097 utf8 = F_UTF8
1070 indent = F_INDENT 1098 indent = F_INDENT
1071 canonical = F_CANONICAL 1099 canonical = F_CANONICAL
1072 space_before = F_SPACE_BEFORE 1100 space_before = F_SPACE_BEFORE
1073 space_after = F_SPACE_AFTER 1101 space_after = F_SPACE_AFTER
1109 PPCODE: 1137 PPCODE:
1110 XPUSHs (encode_json (scalar, *SvJSON (self))); 1138 XPUSHs (encode_json (scalar, *SvJSON (self)));
1111 1139
1112void decode (SV *self, SV *jsonstr) 1140void decode (SV *self, SV *jsonstr)
1113 PPCODE: 1141 PPCODE:
1114 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1142 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1143
1144void decode_prefix (SV *self, SV *jsonstr)
1145 PPCODE:
1146{
1147 UV offset;
1148 EXTEND (SP, 2);
1149 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1150 PUSHs (sv_2mortal (newSVuv (offset)));
1151}
1115 1152
1116PROTOTYPES: ENABLE 1153PROTOTYPES: ENABLE
1117 1154
1118void to_json (SV *scalar) 1155void to_json (SV *scalar)
1119 ALIAS: 1156 ALIAS:
1123 1160
1124void from_json (SV *jsonstr) 1161void from_json (SV *jsonstr)
1125 ALIAS: 1162 ALIAS:
1126 jsonToObj = 0 1163 jsonToObj = 0
1127 PPCODE: 1164 PPCODE:
1128 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1165 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1129 1166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines