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.29 by root, Wed May 9 15:34:04 2007 UTC vs.
Revision 1.34 by root, Wed May 23 22:07:13 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
180 } 181 }
181 182
182 if (uch > 0x10FFFFUL) 183 if (uch > 0x10FFFFUL)
183 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);
184 185
185 if (uch < 0x80 || enc->flags & F_ASCII) 186 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
186 { 187 {
187 if (uch > 0xFFFFUL) 188 if (uch > 0xFFFFUL)
188 { 189 {
189 need (enc, len += 11); 190 need (enc, len += 11);
190 sprintf (enc->cur, "\\u%04x\\u%04x", 191 sprintf (enc->cur, "\\u%04x\\u%04x",
204 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 205 *enc->cur++ = hexdigit [(uch >> 0) & 15];
205 } 206 }
206 207
207 str += clen; 208 str += clen;
208 } 209 }
210 else if (enc->flags & F_LATIN1)
211 {
212 *enc->cur++ = uch;
213 str += clen;
214 }
209 else if (is_utf8) 215 else if (is_utf8)
210 { 216 {
211 need (enc, len += clen); 217 need (enc, len += clen);
212 do 218 do
213 { 219 {
366 // actually, this is mostly due to the stupid so-called 372 // actually, this is mostly due to the stupid so-called
367 // security workaround added somewhere in 5.8.x. 373 // security workaround added somewhere in 5.8.x.
368 // that randomises hash orderings 374 // that randomises hash orderings
369 if (enc->flags & F_CANONICAL) 375 if (enc->flags & F_CANONICAL)
370 { 376 {
371 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
372 int fast = 1; 377 int fast = 1;
378 HE *he;
379#if defined(__BORLANDC__) || defined(_MSC_VER)
380 HE **hes = _alloca (count * sizeof (HE));
381#else
382 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
383#endif
373 384
374 i = 0; 385 i = 0;
375 while ((he = hv_iternext (hv))) 386 while ((he = hv_iternext (hv)))
376 { 387 {
377 hes [i++] = he; 388 hes [i++] = he;
514 enc.maxdepth = DEC_DEPTH (flags); 525 enc.maxdepth = DEC_DEPTH (flags);
515 526
516 SvPOK_only (enc.sv); 527 SvPOK_only (enc.sv);
517 encode_sv (&enc, scalar); 528 encode_sv (&enc, scalar);
518 529
519 if (!(flags & (F_ASCII | F_UTF8)))
520 SvUTF8_on (enc.sv);
521
522 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 530 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
523 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 531 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
532
533 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
534 SvUTF8_on (enc.sv);
524 535
525 if (enc.flags & F_SHRINK) 536 if (enc.flags & F_SHRINK)
526 shrink (enc.sv); 537 shrink (enc.sv);
527 538
528 return enc.sv; 539 return enc.sv;
977fail: 988fail:
978 return 0; 989 return 0;
979} 990}
980 991
981static SV * 992static SV *
982decode_json (SV *string, U32 flags) 993decode_json (SV *string, U32 flags, UV *offset_return)
983{ 994{
984 dec_t dec; 995 dec_t dec;
996 UV offset;
985 SV *sv; 997 SV *sv;
986 998
987 SvGETMAGIC (string); 999 SvGETMAGIC (string);
988 SvUPGRADE (string, SVt_PV); 1000 SvUPGRADE (string, SVt_PV);
989 1001
999 dec.end = SvEND (string); 1011 dec.end = SvEND (string);
1000 dec.err = 0; 1012 dec.err = 0;
1001 dec.depth = 0; 1013 dec.depth = 0;
1002 dec.maxdepth = DEC_DEPTH (dec.flags); 1014 dec.maxdepth = DEC_DEPTH (dec.flags);
1003 1015
1004 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1016 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1005 sv = decode_sv (&dec); 1017 sv = decode_sv (&dec);
1006 1018
1019 if (!(offset_return || !sv))
1020 {
1021 // check for trailing garbage
1022 decode_ws (&dec);
1023
1024 if (*dec.cur)
1025 {
1026 dec.err = "garbage after JSON object";
1027 SvREFCNT_dec (sv);
1028 sv = 0;
1029 }
1030 }
1031
1032 if (offset_return || !sv)
1033 {
1034 offset = dec.flags & F_UTF8
1035 ? dec.cur - SvPVX (string)
1036 : utf8_distance (dec.cur, SvPVX (string));
1037
1038 if (offset_return)
1039 *offset_return = offset;
1040 }
1041
1007 if (!sv) 1042 if (!sv)
1008 { 1043 {
1009 IV offset = dec.flags & F_UTF8
1010 ? dec.cur - SvPVX (string)
1011 : utf8_distance (dec.cur, SvPVX (string));
1012 SV *uni = sv_newmortal (); 1044 SV *uni = sv_newmortal ();
1013 1045
1014 // horrible hack to silence warning inside pv_uni_display 1046 // horrible hack to silence warning inside pv_uni_display
1015 COP cop = *PL_curcop; 1047 COP cop = *PL_curcop;
1016 cop.cop_warnings = pWARN_NONE; 1048 cop.cop_warnings = pWARN_NONE;
1064 RETVAL 1096 RETVAL
1065 1097
1066SV *ascii (SV *self, int enable = 1) 1098SV *ascii (SV *self, int enable = 1)
1067 ALIAS: 1099 ALIAS:
1068 ascii = F_ASCII 1100 ascii = F_ASCII
1101 latin1 = F_LATIN1
1069 utf8 = F_UTF8 1102 utf8 = F_UTF8
1070 indent = F_INDENT 1103 indent = F_INDENT
1071 canonical = F_CANONICAL 1104 canonical = F_CANONICAL
1072 space_before = F_SPACE_BEFORE 1105 space_before = F_SPACE_BEFORE
1073 space_after = F_SPACE_AFTER 1106 space_after = F_SPACE_AFTER
1109 PPCODE: 1142 PPCODE:
1110 XPUSHs (encode_json (scalar, *SvJSON (self))); 1143 XPUSHs (encode_json (scalar, *SvJSON (self)));
1111 1144
1112void decode (SV *self, SV *jsonstr) 1145void decode (SV *self, SV *jsonstr)
1113 PPCODE: 1146 PPCODE:
1114 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1147 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1148
1149void decode_prefix (SV *self, SV *jsonstr)
1150 PPCODE:
1151{
1152 UV offset;
1153 EXTEND (SP, 2);
1154 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1155 PUSHs (sv_2mortal (newSVuv (offset)));
1156}
1115 1157
1116PROTOTYPES: ENABLE 1158PROTOTYPES: ENABLE
1117 1159
1118void to_json (SV *scalar) 1160void to_json (SV *scalar)
1119 ALIAS: 1161 ALIAS:
1123 1165
1124void from_json (SV *jsonstr) 1166void from_json (SV *jsonstr)
1125 ALIAS: 1167 ALIAS:
1126 jsonToObj = 0 1168 jsonToObj = 0
1127 PPCODE: 1169 PPCODE:
1128 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1170 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1129 1171

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines