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.57 by root, Mon Aug 13 16:05:42 2007 UTC vs.
Revision 1.74 by root, Wed Mar 19 15:17:53 2008 UTC

27#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
28#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
29#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
30#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
31#define F_CONV_BLESSED 0x00000800UL 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
32#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
33#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
34#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
35#define S_MAXSIZE 20 37#define S_MAXSIZE 20
36#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
48 50
49#define SB do { 51#define SB do {
50#define SE } while (0) 52#define SE } while (0)
51 53
52#if __GNUC__ >= 3 54#if __GNUC__ >= 3
53# define expect(expr,value) __builtin_expect ((expr),(value)) 55# define expect(expr,value) __builtin_expect ((expr), (value))
54# define inline inline 56# define INLINE static inline
55#else 57#else
56# define expect(expr,value) (expr) 58# define expect(expr,value) (expr)
57# define inline static 59# define INLINE static
58#endif 60#endif
59 61
60#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
61#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
62 64
65#define IN_RANGE_INC(type,val,beg,end) \
66 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
67 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
68
63#ifdef USE_ITHREADS 69#ifdef USE_ITHREADS
64# define JSON_SLOW 1 70# define JSON_SLOW 1
71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
65#else 72#else
66# define JSON_SLOW 1 73# define JSON_SLOW 0
74# define JSON_STASH json_stash
67#endif 75#endif
68 76
69static HV *json_stash, *json_boolean_stash; // JSON::XS:: 77static HV *json_stash, *json_boolean_stash; // JSON::XS::
70static SV *json_true, *json_false; 78static SV *json_true, *json_false;
71 79
76} JSON; 84} JSON;
77 85
78///////////////////////////////////////////////////////////////////////////// 86/////////////////////////////////////////////////////////////////////////////
79// utility functions 87// utility functions
80 88
81inline void 89INLINE void
82shrink (SV *sv) 90shrink (SV *sv)
83{ 91{
84 sv_utf8_downgrade (sv, 1); 92 sv_utf8_downgrade (sv, 1);
85 if (SvLEN (sv) > SvCUR (sv) + 1) 93 if (SvLEN (sv) > SvCUR (sv) + 1)
86 { 94 {
95// decode an utf-8 character and return it, or (UV)-1 in 103// decode an utf-8 character and return it, or (UV)-1 in
96// case of an error. 104// case of an error.
97// we special-case "safe" characters from U+80 .. U+7FF, 105// we special-case "safe" characters from U+80 .. U+7FF,
98// but use the very good perl function to parse anything else. 106// but use the very good perl function to parse anything else.
99// note that we never call this function for a ascii codepoints 107// note that we never call this function for a ascii codepoints
100inline UV 108INLINE UV
101decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
102{ 110{
103 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 111 if (expect_true (len >= 2
104 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 112 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
105 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 113 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
106 { 114 {
107 *clen = 2; 115 *clen = 2;
108 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 116 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
109 } 117 }
110 else 118 else
111 { 119 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
112 *clen = (STRLEN)-1; 120}
113 return (UV)-1; 121
114 } 122// likewise for encoding, also never called for ascii codepoints
123// this function takes advantage of this fact, although current gccs
124// seem to optimise the check for >= 0x80 away anyways
125INLINE unsigned char *
126encode_utf8 (unsigned char *s, UV ch)
127{
128 if (expect_false (ch < 0x000080))
129 *s++ = ch;
130 else if (expect_true (ch < 0x000800))
131 *s++ = 0xc0 | ( ch >> 6),
132 *s++ = 0x80 | ( ch & 0x3f);
133 else if ( ch < 0x010000)
134 *s++ = 0xe0 | ( ch >> 12),
135 *s++ = 0x80 | ((ch >> 6) & 0x3f),
136 *s++ = 0x80 | ( ch & 0x3f);
137 else if ( ch < 0x110000)
138 *s++ = 0xf0 | ( ch >> 18),
139 *s++ = 0x80 | ((ch >> 12) & 0x3f),
140 *s++ = 0x80 | ((ch >> 6) & 0x3f),
141 *s++ = 0x80 | ( ch & 0x3f);
142
143 return s;
115} 144}
116 145
117///////////////////////////////////////////////////////////////////////////// 146/////////////////////////////////////////////////////////////////////////////
118// encoder 147// encoder
119 148
124 char *end; // SvEND (sv) 153 char *end; // SvEND (sv)
125 SV *sv; // result scalar 154 SV *sv; // result scalar
126 JSON json; 155 JSON json;
127 U32 indent; // indentation level 156 U32 indent; // indentation level
128 U32 maxdepth; // max. indentation/recursion level 157 U32 maxdepth; // max. indentation/recursion level
158 UV limit; // escape character values >= this value when encoding
129} enc_t; 159} enc_t;
130 160
131inline void 161INLINE void
132need (enc_t *enc, STRLEN len) 162need (enc_t *enc, STRLEN len)
133{ 163{
134 if (expect_false (enc->cur + len >= enc->end)) 164 if (expect_false (enc->cur + len >= enc->end))
135 { 165 {
136 STRLEN cur = enc->cur - SvPVX (enc->sv); 166 STRLEN cur = enc->cur - SvPVX (enc->sv);
138 enc->cur = SvPVX (enc->sv) + cur; 168 enc->cur = SvPVX (enc->sv) + cur;
139 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 169 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
140 } 170 }
141} 171}
142 172
143inline void 173INLINE void
144encode_ch (enc_t *enc, char ch) 174encode_ch (enc_t *enc, char ch)
145{ 175{
146 need (enc, 1); 176 need (enc, 1);
147 *enc->cur++ = ch; 177 *enc->cur++ = ch;
148} 178}
202 { 232 {
203 uch = ch; 233 uch = ch;
204 clen = 1; 234 clen = 1;
205 } 235 }
206 236
207 if (uch > 0x10FFFFUL) 237 if (uch < 0x80/*0x20*/ || uch >= enc->limit)
208 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
209
210 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
211 { 238 {
212 if (uch > 0xFFFFUL) 239 if (uch >= 0x10000UL)
213 { 240 {
241 if (uch >= 0x110000UL)
242 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
243
214 need (enc, len += 11); 244 need (enc, len += 11);
215 sprintf (enc->cur, "\\u%04x\\u%04x", 245 sprintf (enc->cur, "\\u%04x\\u%04x",
216 (int)((uch - 0x10000) / 0x400 + 0xD800), 246 (int)((uch - 0x10000) / 0x400 + 0xD800),
217 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 247 (int)((uch - 0x10000) % 0x400 + 0xDC00));
218 enc->cur += 12; 248 enc->cur += 12;
246 while (--clen); 276 while (--clen);
247 } 277 }
248 else 278 else
249 { 279 {
250 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 280 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
251 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 281 enc->cur = encode_utf8 (enc->cur, uch);
252 ++str; 282 ++str;
253 } 283 }
254 } 284 }
255 } 285 }
256 } 286 }
257 287
258 --len; 288 --len;
259 } 289 }
260} 290}
261 291
262inline void 292INLINE void
263encode_indent (enc_t *enc) 293encode_indent (enc_t *enc)
264{ 294{
265 if (enc->json.flags & F_INDENT) 295 if (enc->json.flags & F_INDENT)
266 { 296 {
267 int spaces = enc->indent * INDENT_STEP; 297 int spaces = enc->indent * INDENT_STEP;
270 memset (enc->cur, ' ', spaces); 300 memset (enc->cur, ' ', spaces);
271 enc->cur += spaces; 301 enc->cur += spaces;
272 } 302 }
273} 303}
274 304
275inline void 305INLINE void
276encode_space (enc_t *enc) 306encode_space (enc_t *enc)
277{ 307{
278 need (enc, 1); 308 need (enc, 1);
279 encode_ch (enc, ' '); 309 encode_ch (enc, ' ');
280} 310}
281 311
282inline void 312INLINE void
283encode_nl (enc_t *enc) 313encode_nl (enc_t *enc)
284{ 314{
285 if (enc->json.flags & F_INDENT) 315 if (enc->json.flags & F_INDENT)
286 { 316 {
287 need (enc, 1); 317 need (enc, 1);
288 encode_ch (enc, '\n'); 318 encode_ch (enc, '\n');
289 } 319 }
290} 320}
291 321
292inline void 322INLINE void
293encode_comma (enc_t *enc) 323encode_comma (enc_t *enc)
294{ 324{
295 encode_ch (enc, ','); 325 encode_ch (enc, ',');
296 326
297 if (enc->json.flags & F_INDENT) 327 if (enc->json.flags & F_INDENT)
308 int i, len = av_len (av); 338 int i, len = av_len (av);
309 339
310 if (enc->indent >= enc->maxdepth) 340 if (enc->indent >= enc->maxdepth)
311 croak ("data structure too deep (hit recursion limit)"); 341 croak ("data structure too deep (hit recursion limit)");
312 342
313 encode_ch (enc, '['); encode_nl (enc); 343 encode_ch (enc, '[');
314 ++enc->indent; 344
345 if (len >= 0)
346 {
347 encode_nl (enc); ++enc->indent;
315 348
316 for (i = 0; i <= len; ++i) 349 for (i = 0; i <= len; ++i)
317 { 350 {
318 SV **svp = av_fetch (av, i, 0); 351 SV **svp = av_fetch (av, i, 0);
319 352
320 encode_indent (enc); 353 encode_indent (enc);
321 354
322 if (svp) 355 if (svp)
323 encode_sv (enc, *svp); 356 encode_sv (enc, *svp);
324 else 357 else
325 encode_str (enc, "null", 4, 0); 358 encode_str (enc, "null", 4, 0);
326 359
327 if (i < len) 360 if (i < len)
328 encode_comma (enc); 361 encode_comma (enc);
329 } 362 }
330 363
364 encode_nl (enc); --enc->indent; encode_indent (enc);
365 }
366
331 encode_nl (enc); 367 encode_ch (enc, ']');
332
333 --enc->indent;
334 encode_indent (enc); encode_ch (enc, ']');
335} 368}
336 369
337static void 370static void
338encode_he (enc_t *enc, HE *he) 371encode_hk (enc_t *enc, HE *he)
339{ 372{
340 encode_ch (enc, '"'); 373 encode_ch (enc, '"');
341 374
342 if (HeKLEN (he) == HEf_SVKEY) 375 if (HeKLEN (he) == HEf_SVKEY)
343 { 376 {
356 encode_ch (enc, '"'); 389 encode_ch (enc, '"');
357 390
358 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 391 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
359 encode_ch (enc, ':'); 392 encode_ch (enc, ':');
360 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 393 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
361 encode_sv (enc, HeVAL (he));
362} 394}
363 395
364// compare hash entries, used when all keys are bytestrings 396// compare hash entries, used when all keys are bytestrings
365static int 397static int
366he_cmp_fast (const void *a_, const void *b_) 398he_cmp_fast (const void *a_, const void *b_)
371 HE *b = *(HE **)b_; 403 HE *b = *(HE **)b_;
372 404
373 STRLEN la = HeKLEN (a); 405 STRLEN la = HeKLEN (a);
374 STRLEN lb = HeKLEN (b); 406 STRLEN lb = HeKLEN (b);
375 407
376 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 408 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
377 cmp = la - lb; 409 cmp = lb - la;
378 410
379 return cmp; 411 return cmp;
380} 412}
381 413
382// compare hash entries, used when some keys are sv's or utf-x 414// compare hash entries, used when some keys are sv's or utf-x
383static int 415static int
384he_cmp_slow (const void *a, const void *b) 416he_cmp_slow (const void *a, const void *b)
385{ 417{
386 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 418 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
387} 419}
388 420
389static void 421static void
390encode_hv (enc_t *enc, HV *hv) 422encode_hv (enc_t *enc, HV *hv)
391{ 423{
424 HE *he;
392 int count, i; 425 int count;
393 426
394 if (enc->indent >= enc->maxdepth) 427 if (enc->indent >= enc->maxdepth)
395 croak ("data structure too deep (hit recursion limit)"); 428 croak ("data structure too deep (hit recursion limit)");
396 429
397 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 430 encode_ch (enc, '{');
398 431
399 if ((count = hv_iterinit (hv)))
400 {
401 // for canonical output we have to sort by keys first 432 // for canonical output we have to sort by keys first
402 // actually, this is mostly due to the stupid so-called 433 // actually, this is mostly due to the stupid so-called
403 // security workaround added somewhere in 5.8.x. 434 // security workaround added somewhere in 5.8.x.
404 // that randomises hash orderings 435 // that randomises hash orderings
405 if (enc->json.flags & F_CANONICAL) 436 if (enc->json.flags & F_CANONICAL)
437 {
438 int count = hv_iterinit (hv);
439
440 if (SvMAGICAL (hv))
406 { 441 {
442 // need to count by iterating. could improve by dynamically building the vector below
443 // but I don't care for the speed of this special case.
444 // note also that we will run into undefined behaviour when the two iterations
445 // do not result in the same count, something I might care for in some later release.
446
447 count = 0;
448 while (hv_iternext (hv))
449 ++count;
450
451 hv_iterinit (hv);
452 }
453
454 if (count)
455 {
407 int fast = 1; 456 int i, fast = 1;
408 HE *he;
409#if defined(__BORLANDC__) || defined(_MSC_VER) 457#if defined(__BORLANDC__) || defined(_MSC_VER)
410 HE **hes = _alloca (count * sizeof (HE)); 458 HE **hes = _alloca (count * sizeof (HE));
411#else 459#else
412 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 460 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
413#endif 461#endif
440 488
441 FREETMPS; 489 FREETMPS;
442 LEAVE; 490 LEAVE;
443 } 491 }
444 492
445 for (i = 0; i < count; ++i) 493 encode_nl (enc); ++enc->indent;
494
495 while (count--)
446 { 496 {
447 encode_indent (enc); 497 encode_indent (enc);
498 he = hes [count];
448 encode_he (enc, hes [i]); 499 encode_hk (enc, he);
500 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
449 501
450 if (i < count - 1) 502 if (count)
451 encode_comma (enc); 503 encode_comma (enc);
452 } 504 }
453 505
454 encode_nl (enc); 506 encode_nl (enc); --enc->indent; encode_indent (enc);
455 } 507 }
508 }
456 else 509 else
510 {
511 if (hv_iterinit (hv) || SvMAGICAL (hv))
512 if ((he = hv_iternext (hv)))
457 { 513 {
458 HE *he = hv_iternext (hv); 514 encode_nl (enc); ++enc->indent;
459 515
460 for (;;) 516 for (;;)
461 { 517 {
462 encode_indent (enc); 518 encode_indent (enc);
463 encode_he (enc, he); 519 encode_hk (enc, he);
520 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
464 521
465 if (!(he = hv_iternext (hv))) 522 if (!(he = hv_iternext (hv)))
466 break; 523 break;
467 524
468 encode_comma (enc); 525 encode_comma (enc);
469 } 526 }
470 527
471 encode_nl (enc); 528 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 529 }
473 } 530 }
474 531
475 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 532 encode_ch (enc, '}');
476} 533}
477 534
478// encode objects, arrays and special \0=false and \1=true values. 535// encode objects, arrays and special \0=false and \1=true values.
479static void 536static void
480encode_rv (enc_t *enc, SV *sv) 537encode_rv (enc_t *enc, SV *sv)
510 // we re-bless the reference to get overload and other niceties right 567 // we re-bless the reference to get overload and other niceties right
511 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 568 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
512 569
513 if (to_json) 570 if (to_json)
514 { 571 {
515 int count;
516 dSP; 572 dSP;
517 573
518 ENTER; SAVETMPS; PUSHMARK (SP); 574 ENTER; SAVETMPS; PUSHMARK (SP);
519 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 575 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
520 576
589 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
590 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
591 } 647 }
592 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
593 { 649 {
594 // we assume we can always read an IV as a UV 650 // we assume we can always read an IV as a UV and vice versa
595 if (SvUV (sv) & ~(UV)0x7fff) 651 // we assume two's complement
652 // we assume no aliasing issues in the union
653 if (SvIsUV (sv) ? SvUVX (sv) > 59000
654 : SvIVX (sv) > 59000 || SvIVX (sv) < -59000)
596 { 655 {
597 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
598 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
599 enc->cur += 658 enc->cur +=
600 SvIsUV(sv) 659 SvIsUV(sv)
601 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
602 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
603 } 662 }
604 else 663 else
605 { 664 {
606 // optimise the "small number case" 665 // optimise the "small number case"
607 // code will likely be branchless and use only a single multiplication 666 // code will likely be branchless and use only a single multiplication
667 // works for numbers up to 59074
608 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
609 U32 u; 669 U32 u;
610 char digit, nz = 0; 670 char digit, nz = 0;
611 671
612 need (enc, 6); 672 need (enc, 6);
613 673
650 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
651 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
652 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
653 enc.indent = 0; 713 enc.indent = 0;
654 enc.maxdepth = DEC_DEPTH (enc.json.flags); 714 enc.maxdepth = DEC_DEPTH (enc.json.flags);
715 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
716 : enc.json.flags & F_LATIN1 ? 0x000100UL
717 : 0x110000UL;
655 718
656 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
657 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
658 721
659 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
680 JSON json; 743 JSON json;
681 U32 depth; // recursion depth 744 U32 depth; // recursion depth
682 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
683} dec_t; 746} dec_t;
684 747
685inline void 748INLINE void
749decode_comment (dec_t *dec)
750{
751 // only '#'-style comments allowed a.t.m.
752
753 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
754 ++dec->cur;
755}
756
757INLINE void
686decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
687{ 759{
688 for (;;) 760 for (;;)
689 { 761 {
690 char ch = *dec->cur; 762 char ch = *dec->cur;
691 763
692 if (ch > 0x20 764 if (ch > 0x20)
765 {
766 if (expect_false (ch == '#'))
767 {
768 if (dec->json.flags & F_RELAXED)
769 decode_comment (dec);
770 else
771 break;
772 }
773 else
774 break;
775 }
693 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 776 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
694 break; 777 break; // parse error, but let higher level handle it, gives better error messages
695 778
696 ++dec->cur; 779 ++dec->cur;
697 } 780 }
698} 781}
699 782
805 888
806 if (hi >= 0x80) 889 if (hi >= 0x80)
807 { 890 {
808 utf8 = 1; 891 utf8 = 1;
809 892
810 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
811 } 894 }
812 else 895 else
813 *cur++ = hi; 896 *cur++ = hi;
814 } 897 }
815 break; 898 break;
817 default: 900 default:
818 --dec_cur; 901 --dec_cur;
819 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
820 } 903 }
821 } 904 }
822 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
823 *cur++ = ch; 906 *cur++ = ch;
824 else if (ch >= 0x80) 907 else if (ch >= 0x80)
825 { 908 {
826 STRLEN clen; 909 STRLEN clen;
827 UV uch; 910 UV uch;
950 1033
951 if (!is_nv) 1034 if (!is_nv)
952 { 1035 {
953 int len = dec->cur - start; 1036 int len = dec->cur - start;
954 1037
955 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1038 // special case the rather common 1..5-digit-int case
956 if (*start == '-') 1039 if (*start == '-')
957 switch (len) 1040 switch (len)
958 { 1041 {
959 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
960 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
961 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1044 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
962 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1045 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1046 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
963 } 1047 }
964 else 1048 else
965 switch (len) 1049 switch (len)
966 { 1050 {
967 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
968 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
969 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1053 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
970 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1054 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1055 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
971 } 1056 }
972 1057
973 { 1058 {
974 UV uv; 1059 UV uv;
975 int numtype = grok_number (start, len, &uv); 1060 int numtype = grok_number (start, len, &uv);
1036 1121
1037 if (*dec->cur != ',') 1122 if (*dec->cur != ',')
1038 ERR (", or ] expected while parsing array"); 1123 ERR (", or ] expected while parsing array");
1039 1124
1040 ++dec->cur; 1125 ++dec->cur;
1126
1127 decode_ws (dec);
1128
1129 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1130 {
1131 ++dec->cur;
1132 break;
1133 }
1041 } 1134 }
1042 1135
1043 DEC_DEC_DEPTH; 1136 DEC_DEC_DEPTH;
1044 return newRV_noinc ((SV *)av); 1137 return newRV_noinc ((SV *)av);
1045 1138
1061 if (*dec->cur == '}') 1154 if (*dec->cur == '}')
1062 ++dec->cur; 1155 ++dec->cur;
1063 else 1156 else
1064 for (;;) 1157 for (;;)
1065 { 1158 {
1066 decode_ws (dec); EXPECT_CH ('"'); 1159 EXPECT_CH ('"');
1067 1160
1068 // heuristic: assume that 1161 // heuristic: assume that
1069 // a) decode_str + hv_store_ent are abysmally slow. 1162 // a) decode_str + hv_store_ent are abysmally slow.
1070 // b) most hash keys are short, simple ascii text. 1163 // b) most hash keys are short, simple ascii text.
1071 // => try to "fast-match" such strings to avoid 1164 // => try to "fast-match" such strings to avoid
1085 if (!key) 1178 if (!key)
1086 goto fail; 1179 goto fail;
1087 1180
1088 decode_ws (dec); EXPECT_CH (':'); 1181 decode_ws (dec); EXPECT_CH (':');
1089 1182
1183 decode_ws (dec);
1090 value = decode_sv (dec); 1184 value = decode_sv (dec);
1091 if (!value) 1185 if (!value)
1092 { 1186 {
1093 SvREFCNT_dec (key); 1187 SvREFCNT_dec (key);
1094 goto fail; 1188 goto fail;
1106 int len = p - key; 1200 int len = p - key;
1107 dec->cur = p + 1; 1201 dec->cur = p + 1;
1108 1202
1109 decode_ws (dec); EXPECT_CH (':'); 1203 decode_ws (dec); EXPECT_CH (':');
1110 1204
1205 decode_ws (dec);
1111 value = decode_sv (dec); 1206 value = decode_sv (dec);
1112 if (!value) 1207 if (!value)
1113 goto fail; 1208 goto fail;
1114 1209
1115 hv_store (hv, key, len, value, 0); 1210 hv_store (hv, key, len, value, 0);
1131 1226
1132 if (*dec->cur != ',') 1227 if (*dec->cur != ',')
1133 ERR (", or } expected while parsing object/hash"); 1228 ERR (", or } expected while parsing object/hash");
1134 1229
1135 ++dec->cur; 1230 ++dec->cur;
1231
1232 decode_ws (dec);
1233
1234 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1235 {
1236 ++dec->cur;
1237 break;
1238 }
1136 } 1239 }
1137 1240
1138 DEC_DEC_DEPTH; 1241 DEC_DEC_DEPTH;
1139 sv = newRV_noinc ((SV *)hv); 1242 sv = newRV_noinc ((SV *)hv);
1140 1243
1205} 1308}
1206 1309
1207static SV * 1310static SV *
1208decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1209{ 1312{
1210 decode_ws (dec);
1211
1212 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1213 // to parse anything. 1314 // to parse everything.
1214 switch (*dec->cur) 1315 switch (*dec->cur)
1215 { 1316 {
1216 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1217 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1218 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1219 1320
1220 case '-': 1321 case '-':
1221 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1222 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1223 return decode_num (dec); 1324 return decode_num (dec);
1224 1325
1225 case 't': 1326 case 't':
1226 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1327 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1227 { 1328 {
1228 dec->cur += 4; 1329 dec->cur += 4;
1330#if JSON_SLOW
1331 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1332#endif
1229 return SvREFCNT_inc (json_true); 1333 return SvREFCNT_inc (json_true);
1230 } 1334 }
1231 else 1335 else
1232 ERR ("'true' expected"); 1336 ERR ("'true' expected");
1233 1337
1235 1339
1236 case 'f': 1340 case 'f':
1237 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1341 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1238 { 1342 {
1239 dec->cur += 5; 1343 dec->cur += 5;
1344#if JSON_SLOW
1345 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1346#endif
1240 return SvREFCNT_inc (json_false); 1347 return SvREFCNT_inc (json_false);
1241 } 1348 }
1242 else 1349 else
1243 ERR ("'false' expected"); 1350 ERR ("'false' expected");
1244 1351
1294 1401
1295 if (dec.json.cb_object || dec.json.cb_sk_object) 1402 if (dec.json.cb_object || dec.json.cb_sk_object)
1296 dec.json.flags |= F_HOOK; 1403 dec.json.flags |= F_HOOK;
1297 1404
1298 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1405 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1406
1407 decode_ws (&dec);
1299 sv = decode_sv (&dec); 1408 sv = decode_sv (&dec);
1300 1409
1301 if (!(offset_return || !sv)) 1410 if (!(offset_return || !sv))
1302 { 1411 {
1303 // check for trailing garbage 1412 // check for trailing garbage
1379 json_boolean_stash = 0; 1488 json_boolean_stash = 0;
1380 1489
1381void new (char *klass) 1490void new (char *klass)
1382 PPCODE: 1491 PPCODE:
1383{ 1492{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1387 SV *pv = NEWSV (0, sizeof (JSON)); 1493 SV *pv = NEWSV (0, sizeof (JSON));
1388 SvPOK_only (pv); 1494 SvPOK_only (pv);
1389 Zero (SvPVX (pv), 1, JSON); 1495 Zero (SvPVX (pv), 1, JSON);
1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1496 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); 1497 XPUSHs (sv_2mortal (sv_bless (
1498 newRV_noinc (pv),
1499 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1500 )));
1392} 1501}
1393 1502
1394void ascii (JSON *self, int enable = 1) 1503void ascii (JSON *self, int enable = 1)
1395 ALIAS: 1504 ALIAS:
1396 ascii = F_ASCII 1505 ascii = F_ASCII
1403 pretty = F_PRETTY 1512 pretty = F_PRETTY
1404 allow_nonref = F_ALLOW_NONREF 1513 allow_nonref = F_ALLOW_NONREF
1405 shrink = F_SHRINK 1514 shrink = F_SHRINK
1406 allow_blessed = F_ALLOW_BLESSED 1515 allow_blessed = F_ALLOW_BLESSED
1407 convert_blessed = F_CONV_BLESSED 1516 convert_blessed = F_CONV_BLESSED
1517 relaxed = F_RELAXED
1408 PPCODE: 1518 PPCODE:
1409{ 1519{
1410 if (enable) 1520 if (enable)
1411 self->flags |= ix; 1521 self->flags |= ix;
1412 else 1522 else
1413 self->flags &= ~ix; 1523 self->flags &= ~ix;
1414 1524
1415 XPUSHs (ST (0)); 1525 XPUSHs (ST (0));
1416} 1526}
1417 1527
1528void get_ascii (JSON *self)
1529 ALIAS:
1530 get_ascii = F_ASCII
1531 get_latin1 = F_LATIN1
1532 get_utf8 = F_UTF8
1533 get_indent = F_INDENT
1534 get_canonical = F_CANONICAL
1535 get_space_before = F_SPACE_BEFORE
1536 get_space_after = F_SPACE_AFTER
1537 get_allow_nonref = F_ALLOW_NONREF
1538 get_shrink = F_SHRINK
1539 get_allow_blessed = F_ALLOW_BLESSED
1540 get_convert_blessed = F_CONV_BLESSED
1541 get_relaxed = F_RELAXED
1542 PPCODE:
1543 XPUSHs (boolSV (self->flags & ix));
1544
1418void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1545void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1419 PPCODE: 1546 PPCODE:
1420{ 1547{
1421 UV log2 = 0; 1548 UV log2 = 0;
1422 1549
1428 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1555 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1429 1556
1430 XPUSHs (ST (0)); 1557 XPUSHs (ST (0));
1431} 1558}
1432 1559
1560U32 get_max_depth (JSON *self)
1561 CODE:
1562 RETVAL = DEC_DEPTH (self->flags);
1563 OUTPUT:
1564 RETVAL
1565
1433void max_size (JSON *self, UV max_size = 0) 1566void max_size (JSON *self, UV max_size = 0)
1434 PPCODE: 1567 PPCODE:
1435{ 1568{
1436 UV log2 = 0; 1569 UV log2 = 0;
1437 1570
1443 1576
1444 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1577 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1445 1578
1446 XPUSHs (ST (0)); 1579 XPUSHs (ST (0));
1447} 1580}
1581
1582int get_max_size (JSON *self)
1583 CODE:
1584 RETVAL = DEC_SIZE (self->flags);
1585 OUTPUT:
1586 RETVAL
1448 1587
1449void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1588void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1450 PPCODE: 1589 PPCODE:
1451{ 1590{
1452 SvREFCNT_dec (self->cb_object); 1591 SvREFCNT_dec (self->cb_object);
1499 SvREFCNT_dec (self->cb_sk_object); 1638 SvREFCNT_dec (self->cb_sk_object);
1500 SvREFCNT_dec (self->cb_object); 1639 SvREFCNT_dec (self->cb_object);
1501 1640
1502PROTOTYPES: ENABLE 1641PROTOTYPES: ENABLE
1503 1642
1504void to_json (SV *scalar) 1643void encode_json (SV *scalar)
1505 PPCODE: 1644 PPCODE:
1506{ 1645{
1507 JSON json = { F_DEFAULT | F_UTF8 }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1508 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1509} 1648}
1510 1649
1511void from_json (SV *jsonstr) 1650void decode_json (SV *jsonstr)
1512 PPCODE: 1651 PPCODE:
1513{ 1652{
1514 JSON json = { F_DEFAULT | F_UTF8 }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1515 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1516} 1655}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines