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.70 by root, Wed Mar 19 00:44:54 2008 UTC vs.
Revision 1.71 by root, Wed Mar 19 03:17:38 2008 UTC

50 50
51#define SB do { 51#define SB do {
52#define SE } while (0) 52#define SE } while (0)
53 53
54#if __GNUC__ >= 3 54#if __GNUC__ >= 3
55# define expect(expr,value) __builtin_expect ((expr),(value)) 55# define expect(expr,value) __builtin_expect ((expr), (value))
56# define inline inline 56# define INLINE static inline
57#else 57#else
58# define expect(expr,value) (expr) 58# define expect(expr,value) (expr)
59# define inline static 59# define INLINE static
60#endif 60#endif
61 61
62#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
63#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
64 64
80} JSON; 80} JSON;
81 81
82///////////////////////////////////////////////////////////////////////////// 82/////////////////////////////////////////////////////////////////////////////
83// utility functions 83// utility functions
84 84
85inline void 85INLINE void
86shrink (SV *sv) 86shrink (SV *sv)
87{ 87{
88 sv_utf8_downgrade (sv, 1); 88 sv_utf8_downgrade (sv, 1);
89 if (SvLEN (sv) > SvCUR (sv) + 1) 89 if (SvLEN (sv) > SvCUR (sv) + 1)
90 { 90 {
99// decode an utf-8 character and return it, or (UV)-1 in 99// decode an utf-8 character and return it, or (UV)-1 in
100// case of an error. 100// case of an error.
101// we special-case "safe" characters from U+80 .. U+7FF, 101// we special-case "safe" characters from U+80 .. U+7FF,
102// but use the very good perl function to parse anything else. 102// but use the very good perl function to parse anything else.
103// note that we never call this function for a ascii codepoints 103// note that we never call this function for a ascii codepoints
104inline UV 104INLINE UV
105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
106{ 106{
107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
128 char *end; // SvEND (sv) 128 char *end; // SvEND (sv)
129 SV *sv; // result scalar 129 SV *sv; // result scalar
130 JSON json; 130 JSON json;
131 U32 indent; // indentation level 131 U32 indent; // indentation level
132 U32 maxdepth; // max. indentation/recursion level 132 U32 maxdepth; // max. indentation/recursion level
133 UV limit; // escape character values >= this value when encoding
133} enc_t; 134} enc_t;
134 135
135inline void 136INLINE void
136need (enc_t *enc, STRLEN len) 137need (enc_t *enc, STRLEN len)
137{ 138{
138 if (expect_false (enc->cur + len >= enc->end)) 139 if (expect_false (enc->cur + len >= enc->end))
139 { 140 {
140 STRLEN cur = enc->cur - SvPVX (enc->sv); 141 STRLEN cur = enc->cur - SvPVX (enc->sv);
142 enc->cur = SvPVX (enc->sv) + cur; 143 enc->cur = SvPVX (enc->sv) + cur;
143 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 144 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
144 } 145 }
145} 146}
146 147
147inline void 148INLINE void
148encode_ch (enc_t *enc, char ch) 149encode_ch (enc_t *enc, char ch)
149{ 150{
150 need (enc, 1); 151 need (enc, 1);
151 *enc->cur++ = ch; 152 *enc->cur++ = ch;
152} 153}
206 { 207 {
207 uch = ch; 208 uch = ch;
208 clen = 1; 209 clen = 1;
209 } 210 }
210 211
211 if (uch > 0x10FFFFUL) 212 if (uch < 0x20 || uch >= enc->limit)
212 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
213
214 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
215 { 213 {
216 if (uch > 0xFFFFUL) 214 if (uch > 0xFFFFUL)
217 { 215 {
216 if (uch > 0x10FFFFUL)
217 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
218
218 need (enc, len += 11); 219 need (enc, len += 11);
219 sprintf (enc->cur, "\\u%04x\\u%04x", 220 sprintf (enc->cur, "\\u%04x\\u%04x",
220 (int)((uch - 0x10000) / 0x400 + 0xD800), 221 (int)((uch - 0x10000) / 0x400 + 0xD800),
221 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 222 (int)((uch - 0x10000) % 0x400 + 0xDC00));
222 enc->cur += 12; 223 enc->cur += 12;
261 262
262 --len; 263 --len;
263 } 264 }
264} 265}
265 266
266inline void 267INLINE void
267encode_indent (enc_t *enc) 268encode_indent (enc_t *enc)
268{ 269{
269 if (enc->json.flags & F_INDENT) 270 if (enc->json.flags & F_INDENT)
270 { 271 {
271 int spaces = enc->indent * INDENT_STEP; 272 int spaces = enc->indent * INDENT_STEP;
274 memset (enc->cur, ' ', spaces); 275 memset (enc->cur, ' ', spaces);
275 enc->cur += spaces; 276 enc->cur += spaces;
276 } 277 }
277} 278}
278 279
279inline void 280INLINE void
280encode_space (enc_t *enc) 281encode_space (enc_t *enc)
281{ 282{
282 need (enc, 1); 283 need (enc, 1);
283 encode_ch (enc, ' '); 284 encode_ch (enc, ' ');
284} 285}
285 286
286inline void 287INLINE void
287encode_nl (enc_t *enc) 288encode_nl (enc_t *enc)
288{ 289{
289 if (enc->json.flags & F_INDENT) 290 if (enc->json.flags & F_INDENT)
290 { 291 {
291 need (enc, 1); 292 need (enc, 1);
292 encode_ch (enc, '\n'); 293 encode_ch (enc, '\n');
293 } 294 }
294} 295}
295 296
296inline void 297INLINE void
297encode_comma (enc_t *enc) 298encode_comma (enc_t *enc)
298{ 299{
299 encode_ch (enc, ','); 300 encode_ch (enc, ',');
300 301
301 if (enc->json.flags & F_INDENT) 302 if (enc->json.flags & F_INDENT)
680 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 681 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
681 enc.cur = SvPVX (enc.sv); 682 enc.cur = SvPVX (enc.sv);
682 enc.end = SvEND (enc.sv); 683 enc.end = SvEND (enc.sv);
683 enc.indent = 0; 684 enc.indent = 0;
684 enc.maxdepth = DEC_DEPTH (enc.json.flags); 685 enc.maxdepth = DEC_DEPTH (enc.json.flags);
686 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
687 : enc.json.flags & F_LATIN1 ? 0x000100UL
688 : 0x10FFFFUL;
685 689
686 SvPOK_only (enc.sv); 690 SvPOK_only (enc.sv);
687 encode_sv (&enc, scalar); 691 encode_sv (&enc, scalar);
688 692
689 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 693 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
710 JSON json; 714 JSON json;
711 U32 depth; // recursion depth 715 U32 depth; // recursion depth
712 U32 maxdepth; // recursion depth limit 716 U32 maxdepth; // recursion depth limit
713} dec_t; 717} dec_t;
714 718
715inline void 719INLINE void
716decode_comment (dec_t *dec) 720decode_comment (dec_t *dec)
717{ 721{
718 // only '#'-style comments allowed a.t.m. 722 // only '#'-style comments allowed a.t.m.
719 723
720 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 724 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
721 ++dec->cur; 725 ++dec->cur;
722} 726}
723 727
724inline void 728INLINE void
725decode_ws (dec_t *dec) 729decode_ws (dec_t *dec)
726{ 730{
727 for (;;) 731 for (;;)
728 { 732 {
729 char ch = *dec->cur; 733 char ch = *dec->cur;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines