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.63 by root, Mon Aug 27 01:49:01 2007 UTC vs.
Revision 1.75 by root, Wed Mar 19 15:37:54 2008 UTC

4 4
5#include <assert.h> 5#include <assert.h>
6#include <string.h> 6#include <string.h>
7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h>
9#include <float.h> 10#include <float.h>
10 11
11#if defined(__BORLANDC__) || defined(_MSC_VER) 12#if defined(__BORLANDC__) || defined(_MSC_VER)
12# define snprintf _snprintf // C compilers have this in stdio.h 13# define snprintf _snprintf // C compilers have this in stdio.h
13#endif 14#endif
15// some old perls do not have this, try to make it work, no 16// some old perls do not have this, try to make it work, no
16// guarentees, though. if it breaks, you get to keep the pieces. 17// guarentees, though. if it breaks, you get to keep the pieces.
17#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
18# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
19#endif 20#endif
21
22#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2)
20 23
21#define F_ASCII 0x00000001UL 24#define F_ASCII 0x00000001UL
22#define F_LATIN1 0x00000002UL 25#define F_LATIN1 0x00000002UL
23#define F_UTF8 0x00000004UL 26#define F_UTF8 0x00000004UL
24#define F_INDENT 0x00000008UL 27#define F_INDENT 0x00000008UL
50 53
51#define SB do { 54#define SB do {
52#define SE } while (0) 55#define SE } while (0)
53 56
54#if __GNUC__ >= 3 57#if __GNUC__ >= 3
55# define expect(expr,value) __builtin_expect ((expr),(value)) 58# define expect(expr,value) __builtin_expect ((expr), (value))
56# define inline inline 59# define INLINE static inline
57#else 60#else
58# define expect(expr,value) (expr) 61# define expect(expr,value) (expr)
59# define inline static 62# define INLINE static
60#endif 63#endif
61 64
62#define expect_false(expr) expect ((expr) != 0, 0) 65#define expect_false(expr) expect ((expr) != 0, 0)
63#define expect_true(expr) expect ((expr) != 0, 1) 66#define expect_true(expr) expect ((expr) != 0, 1)
67
68#define IN_RANGE_INC(type,val,beg,end) \
69 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
70 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
64 71
65#ifdef USE_ITHREADS 72#ifdef USE_ITHREADS
66# define JSON_SLOW 1 73# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 74# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
68#else 75#else
80} JSON; 87} JSON;
81 88
82///////////////////////////////////////////////////////////////////////////// 89/////////////////////////////////////////////////////////////////////////////
83// utility functions 90// utility functions
84 91
85inline void 92INLINE void
86shrink (SV *sv) 93shrink (SV *sv)
87{ 94{
88 sv_utf8_downgrade (sv, 1); 95 sv_utf8_downgrade (sv, 1);
89 if (SvLEN (sv) > SvCUR (sv) + 1) 96 if (SvLEN (sv) > SvCUR (sv) + 1)
90 { 97 {
99// decode an utf-8 character and return it, or (UV)-1 in 106// decode an utf-8 character and return it, or (UV)-1 in
100// case of an error. 107// case of an error.
101// we special-case "safe" characters from U+80 .. U+7FF, 108// we special-case "safe" characters from U+80 .. U+7FF,
102// but use the very good perl function to parse anything else. 109// but use the very good perl function to parse anything else.
103// note that we never call this function for a ascii codepoints 110// note that we never call this function for a ascii codepoints
104inline UV 111INLINE UV
105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 112decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
106{ 113{
107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 114 if (expect_true (len >= 2
108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 115 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 116 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
110 { 117 {
111 *clen = 2; 118 *clen = 2;
112 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 119 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
113 } 120 }
114 else 121 else
115 { 122 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
116 *clen = (STRLEN)-1; 123}
117 return (UV)-1; 124
118 } 125// likewise for encoding, also never called for ascii codepoints
126// this function takes advantage of this fact, although current gccs
127// seem to optimise the check for >= 0x80 away anyways
128INLINE unsigned char *
129encode_utf8 (unsigned char *s, UV ch)
130{
131 if (expect_false (ch < 0x000080))
132 *s++ = ch;
133 else if (expect_true (ch < 0x000800))
134 *s++ = 0xc0 | ( ch >> 6),
135 *s++ = 0x80 | ( ch & 0x3f);
136 else if ( ch < 0x010000)
137 *s++ = 0xe0 | ( ch >> 12),
138 *s++ = 0x80 | ((ch >> 6) & 0x3f),
139 *s++ = 0x80 | ( ch & 0x3f);
140 else if ( ch < 0x110000)
141 *s++ = 0xf0 | ( ch >> 18),
142 *s++ = 0x80 | ((ch >> 12) & 0x3f),
143 *s++ = 0x80 | ((ch >> 6) & 0x3f),
144 *s++ = 0x80 | ( ch & 0x3f);
145
146 return s;
119} 147}
120 148
121///////////////////////////////////////////////////////////////////////////// 149/////////////////////////////////////////////////////////////////////////////
122// encoder 150// encoder
123 151
128 char *end; // SvEND (sv) 156 char *end; // SvEND (sv)
129 SV *sv; // result scalar 157 SV *sv; // result scalar
130 JSON json; 158 JSON json;
131 U32 indent; // indentation level 159 U32 indent; // indentation level
132 U32 maxdepth; // max. indentation/recursion level 160 U32 maxdepth; // max. indentation/recursion level
161 UV limit; // escape character values >= this value when encoding
133} enc_t; 162} enc_t;
134 163
135inline void 164INLINE void
136need (enc_t *enc, STRLEN len) 165need (enc_t *enc, STRLEN len)
137{ 166{
138 if (expect_false (enc->cur + len >= enc->end)) 167 if (expect_false (enc->cur + len >= enc->end))
139 { 168 {
140 STRLEN cur = enc->cur - SvPVX (enc->sv); 169 STRLEN cur = enc->cur - SvPVX (enc->sv);
142 enc->cur = SvPVX (enc->sv) + cur; 171 enc->cur = SvPVX (enc->sv) + cur;
143 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 172 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
144 } 173 }
145} 174}
146 175
147inline void 176INLINE void
148encode_ch (enc_t *enc, char ch) 177encode_ch (enc_t *enc, char ch)
149{ 178{
150 need (enc, 1); 179 need (enc, 1);
151 *enc->cur++ = ch; 180 *enc->cur++ = ch;
152} 181}
206 { 235 {
207 uch = ch; 236 uch = ch;
208 clen = 1; 237 clen = 1;
209 } 238 }
210 239
211 if (uch > 0x10FFFFUL) 240 if (uch < 0x80/*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 { 241 {
216 if (uch > 0xFFFFUL) 242 if (uch >= 0x10000UL)
217 { 243 {
244 if (uch >= 0x110000UL)
245 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
246
218 need (enc, len += 11); 247 need (enc, len += 11);
219 sprintf (enc->cur, "\\u%04x\\u%04x", 248 sprintf (enc->cur, "\\u%04x\\u%04x",
220 (int)((uch - 0x10000) / 0x400 + 0xD800), 249 (int)((uch - 0x10000) / 0x400 + 0xD800),
221 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 250 (int)((uch - 0x10000) % 0x400 + 0xDC00));
222 enc->cur += 12; 251 enc->cur += 12;
250 while (--clen); 279 while (--clen);
251 } 280 }
252 else 281 else
253 { 282 {
254 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 283 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
255 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 284 enc->cur = encode_utf8 (enc->cur, uch);
256 ++str; 285 ++str;
257 } 286 }
258 } 287 }
259 } 288 }
260 } 289 }
261 290
262 --len; 291 --len;
263 } 292 }
264} 293}
265 294
266inline void 295INLINE void
267encode_indent (enc_t *enc) 296encode_indent (enc_t *enc)
268{ 297{
269 if (enc->json.flags & F_INDENT) 298 if (enc->json.flags & F_INDENT)
270 { 299 {
271 int spaces = enc->indent * INDENT_STEP; 300 int spaces = enc->indent * INDENT_STEP;
274 memset (enc->cur, ' ', spaces); 303 memset (enc->cur, ' ', spaces);
275 enc->cur += spaces; 304 enc->cur += spaces;
276 } 305 }
277} 306}
278 307
279inline void 308INLINE void
280encode_space (enc_t *enc) 309encode_space (enc_t *enc)
281{ 310{
282 need (enc, 1); 311 need (enc, 1);
283 encode_ch (enc, ' '); 312 encode_ch (enc, ' ');
284} 313}
285 314
286inline void 315INLINE void
287encode_nl (enc_t *enc) 316encode_nl (enc_t *enc)
288{ 317{
289 if (enc->json.flags & F_INDENT) 318 if (enc->json.flags & F_INDENT)
290 { 319 {
291 need (enc, 1); 320 need (enc, 1);
292 encode_ch (enc, '\n'); 321 encode_ch (enc, '\n');
293 } 322 }
294} 323}
295 324
296inline void 325INLINE void
297encode_comma (enc_t *enc) 326encode_comma (enc_t *enc)
298{ 327{
299 encode_ch (enc, ','); 328 encode_ch (enc, ',');
300 329
301 if (enc->json.flags & F_INDENT) 330 if (enc->json.flags & F_INDENT)
312 int i, len = av_len (av); 341 int i, len = av_len (av);
313 342
314 if (enc->indent >= enc->maxdepth) 343 if (enc->indent >= enc->maxdepth)
315 croak ("data structure too deep (hit recursion limit)"); 344 croak ("data structure too deep (hit recursion limit)");
316 345
317 encode_ch (enc, '['); encode_nl (enc); 346 encode_ch (enc, '[');
318 ++enc->indent; 347
348 if (len >= 0)
349 {
350 encode_nl (enc); ++enc->indent;
319 351
320 for (i = 0; i <= len; ++i) 352 for (i = 0; i <= len; ++i)
321 { 353 {
322 SV **svp = av_fetch (av, i, 0); 354 SV **svp = av_fetch (av, i, 0);
323 355
324 encode_indent (enc); 356 encode_indent (enc);
325 357
326 if (svp) 358 if (svp)
327 encode_sv (enc, *svp); 359 encode_sv (enc, *svp);
328 else 360 else
329 encode_str (enc, "null", 4, 0); 361 encode_str (enc, "null", 4, 0);
330 362
331 if (i < len) 363 if (i < len)
332 encode_comma (enc); 364 encode_comma (enc);
333 } 365 }
334 366
367 encode_nl (enc); --enc->indent; encode_indent (enc);
368 }
369
335 encode_nl (enc); 370 encode_ch (enc, ']');
336
337 --enc->indent;
338 encode_indent (enc); encode_ch (enc, ']');
339} 371}
340 372
341static void 373static void
342encode_hk (enc_t *enc, HE *he) 374encode_hk (enc_t *enc, HE *he)
343{ 375{
396 int count; 428 int count;
397 429
398 if (enc->indent >= enc->maxdepth) 430 if (enc->indent >= enc->maxdepth)
399 croak ("data structure too deep (hit recursion limit)"); 431 croak ("data structure too deep (hit recursion limit)");
400 432
401 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 433 encode_ch (enc, '{');
402 434
403 // for canonical output we have to sort by keys first 435 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 436 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 437 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 438 // that randomises hash orderings
459 491
460 FREETMPS; 492 FREETMPS;
461 LEAVE; 493 LEAVE;
462 } 494 }
463 495
496 encode_nl (enc); ++enc->indent;
497
464 while (count--) 498 while (count--)
465 { 499 {
466 encode_indent (enc); 500 encode_indent (enc);
467 he = hes [count]; 501 he = hes [count];
468 encode_hk (enc, he); 502 encode_hk (enc, he);
469 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 503 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
470 504
471 if (count) 505 if (count)
472 encode_comma (enc); 506 encode_comma (enc);
473 } 507 }
508
509 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 510 }
475 } 511 }
476 else 512 else
477 { 513 {
478 if (hv_iterinit (hv) || SvMAGICAL (hv)) 514 if (hv_iterinit (hv) || SvMAGICAL (hv))
479 if ((he = hv_iternext (hv))) 515 if ((he = hv_iternext (hv)))
516 {
517 encode_nl (enc); ++enc->indent;
518
480 for (;;) 519 for (;;)
481 { 520 {
482 encode_indent (enc); 521 encode_indent (enc);
483 encode_hk (enc, he); 522 encode_hk (enc, he);
484 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 523 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
485 524
486 if (!(he = hv_iternext (hv))) 525 if (!(he = hv_iternext (hv)))
487 break; 526 break;
488 527
489 encode_comma (enc); 528 encode_comma (enc);
490 } 529 }
491 }
492 530
531 encode_nl (enc); --enc->indent; encode_indent (enc);
532 }
533 }
534
493 encode_nl (enc); 535 encode_ch (enc, '}');
494
495 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
496} 536}
497 537
498// encode objects, arrays and special \0=false and \1=true values. 538// encode objects, arrays and special \0=false and \1=true values.
499static void 539static void
500encode_rv (enc_t *enc, SV *sv) 540encode_rv (enc_t *enc, SV *sv)
608 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 648 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
609 enc->cur += strlen (enc->cur); 649 enc->cur += strlen (enc->cur);
610 } 650 }
611 else if (SvIOKp (sv)) 651 else if (SvIOKp (sv))
612 { 652 {
613 // we assume we can always read an IV as a UV 653 // we assume we can always read an IV as a UV and vice versa
614 if (SvUV (sv) & ~(UV)0x7fff) 654 // we assume two's complement
615 { 655 // we assume no aliasing issues in the union
616 // large integer, use the (rather slow) snprintf way. 656 if (SvIsUV (sv) ? SvUVX (sv) <= 59000
617 need (enc, sizeof (UV) * 3); 657 : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000)
618 enc->cur +=
619 SvIsUV(sv)
620 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
621 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
622 }
623 else
624 { 658 {
625 // optimise the "small number case" 659 // optimise the "small number case"
626 // code will likely be branchless and use only a single multiplication 660 // code will likely be branchless and use only a single multiplication
661 // works for numbers up to 59074
627 I32 i = SvIV (sv); 662 I32 i = SvIVX (sv);
628 U32 u; 663 U32 u;
629 char digit, nz = 0; 664 char digit, nz = 0;
630 665
631 need (enc, 6); 666 need (enc, 6);
632 667
638 673
639 // now output digit by digit, each time masking out the integer part 674 // now output digit by digit, each time masking out the integer part
640 // and multiplying by 5 while moving the decimal point one to the right, 675 // and multiplying by 5 while moving the decimal point one to the right,
641 // resulting in a net multiplication by 10. 676 // resulting in a net multiplication by 10.
642 // we always write the digit to memory but conditionally increment 677 // we always write the digit to memory but conditionally increment
643 // the pointer, to ease the usage of conditional move instructions. 678 // the pointer, to enable the use of conditional move instructions.
644 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; 679 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5;
645 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; 680 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5;
646 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; 681 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5;
647 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; 682 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5;
648 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' 683 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
684 }
685 else
686 {
687 // large integer, use the (rather slow) snprintf way.
688 need (enc, IVUV_MAXCHARS);
689 enc->cur +=
690 SvIsUV(sv)
691 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
692 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
649 } 693 }
650 } 694 }
651 else if (SvROK (sv)) 695 else if (SvROK (sv))
652 encode_rv (enc, SvRV (sv)); 696 encode_rv (enc, SvRV (sv));
653 else if (!SvOK (sv)) 697 else if (!SvOK (sv))
669 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 713 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
670 enc.cur = SvPVX (enc.sv); 714 enc.cur = SvPVX (enc.sv);
671 enc.end = SvEND (enc.sv); 715 enc.end = SvEND (enc.sv);
672 enc.indent = 0; 716 enc.indent = 0;
673 enc.maxdepth = DEC_DEPTH (enc.json.flags); 717 enc.maxdepth = DEC_DEPTH (enc.json.flags);
718 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
719 : enc.json.flags & F_LATIN1 ? 0x000100UL
720 : 0x110000UL;
674 721
675 SvPOK_only (enc.sv); 722 SvPOK_only (enc.sv);
676 encode_sv (&enc, scalar); 723 encode_sv (&enc, scalar);
677 724
678 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 725 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
699 JSON json; 746 JSON json;
700 U32 depth; // recursion depth 747 U32 depth; // recursion depth
701 U32 maxdepth; // recursion depth limit 748 U32 maxdepth; // recursion depth limit
702} dec_t; 749} dec_t;
703 750
704inline void 751INLINE void
752decode_comment (dec_t *dec)
753{
754 // only '#'-style comments allowed a.t.m.
755
756 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
757 ++dec->cur;
758}
759
760INLINE void
705decode_ws (dec_t *dec) 761decode_ws (dec_t *dec)
706{ 762{
707 for (;;) 763 for (;;)
708 { 764 {
709 char ch = *dec->cur; 765 char ch = *dec->cur;
710 766
711 if (ch > 0x20 767 if (ch > 0x20)
768 {
769 if (expect_false (ch == '#'))
770 {
771 if (dec->json.flags & F_RELAXED)
772 decode_comment (dec);
773 else
774 break;
775 }
776 else
777 break;
778 }
712 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 779 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
713 break; 780 break; // parse error, but let higher level handle it, gives better error messages
714
715 if (ch == '#' && dec->json.flags & F_RELAXED)
716 ++dec->cur;
717 781
718 ++dec->cur; 782 ++dec->cur;
719 } 783 }
720} 784}
721 785
827 891
828 if (hi >= 0x80) 892 if (hi >= 0x80)
829 { 893 {
830 utf8 = 1; 894 utf8 = 1;
831 895
832 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 896 cur = encode_utf8 (cur, hi);
833 } 897 }
834 else 898 else
835 *cur++ = hi; 899 *cur++ = hi;
836 } 900 }
837 break; 901 break;
839 default: 903 default:
840 --dec_cur; 904 --dec_cur;
841 ERR ("illegal backslash escape sequence in string"); 905 ERR ("illegal backslash escape sequence in string");
842 } 906 }
843 } 907 }
844 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 908 else if (expect_true (ch >= 0x20 && ch < 0x80))
845 *cur++ = ch; 909 *cur++ = ch;
846 else if (ch >= 0x80) 910 else if (ch >= 0x80)
847 { 911 {
848 STRLEN clen; 912 STRLEN clen;
849 UV uch; 913 UV uch;
972 1036
973 if (!is_nv) 1037 if (!is_nv)
974 { 1038 {
975 int len = dec->cur - start; 1039 int len = dec->cur - start;
976 1040
977 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1041 // special case the rather common 1..5-digit-int case
978 if (*start == '-') 1042 if (*start == '-')
979 switch (len) 1043 switch (len)
980 { 1044 {
981 case 2: return newSViv (-( start [1] - '0' * 1)); 1045 case 2: return newSViv (-( start [1] - '0' * 1));
982 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1046 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
983 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1047 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
984 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1048 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1049 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
985 } 1050 }
986 else 1051 else
987 switch (len) 1052 switch (len)
988 { 1053 {
989 case 1: return newSViv ( start [0] - '0' * 1); 1054 case 1: return newSViv ( start [0] - '0' * 1);
990 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1055 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
991 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1056 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
992 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1057 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1058 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
993 } 1059 }
994 1060
995 { 1061 {
996 UV uv; 1062 UV uv;
997 int numtype = grok_number (start, len, &uv); 1063 int numtype = grok_number (start, len, &uv);
1246 1312
1247static SV * 1313static SV *
1248decode_sv (dec_t *dec) 1314decode_sv (dec_t *dec)
1249{ 1315{
1250 // the beauty of JSON: you need exactly one character lookahead 1316 // the beauty of JSON: you need exactly one character lookahead
1251 // to parse anything. 1317 // to parse everything.
1252 switch (*dec->cur) 1318 switch (*dec->cur)
1253 { 1319 {
1254 case '"': ++dec->cur; return decode_str (dec); 1320 case '"': ++dec->cur; return decode_str (dec);
1255 case '[': ++dec->cur; return decode_av (dec); 1321 case '[': ++dec->cur; return decode_av (dec);
1256 case '{': ++dec->cur; return decode_hv (dec); 1322 case '{': ++dec->cur; return decode_hv (dec);
1257 1323
1258 case '-': 1324 case '-':
1259 case '0': case '1': case '2': case '3': case '4': 1325 case '0': case '1': case '2': case '3': case '4':
1260 case '5': case '6': case '7': case '8': case '9': 1326 case '5': case '6': case '7': case '8': case '9':
1261 return decode_num (dec); 1327 return decode_num (dec);
1429{ 1495{
1430 SV *pv = NEWSV (0, sizeof (JSON)); 1496 SV *pv = NEWSV (0, sizeof (JSON));
1431 SvPOK_only (pv); 1497 SvPOK_only (pv);
1432 Zero (SvPVX (pv), 1, JSON); 1498 Zero (SvPVX (pv), 1, JSON);
1433 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1499 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1434 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1500 XPUSHs (sv_2mortal (sv_bless (
1501 newRV_noinc (pv),
1502 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1503 )));
1435} 1504}
1436 1505
1437void ascii (JSON *self, int enable = 1) 1506void ascii (JSON *self, int enable = 1)
1438 ALIAS: 1507 ALIAS:
1439 ascii = F_ASCII 1508 ascii = F_ASCII
1457 self->flags &= ~ix; 1526 self->flags &= ~ix;
1458 1527
1459 XPUSHs (ST (0)); 1528 XPUSHs (ST (0));
1460} 1529}
1461 1530
1531void get_ascii (JSON *self)
1532 ALIAS:
1533 get_ascii = F_ASCII
1534 get_latin1 = F_LATIN1
1535 get_utf8 = F_UTF8
1536 get_indent = F_INDENT
1537 get_canonical = F_CANONICAL
1538 get_space_before = F_SPACE_BEFORE
1539 get_space_after = F_SPACE_AFTER
1540 get_allow_nonref = F_ALLOW_NONREF
1541 get_shrink = F_SHRINK
1542 get_allow_blessed = F_ALLOW_BLESSED
1543 get_convert_blessed = F_CONV_BLESSED
1544 get_relaxed = F_RELAXED
1545 PPCODE:
1546 XPUSHs (boolSV (self->flags & ix));
1547
1462void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1548void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1463 PPCODE: 1549 PPCODE:
1464{ 1550{
1465 UV log2 = 0; 1551 UV log2 = 0;
1466 1552
1472 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1558 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1473 1559
1474 XPUSHs (ST (0)); 1560 XPUSHs (ST (0));
1475} 1561}
1476 1562
1563U32 get_max_depth (JSON *self)
1564 CODE:
1565 RETVAL = DEC_DEPTH (self->flags);
1566 OUTPUT:
1567 RETVAL
1568
1477void max_size (JSON *self, UV max_size = 0) 1569void max_size (JSON *self, UV max_size = 0)
1478 PPCODE: 1570 PPCODE:
1479{ 1571{
1480 UV log2 = 0; 1572 UV log2 = 0;
1481 1573
1487 1579
1488 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1580 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1489 1581
1490 XPUSHs (ST (0)); 1582 XPUSHs (ST (0));
1491} 1583}
1584
1585int get_max_size (JSON *self)
1586 CODE:
1587 RETVAL = DEC_SIZE (self->flags);
1588 OUTPUT:
1589 RETVAL
1492 1590
1493void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1591void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1494 PPCODE: 1592 PPCODE:
1495{ 1593{
1496 SvREFCNT_dec (self->cb_object); 1594 SvREFCNT_dec (self->cb_object);
1543 SvREFCNT_dec (self->cb_sk_object); 1641 SvREFCNT_dec (self->cb_sk_object);
1544 SvREFCNT_dec (self->cb_object); 1642 SvREFCNT_dec (self->cb_object);
1545 1643
1546PROTOTYPES: ENABLE 1644PROTOTYPES: ENABLE
1547 1645
1548void to_json (SV *scalar) 1646void encode_json (SV *scalar)
1549 PPCODE: 1647 PPCODE:
1550{ 1648{
1551 JSON json = { F_DEFAULT | F_UTF8 }; 1649 JSON json = { F_DEFAULT | F_UTF8 };
1552 XPUSHs (encode_json (scalar, &json)); 1650 XPUSHs (encode_json (scalar, &json));
1553} 1651}
1554 1652
1555void from_json (SV *jsonstr) 1653void decode_json (SV *jsonstr)
1556 PPCODE: 1654 PPCODE:
1557{ 1655{
1558 JSON json = { F_DEFAULT | F_UTF8 }; 1656 JSON json = { F_DEFAULT | F_UTF8 };
1559 XPUSHs (decode_json (jsonstr, &json, 0)); 1657 XPUSHs (decode_json (jsonstr, &json, 0));
1560} 1658}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines