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.26 by root, Fri Apr 6 21:17:09 2007 UTC vs.
Revision 1.30 by root, Wed May 9 16:10:37 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
109 if (enc->cur + len >= enc->end) 109 if (enc->cur + len >= enc->end)
110 { 110 {
111 STRLEN cur = enc->cur - SvPVX (enc->sv); 111 STRLEN cur = enc->cur - SvPVX (enc->sv);
112 SvGROW (enc->sv, cur + len + 1); 112 SvGROW (enc->sv, cur + len + 1);
113 enc->cur = SvPVX (enc->sv) + cur; 113 enc->cur = SvPVX (enc->sv) + cur;
114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); 114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
115 } 115 }
116} 116}
117 117
118static void 118static void
119encode_ch (enc_t *enc, char ch) 119encode_ch (enc_t *enc, char ch)
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
525 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
526 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
527
520 if (!(flags & (F_ASCII | F_UTF8))) 528 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
521 SvUTF8_on (enc.sv); 529 SvUTF8_on (enc.sv);
522
523 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
524 530
525 if (enc.flags & F_SHRINK) 531 if (enc.flags & F_SHRINK)
526 shrink (enc.sv); 532 shrink (enc.sv);
527 533
528 return enc.sv; 534 return enc.sv;
600 SV *sv = 0; 606 SV *sv = 0;
601 int utf8 = 0; 607 int utf8 = 0;
602 608
603 do 609 do
604 { 610 {
605 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 611 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
606 char *cur = buf; 612 char *cur = buf;
607 613
608 do 614 do
609 { 615 {
610 unsigned char ch = *(unsigned char *)dec->cur++; 616 unsigned char ch = *(unsigned char *)dec->cur++;
982decode_json (SV *string, U32 flags) 988decode_json (SV *string, U32 flags)
983{ 989{
984 dec_t dec; 990 dec_t dec;
985 SV *sv; 991 SV *sv;
986 992
993 SvGETMAGIC (string);
987 SvUPGRADE (string, SVt_PV); 994 SvUPGRADE (string, SVt_PV);
988 995
989 if (flags & F_UTF8) 996 if (flags & F_UTF8)
990 sv_utf8_downgrade (string, 0); 997 sv_utf8_downgrade (string, 0);
991 else 998 else
1063 RETVAL 1070 RETVAL
1064 1071
1065SV *ascii (SV *self, int enable = 1) 1072SV *ascii (SV *self, int enable = 1)
1066 ALIAS: 1073 ALIAS:
1067 ascii = F_ASCII 1074 ascii = F_ASCII
1075 latin1 = F_LATIN1
1068 utf8 = F_UTF8 1076 utf8 = F_UTF8
1069 indent = F_INDENT 1077 indent = F_INDENT
1070 canonical = F_CANONICAL 1078 canonical = F_CANONICAL
1071 space_before = F_SPACE_BEFORE 1079 space_before = F_SPACE_BEFORE
1072 space_after = F_SPACE_AFTER 1080 space_after = F_SPACE_AFTER

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines