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.59 by root, Mon Aug 13 16:14:20 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)
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)))
62 68
63#ifdef USE_ITHREADS 69#ifdef USE_ITHREADS
64# define JSON_SLOW 1 70# define JSON_SLOW 1
65# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
66#else 72#else
78} JSON; 84} JSON;
79 85
80///////////////////////////////////////////////////////////////////////////// 86/////////////////////////////////////////////////////////////////////////////
81// utility functions 87// utility functions
82 88
83inline void 89INLINE void
84shrink (SV *sv) 90shrink (SV *sv)
85{ 91{
86 sv_utf8_downgrade (sv, 1); 92 sv_utf8_downgrade (sv, 1);
87 if (SvLEN (sv) > SvCUR (sv) + 1) 93 if (SvLEN (sv) > SvCUR (sv) + 1)
88 { 94 {
97// 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
98// case of an error. 104// case of an error.
99// we special-case "safe" characters from U+80 .. U+7FF, 105// we special-case "safe" characters from U+80 .. U+7FF,
100// but use the very good perl function to parse anything else. 106// but use the very good perl function to parse anything else.
101// note that we never call this function for a ascii codepoints 107// note that we never call this function for a ascii codepoints
102inline UV 108INLINE UV
103decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
104{ 110{
105 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 111 if (expect_true (len >= 2
106 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 112 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
107 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 113 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
108 { 114 {
109 *clen = 2; 115 *clen = 2;
110 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 116 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
111 } 117 }
112 else 118 else
113 { 119 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
114 *clen = (STRLEN)-1; 120}
115 return (UV)-1; 121
116 } 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;
117} 144}
118 145
119///////////////////////////////////////////////////////////////////////////// 146/////////////////////////////////////////////////////////////////////////////
120// encoder 147// encoder
121 148
126 char *end; // SvEND (sv) 153 char *end; // SvEND (sv)
127 SV *sv; // result scalar 154 SV *sv; // result scalar
128 JSON json; 155 JSON json;
129 U32 indent; // indentation level 156 U32 indent; // indentation level
130 U32 maxdepth; // max. indentation/recursion level 157 U32 maxdepth; // max. indentation/recursion level
158 UV limit; // escape character values >= this value when encoding
131} enc_t; 159} enc_t;
132 160
133inline void 161INLINE void
134need (enc_t *enc, STRLEN len) 162need (enc_t *enc, STRLEN len)
135{ 163{
136 if (expect_false (enc->cur + len >= enc->end)) 164 if (expect_false (enc->cur + len >= enc->end))
137 { 165 {
138 STRLEN cur = enc->cur - SvPVX (enc->sv); 166 STRLEN cur = enc->cur - SvPVX (enc->sv);
140 enc->cur = SvPVX (enc->sv) + cur; 168 enc->cur = SvPVX (enc->sv) + cur;
141 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 169 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
142 } 170 }
143} 171}
144 172
145inline void 173INLINE void
146encode_ch (enc_t *enc, char ch) 174encode_ch (enc_t *enc, char ch)
147{ 175{
148 need (enc, 1); 176 need (enc, 1);
149 *enc->cur++ = ch; 177 *enc->cur++ = ch;
150} 178}
204 { 232 {
205 uch = ch; 233 uch = ch;
206 clen = 1; 234 clen = 1;
207 } 235 }
208 236
209 if (uch > 0x10FFFFUL) 237 if (uch < 0x80/*0x20*/ || uch >= enc->limit)
210 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
211
212 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
213 { 238 {
214 if (uch > 0xFFFFUL) 239 if (uch >= 0x10000UL)
215 { 240 {
241 if (uch >= 0x110000UL)
242 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
243
216 need (enc, len += 11); 244 need (enc, len += 11);
217 sprintf (enc->cur, "\\u%04x\\u%04x", 245 sprintf (enc->cur, "\\u%04x\\u%04x",
218 (int)((uch - 0x10000) / 0x400 + 0xD800), 246 (int)((uch - 0x10000) / 0x400 + 0xD800),
219 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 247 (int)((uch - 0x10000) % 0x400 + 0xDC00));
220 enc->cur += 12; 248 enc->cur += 12;
248 while (--clen); 276 while (--clen);
249 } 277 }
250 else 278 else
251 { 279 {
252 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
253 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 281 enc->cur = encode_utf8 (enc->cur, uch);
254 ++str; 282 ++str;
255 } 283 }
256 } 284 }
257 } 285 }
258 } 286 }
259 287
260 --len; 288 --len;
261 } 289 }
262} 290}
263 291
264inline void 292INLINE void
265encode_indent (enc_t *enc) 293encode_indent (enc_t *enc)
266{ 294{
267 if (enc->json.flags & F_INDENT) 295 if (enc->json.flags & F_INDENT)
268 { 296 {
269 int spaces = enc->indent * INDENT_STEP; 297 int spaces = enc->indent * INDENT_STEP;
272 memset (enc->cur, ' ', spaces); 300 memset (enc->cur, ' ', spaces);
273 enc->cur += spaces; 301 enc->cur += spaces;
274 } 302 }
275} 303}
276 304
277inline void 305INLINE void
278encode_space (enc_t *enc) 306encode_space (enc_t *enc)
279{ 307{
280 need (enc, 1); 308 need (enc, 1);
281 encode_ch (enc, ' '); 309 encode_ch (enc, ' ');
282} 310}
283 311
284inline void 312INLINE void
285encode_nl (enc_t *enc) 313encode_nl (enc_t *enc)
286{ 314{
287 if (enc->json.flags & F_INDENT) 315 if (enc->json.flags & F_INDENT)
288 { 316 {
289 need (enc, 1); 317 need (enc, 1);
290 encode_ch (enc, '\n'); 318 encode_ch (enc, '\n');
291 } 319 }
292} 320}
293 321
294inline void 322INLINE void
295encode_comma (enc_t *enc) 323encode_comma (enc_t *enc)
296{ 324{
297 encode_ch (enc, ','); 325 encode_ch (enc, ',');
298 326
299 if (enc->json.flags & F_INDENT) 327 if (enc->json.flags & F_INDENT)
310 int i, len = av_len (av); 338 int i, len = av_len (av);
311 339
312 if (enc->indent >= enc->maxdepth) 340 if (enc->indent >= enc->maxdepth)
313 croak ("data structure too deep (hit recursion limit)"); 341 croak ("data structure too deep (hit recursion limit)");
314 342
315 encode_ch (enc, '['); encode_nl (enc); 343 encode_ch (enc, '[');
316 ++enc->indent; 344
345 if (len >= 0)
346 {
347 encode_nl (enc); ++enc->indent;
317 348
318 for (i = 0; i <= len; ++i) 349 for (i = 0; i <= len; ++i)
319 { 350 {
320 SV **svp = av_fetch (av, i, 0); 351 SV **svp = av_fetch (av, i, 0);
321 352
322 encode_indent (enc); 353 encode_indent (enc);
323 354
324 if (svp) 355 if (svp)
325 encode_sv (enc, *svp); 356 encode_sv (enc, *svp);
326 else 357 else
327 encode_str (enc, "null", 4, 0); 358 encode_str (enc, "null", 4, 0);
328 359
329 if (i < len) 360 if (i < len)
330 encode_comma (enc); 361 encode_comma (enc);
331 } 362 }
332 363
364 encode_nl (enc); --enc->indent; encode_indent (enc);
365 }
366
333 encode_nl (enc); 367 encode_ch (enc, ']');
334
335 --enc->indent;
336 encode_indent (enc); encode_ch (enc, ']');
337} 368}
338 369
339static void 370static void
340encode_he (enc_t *enc, HE *he) 371encode_hk (enc_t *enc, HE *he)
341{ 372{
342 encode_ch (enc, '"'); 373 encode_ch (enc, '"');
343 374
344 if (HeKLEN (he) == HEf_SVKEY) 375 if (HeKLEN (he) == HEf_SVKEY)
345 { 376 {
358 encode_ch (enc, '"'); 389 encode_ch (enc, '"');
359 390
360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 391 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
361 encode_ch (enc, ':'); 392 encode_ch (enc, ':');
362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 393 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
363 encode_sv (enc, HeVAL (he));
364} 394}
365 395
366// compare hash entries, used when all keys are bytestrings 396// compare hash entries, used when all keys are bytestrings
367static int 397static int
368he_cmp_fast (const void *a_, const void *b_) 398he_cmp_fast (const void *a_, const void *b_)
373 HE *b = *(HE **)b_; 403 HE *b = *(HE **)b_;
374 404
375 STRLEN la = HeKLEN (a); 405 STRLEN la = HeKLEN (a);
376 STRLEN lb = HeKLEN (b); 406 STRLEN lb = HeKLEN (b);
377 407
378 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 408 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
379 cmp = la - lb; 409 cmp = lb - la;
380 410
381 return cmp; 411 return cmp;
382} 412}
383 413
384// 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
385static int 415static int
386he_cmp_slow (const void *a, const void *b) 416he_cmp_slow (const void *a, const void *b)
387{ 417{
388 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 418 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
389} 419}
390 420
391static void 421static void
392encode_hv (enc_t *enc, HV *hv) 422encode_hv (enc_t *enc, HV *hv)
393{ 423{
424 HE *he;
394 int count, i; 425 int count;
395 426
396 if (enc->indent >= enc->maxdepth) 427 if (enc->indent >= enc->maxdepth)
397 croak ("data structure too deep (hit recursion limit)"); 428 croak ("data structure too deep (hit recursion limit)");
398 429
399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 430 encode_ch (enc, '{');
400 431
401 if ((count = hv_iterinit (hv)))
402 {
403 // for canonical output we have to sort by keys first 432 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 433 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 434 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 435 // that randomises hash orderings
407 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))
408 { 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 {
409 int fast = 1; 456 int i, fast = 1;
410 HE *he;
411#if defined(__BORLANDC__) || defined(_MSC_VER) 457#if defined(__BORLANDC__) || defined(_MSC_VER)
412 HE **hes = _alloca (count * sizeof (HE)); 458 HE **hes = _alloca (count * sizeof (HE));
413#else 459#else
414 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
415#endif 461#endif
442 488
443 FREETMPS; 489 FREETMPS;
444 LEAVE; 490 LEAVE;
445 } 491 }
446 492
447 for (i = 0; i < count; ++i) 493 encode_nl (enc); ++enc->indent;
494
495 while (count--)
448 { 496 {
449 encode_indent (enc); 497 encode_indent (enc);
498 he = hes [count];
450 encode_he (enc, hes [i]); 499 encode_hk (enc, he);
500 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 501
452 if (i < count - 1) 502 if (count)
453 encode_comma (enc); 503 encode_comma (enc);
454 } 504 }
455 505
456 encode_nl (enc); 506 encode_nl (enc); --enc->indent; encode_indent (enc);
457 } 507 }
508 }
458 else 509 else
510 {
511 if (hv_iterinit (hv) || SvMAGICAL (hv))
512 if ((he = hv_iternext (hv)))
459 { 513 {
460 HE *he = hv_iternext (hv); 514 encode_nl (enc); ++enc->indent;
461 515
462 for (;;) 516 for (;;)
463 { 517 {
464 encode_indent (enc); 518 encode_indent (enc);
465 encode_he (enc, he); 519 encode_hk (enc, he);
520 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
466 521
467 if (!(he = hv_iternext (hv))) 522 if (!(he = hv_iternext (hv)))
468 break; 523 break;
469 524
470 encode_comma (enc); 525 encode_comma (enc);
471 } 526 }
472 527
473 encode_nl (enc); 528 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 529 }
475 } 530 }
476 531
477 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 532 encode_ch (enc, '}');
478} 533}
479 534
480// encode objects, arrays and special \0=false and \1=true values. 535// encode objects, arrays and special \0=false and \1=true values.
481static void 536static void
482encode_rv (enc_t *enc, SV *sv) 537encode_rv (enc_t *enc, SV *sv)
590 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
591 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
592 } 647 }
593 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
594 { 649 {
595 // 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
596 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)
597 { 655 {
598 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
599 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
600 enc->cur += 658 enc->cur +=
601 SvIsUV(sv) 659 SvIsUV(sv)
602 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
603 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
604 } 662 }
605 else 663 else
606 { 664 {
607 // optimise the "small number case" 665 // optimise the "small number case"
608 // 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
609 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
610 U32 u; 669 U32 u;
611 char digit, nz = 0; 670 char digit, nz = 0;
612 671
613 need (enc, 6); 672 need (enc, 6);
614 673
651 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
652 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
653 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
654 enc.indent = 0; 713 enc.indent = 0;
655 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;
656 718
657 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
658 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
659 721
660 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
681 JSON json; 743 JSON json;
682 U32 depth; // recursion depth 744 U32 depth; // recursion depth
683 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
684} dec_t; 746} dec_t;
685 747
686inline 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
687decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
688{ 759{
689 for (;;) 760 for (;;)
690 { 761 {
691 char ch = *dec->cur; 762 char ch = *dec->cur;
692 763
693 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 }
694 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 776 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
695 break; 777 break; // parse error, but let higher level handle it, gives better error messages
696 778
697 ++dec->cur; 779 ++dec->cur;
698 } 780 }
699} 781}
700 782
806 888
807 if (hi >= 0x80) 889 if (hi >= 0x80)
808 { 890 {
809 utf8 = 1; 891 utf8 = 1;
810 892
811 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
812 } 894 }
813 else 895 else
814 *cur++ = hi; 896 *cur++ = hi;
815 } 897 }
816 break; 898 break;
818 default: 900 default:
819 --dec_cur; 901 --dec_cur;
820 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
821 } 903 }
822 } 904 }
823 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
824 *cur++ = ch; 906 *cur++ = ch;
825 else if (ch >= 0x80) 907 else if (ch >= 0x80)
826 { 908 {
827 STRLEN clen; 909 STRLEN clen;
828 UV uch; 910 UV uch;
951 1033
952 if (!is_nv) 1034 if (!is_nv)
953 { 1035 {
954 int len = dec->cur - start; 1036 int len = dec->cur - start;
955 1037
956 // 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
957 if (*start == '-') 1039 if (*start == '-')
958 switch (len) 1040 switch (len)
959 { 1041 {
960 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
961 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
962 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));
963 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));
964 } 1047 }
965 else 1048 else
966 switch (len) 1049 switch (len)
967 { 1050 {
968 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
969 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
970 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);
971 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);
972 } 1056 }
973 1057
974 { 1058 {
975 UV uv; 1059 UV uv;
976 int numtype = grok_number (start, len, &uv); 1060 int numtype = grok_number (start, len, &uv);
1037 1121
1038 if (*dec->cur != ',') 1122 if (*dec->cur != ',')
1039 ERR (", or ] expected while parsing array"); 1123 ERR (", or ] expected while parsing array");
1040 1124
1041 ++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 }
1042 } 1134 }
1043 1135
1044 DEC_DEC_DEPTH; 1136 DEC_DEC_DEPTH;
1045 return newRV_noinc ((SV *)av); 1137 return newRV_noinc ((SV *)av);
1046 1138
1062 if (*dec->cur == '}') 1154 if (*dec->cur == '}')
1063 ++dec->cur; 1155 ++dec->cur;
1064 else 1156 else
1065 for (;;) 1157 for (;;)
1066 { 1158 {
1067 decode_ws (dec); EXPECT_CH ('"'); 1159 EXPECT_CH ('"');
1068 1160
1069 // heuristic: assume that 1161 // heuristic: assume that
1070 // a) decode_str + hv_store_ent are abysmally slow. 1162 // a) decode_str + hv_store_ent are abysmally slow.
1071 // b) most hash keys are short, simple ascii text. 1163 // b) most hash keys are short, simple ascii text.
1072 // => try to "fast-match" such strings to avoid 1164 // => try to "fast-match" such strings to avoid
1086 if (!key) 1178 if (!key)
1087 goto fail; 1179 goto fail;
1088 1180
1089 decode_ws (dec); EXPECT_CH (':'); 1181 decode_ws (dec); EXPECT_CH (':');
1090 1182
1183 decode_ws (dec);
1091 value = decode_sv (dec); 1184 value = decode_sv (dec);
1092 if (!value) 1185 if (!value)
1093 { 1186 {
1094 SvREFCNT_dec (key); 1187 SvREFCNT_dec (key);
1095 goto fail; 1188 goto fail;
1107 int len = p - key; 1200 int len = p - key;
1108 dec->cur = p + 1; 1201 dec->cur = p + 1;
1109 1202
1110 decode_ws (dec); EXPECT_CH (':'); 1203 decode_ws (dec); EXPECT_CH (':');
1111 1204
1205 decode_ws (dec);
1112 value = decode_sv (dec); 1206 value = decode_sv (dec);
1113 if (!value) 1207 if (!value)
1114 goto fail; 1208 goto fail;
1115 1209
1116 hv_store (hv, key, len, value, 0); 1210 hv_store (hv, key, len, value, 0);
1132 1226
1133 if (*dec->cur != ',') 1227 if (*dec->cur != ',')
1134 ERR (", or } expected while parsing object/hash"); 1228 ERR (", or } expected while parsing object/hash");
1135 1229
1136 ++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 }
1137 } 1239 }
1138 1240
1139 DEC_DEC_DEPTH; 1241 DEC_DEC_DEPTH;
1140 sv = newRV_noinc ((SV *)hv); 1242 sv = newRV_noinc ((SV *)hv);
1141 1243
1206} 1308}
1207 1309
1208static SV * 1310static SV *
1209decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1210{ 1312{
1211 decode_ws (dec);
1212
1213 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1214 // to parse anything. 1314 // to parse everything.
1215 switch (*dec->cur) 1315 switch (*dec->cur)
1216 { 1316 {
1217 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1218 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1219 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1220 1320
1221 case '-': 1321 case '-':
1222 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1223 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1224 return decode_num (dec); 1324 return decode_num (dec);
1225 1325
1226 case 't': 1326 case 't':
1227 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1327 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1228 { 1328 {
1229 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
1230 return SvREFCNT_inc (json_true); 1333 return SvREFCNT_inc (json_true);
1231 } 1334 }
1232 else 1335 else
1233 ERR ("'true' expected"); 1336 ERR ("'true' expected");
1234 1337
1236 1339
1237 case 'f': 1340 case 'f':
1238 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1341 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1239 { 1342 {
1240 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
1241 return SvREFCNT_inc (json_false); 1347 return SvREFCNT_inc (json_false);
1242 } 1348 }
1243 else 1349 else
1244 ERR ("'false' expected"); 1350 ERR ("'false' expected");
1245 1351
1295 1401
1296 if (dec.json.cb_object || dec.json.cb_sk_object) 1402 if (dec.json.cb_object || dec.json.cb_sk_object)
1297 dec.json.flags |= F_HOOK; 1403 dec.json.flags |= F_HOOK;
1298 1404
1299 *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);
1300 sv = decode_sv (&dec); 1408 sv = decode_sv (&dec);
1301 1409
1302 if (!(offset_return || !sv)) 1410 if (!(offset_return || !sv))
1303 { 1411 {
1304 // check for trailing garbage 1412 // check for trailing garbage
1384{ 1492{
1385 SV *pv = NEWSV (0, sizeof (JSON)); 1493 SV *pv = NEWSV (0, sizeof (JSON));
1386 SvPOK_only (pv); 1494 SvPOK_only (pv);
1387 Zero (SvPVX (pv), 1, JSON); 1495 Zero (SvPVX (pv), 1, JSON);
1388 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1496 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1389 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1497 XPUSHs (sv_2mortal (sv_bless (
1498 newRV_noinc (pv),
1499 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1500 )));
1390} 1501}
1391 1502
1392void ascii (JSON *self, int enable = 1) 1503void ascii (JSON *self, int enable = 1)
1393 ALIAS: 1504 ALIAS:
1394 ascii = F_ASCII 1505 ascii = F_ASCII
1401 pretty = F_PRETTY 1512 pretty = F_PRETTY
1402 allow_nonref = F_ALLOW_NONREF 1513 allow_nonref = F_ALLOW_NONREF
1403 shrink = F_SHRINK 1514 shrink = F_SHRINK
1404 allow_blessed = F_ALLOW_BLESSED 1515 allow_blessed = F_ALLOW_BLESSED
1405 convert_blessed = F_CONV_BLESSED 1516 convert_blessed = F_CONV_BLESSED
1517 relaxed = F_RELAXED
1406 PPCODE: 1518 PPCODE:
1407{ 1519{
1408 if (enable) 1520 if (enable)
1409 self->flags |= ix; 1521 self->flags |= ix;
1410 else 1522 else
1411 self->flags &= ~ix; 1523 self->flags &= ~ix;
1412 1524
1413 XPUSHs (ST (0)); 1525 XPUSHs (ST (0));
1414} 1526}
1415 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
1416void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1545void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1417 PPCODE: 1546 PPCODE:
1418{ 1547{
1419 UV log2 = 0; 1548 UV log2 = 0;
1420 1549
1426 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1555 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1427 1556
1428 XPUSHs (ST (0)); 1557 XPUSHs (ST (0));
1429} 1558}
1430 1559
1560U32 get_max_depth (JSON *self)
1561 CODE:
1562 RETVAL = DEC_DEPTH (self->flags);
1563 OUTPUT:
1564 RETVAL
1565
1431void max_size (JSON *self, UV max_size = 0) 1566void max_size (JSON *self, UV max_size = 0)
1432 PPCODE: 1567 PPCODE:
1433{ 1568{
1434 UV log2 = 0; 1569 UV log2 = 0;
1435 1570
1441 1576
1442 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1577 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1443 1578
1444 XPUSHs (ST (0)); 1579 XPUSHs (ST (0));
1445} 1580}
1581
1582int get_max_size (JSON *self)
1583 CODE:
1584 RETVAL = DEC_SIZE (self->flags);
1585 OUTPUT:
1586 RETVAL
1446 1587
1447void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1588void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1448 PPCODE: 1589 PPCODE:
1449{ 1590{
1450 SvREFCNT_dec (self->cb_object); 1591 SvREFCNT_dec (self->cb_object);
1497 SvREFCNT_dec (self->cb_sk_object); 1638 SvREFCNT_dec (self->cb_sk_object);
1498 SvREFCNT_dec (self->cb_object); 1639 SvREFCNT_dec (self->cb_object);
1499 1640
1500PROTOTYPES: ENABLE 1641PROTOTYPES: ENABLE
1501 1642
1502void to_json (SV *scalar) 1643void encode_json (SV *scalar)
1503 PPCODE: 1644 PPCODE:
1504{ 1645{
1505 JSON json = { F_DEFAULT | F_UTF8 }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1506 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1507} 1648}
1508 1649
1509void from_json (SV *jsonstr) 1650void decode_json (SV *jsonstr)
1510 PPCODE: 1651 PPCODE:
1511{ 1652{
1512 JSON json = { F_DEFAULT | F_UTF8 }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1513 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1514} 1655}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines