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.64 by root, Mon Aug 27 02:03:23 2007 UTC vs.
Revision 1.79 by root, Tue Mar 25 23:00:31 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
71#endif 78#endif
72 79
73static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
74static SV *json_true, *json_false; 81static SV *json_true, *json_false;
75 82
83enum {
84 INCR_M_WS = 0, // initial whitespace skipping, must be 0
85 INCR_M_STR, // inside string
86 INCR_M_BS, // inside backslash
87 INCR_M_JSON // outside anything, count nesting
88};
89
90#define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON)
91
76typedef struct { 92typedef struct {
77 U32 flags; 93 U32 flags;
78 SV *cb_object; 94 SV *cb_object;
79 HV *cb_sk_object; 95 HV *cb_sk_object;
96
97 // for the incremental parser
98 SV *incr_text; // the source text so far
99 STRLEN incr_pos; // the current offset into the text
100 int incr_nest; // {[]}-nesting level
101 int incr_mode;
80} JSON; 102} JSON;
81 103
82///////////////////////////////////////////////////////////////////////////// 104/////////////////////////////////////////////////////////////////////////////
83// utility functions 105// utility functions
84 106
85inline void 107INLINE void
86shrink (SV *sv) 108shrink (SV *sv)
87{ 109{
88 sv_utf8_downgrade (sv, 1); 110 sv_utf8_downgrade (sv, 1);
89 if (SvLEN (sv) > SvCUR (sv) + 1) 111 if (SvLEN (sv) > SvCUR (sv) + 1)
90 { 112 {
99// decode an utf-8 character and return it, or (UV)-1 in 121// decode an utf-8 character and return it, or (UV)-1 in
100// case of an error. 122// case of an error.
101// we special-case "safe" characters from U+80 .. U+7FF, 123// we special-case "safe" characters from U+80 .. U+7FF,
102// but use the very good perl function to parse anything else. 124// but use the very good perl function to parse anything else.
103// note that we never call this function for a ascii codepoints 125// note that we never call this function for a ascii codepoints
104inline UV 126INLINE UV
105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 127decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
106{ 128{
107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 129 if (expect_true (len >= 2
108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 130 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 131 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
110 { 132 {
111 *clen = 2; 133 *clen = 2;
112 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 134 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
113 } 135 }
114 else 136 else
115 { 137 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
116 *clen = (STRLEN)-1; 138}
117 return (UV)-1; 139
118 } 140// likewise for encoding, also never called for ascii codepoints
141// this function takes advantage of this fact, although current gccs
142// seem to optimise the check for >= 0x80 away anyways
143INLINE unsigned char *
144encode_utf8 (unsigned char *s, UV ch)
145{
146 if (expect_false (ch < 0x000080))
147 *s++ = ch;
148 else if (expect_true (ch < 0x000800))
149 *s++ = 0xc0 | ( ch >> 6),
150 *s++ = 0x80 | ( ch & 0x3f);
151 else if ( ch < 0x010000)
152 *s++ = 0xe0 | ( ch >> 12),
153 *s++ = 0x80 | ((ch >> 6) & 0x3f),
154 *s++ = 0x80 | ( ch & 0x3f);
155 else if ( ch < 0x110000)
156 *s++ = 0xf0 | ( ch >> 18),
157 *s++ = 0x80 | ((ch >> 12) & 0x3f),
158 *s++ = 0x80 | ((ch >> 6) & 0x3f),
159 *s++ = 0x80 | ( ch & 0x3f);
160
161 return s;
119} 162}
120 163
121///////////////////////////////////////////////////////////////////////////// 164/////////////////////////////////////////////////////////////////////////////
122// encoder 165// encoder
123 166
128 char *end; // SvEND (sv) 171 char *end; // SvEND (sv)
129 SV *sv; // result scalar 172 SV *sv; // result scalar
130 JSON json; 173 JSON json;
131 U32 indent; // indentation level 174 U32 indent; // indentation level
132 U32 maxdepth; // max. indentation/recursion level 175 U32 maxdepth; // max. indentation/recursion level
176 UV limit; // escape character values >= this value when encoding
133} enc_t; 177} enc_t;
134 178
135inline void 179INLINE void
136need (enc_t *enc, STRLEN len) 180need (enc_t *enc, STRLEN len)
137{ 181{
138 if (expect_false (enc->cur + len >= enc->end)) 182 if (expect_false (enc->cur + len >= enc->end))
139 { 183 {
140 STRLEN cur = enc->cur - SvPVX (enc->sv); 184 STRLEN cur = enc->cur - SvPVX (enc->sv);
142 enc->cur = SvPVX (enc->sv) + cur; 186 enc->cur = SvPVX (enc->sv) + cur;
143 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 187 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
144 } 188 }
145} 189}
146 190
147inline void 191INLINE void
148encode_ch (enc_t *enc, char ch) 192encode_ch (enc_t *enc, char ch)
149{ 193{
150 need (enc, 1); 194 need (enc, 1);
151 *enc->cur++ = ch; 195 *enc->cur++ = ch;
152} 196}
206 { 250 {
207 uch = ch; 251 uch = ch;
208 clen = 1; 252 clen = 1;
209 } 253 }
210 254
211 if (uch > 0x10FFFFUL) 255 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 { 256 {
216 if (uch > 0xFFFFUL) 257 if (uch >= 0x10000UL)
217 { 258 {
259 if (uch >= 0x110000UL)
260 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
261
218 need (enc, len += 11); 262 need (enc, len += 11);
219 sprintf (enc->cur, "\\u%04x\\u%04x", 263 sprintf (enc->cur, "\\u%04x\\u%04x",
220 (int)((uch - 0x10000) / 0x400 + 0xD800), 264 (int)((uch - 0x10000) / 0x400 + 0xD800),
221 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 265 (int)((uch - 0x10000) % 0x400 + 0xDC00));
222 enc->cur += 12; 266 enc->cur += 12;
250 while (--clen); 294 while (--clen);
251 } 295 }
252 else 296 else
253 { 297 {
254 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 298 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
255 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 299 enc->cur = encode_utf8 (enc->cur, uch);
256 ++str; 300 ++str;
257 } 301 }
258 } 302 }
259 } 303 }
260 } 304 }
261 305
262 --len; 306 --len;
263 } 307 }
264} 308}
265 309
266inline void 310INLINE void
267encode_indent (enc_t *enc) 311encode_indent (enc_t *enc)
268{ 312{
269 if (enc->json.flags & F_INDENT) 313 if (enc->json.flags & F_INDENT)
270 { 314 {
271 int spaces = enc->indent * INDENT_STEP; 315 int spaces = enc->indent * INDENT_STEP;
274 memset (enc->cur, ' ', spaces); 318 memset (enc->cur, ' ', spaces);
275 enc->cur += spaces; 319 enc->cur += spaces;
276 } 320 }
277} 321}
278 322
279inline void 323INLINE void
280encode_space (enc_t *enc) 324encode_space (enc_t *enc)
281{ 325{
282 need (enc, 1); 326 need (enc, 1);
283 encode_ch (enc, ' '); 327 encode_ch (enc, ' ');
284} 328}
285 329
286inline void 330INLINE void
287encode_nl (enc_t *enc) 331encode_nl (enc_t *enc)
288{ 332{
289 if (enc->json.flags & F_INDENT) 333 if (enc->json.flags & F_INDENT)
290 { 334 {
291 need (enc, 1); 335 need (enc, 1);
292 encode_ch (enc, '\n'); 336 encode_ch (enc, '\n');
293 } 337 }
294} 338}
295 339
296inline void 340INLINE void
297encode_comma (enc_t *enc) 341encode_comma (enc_t *enc)
298{ 342{
299 encode_ch (enc, ','); 343 encode_ch (enc, ',');
300 344
301 if (enc->json.flags & F_INDENT) 345 if (enc->json.flags & F_INDENT)
312 int i, len = av_len (av); 356 int i, len = av_len (av);
313 357
314 if (enc->indent >= enc->maxdepth) 358 if (enc->indent >= enc->maxdepth)
315 croak ("data structure too deep (hit recursion limit)"); 359 croak ("data structure too deep (hit recursion limit)");
316 360
317 encode_ch (enc, '['); encode_nl (enc); 361 encode_ch (enc, '[');
318 ++enc->indent; 362
363 if (len >= 0)
364 {
365 encode_nl (enc); ++enc->indent;
319 366
320 for (i = 0; i <= len; ++i) 367 for (i = 0; i <= len; ++i)
321 { 368 {
322 SV **svp = av_fetch (av, i, 0); 369 SV **svp = av_fetch (av, i, 0);
323 370
324 encode_indent (enc); 371 encode_indent (enc);
325 372
326 if (svp) 373 if (svp)
327 encode_sv (enc, *svp); 374 encode_sv (enc, *svp);
328 else 375 else
329 encode_str (enc, "null", 4, 0); 376 encode_str (enc, "null", 4, 0);
330 377
331 if (i < len) 378 if (i < len)
332 encode_comma (enc); 379 encode_comma (enc);
333 } 380 }
334 381
382 encode_nl (enc); --enc->indent; encode_indent (enc);
383 }
384
335 encode_nl (enc); 385 encode_ch (enc, ']');
336
337 --enc->indent;
338 encode_indent (enc); encode_ch (enc, ']');
339} 386}
340 387
341static void 388static void
342encode_hk (enc_t *enc, HE *he) 389encode_hk (enc_t *enc, HE *he)
343{ 390{
391 438
392static void 439static void
393encode_hv (enc_t *enc, HV *hv) 440encode_hv (enc_t *enc, HV *hv)
394{ 441{
395 HE *he; 442 HE *he;
396 int count;
397 443
398 if (enc->indent >= enc->maxdepth) 444 if (enc->indent >= enc->maxdepth)
399 croak ("data structure too deep (hit recursion limit)"); 445 croak ("data structure too deep (hit recursion limit)");
400 446
401 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 447 encode_ch (enc, '{');
402 448
403 // for canonical output we have to sort by keys first 449 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 450 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 451 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 452 // that randomises hash orderings
459 505
460 FREETMPS; 506 FREETMPS;
461 LEAVE; 507 LEAVE;
462 } 508 }
463 509
510 encode_nl (enc); ++enc->indent;
511
464 while (count--) 512 while (count--)
465 { 513 {
466 encode_indent (enc); 514 encode_indent (enc);
467 he = hes [count]; 515 he = hes [count];
468 encode_hk (enc, he); 516 encode_hk (enc, he);
469 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 517 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
470 518
471 if (count) 519 if (count)
472 encode_comma (enc); 520 encode_comma (enc);
473 } 521 }
522
523 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 524 }
475 } 525 }
476 else 526 else
477 { 527 {
478 if (hv_iterinit (hv) || SvMAGICAL (hv)) 528 if (hv_iterinit (hv) || SvMAGICAL (hv))
479 if ((he = hv_iternext (hv))) 529 if ((he = hv_iternext (hv)))
530 {
531 encode_nl (enc); ++enc->indent;
532
480 for (;;) 533 for (;;)
481 { 534 {
482 encode_indent (enc); 535 encode_indent (enc);
483 encode_hk (enc, he); 536 encode_hk (enc, he);
484 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 537 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
485 538
486 if (!(he = hv_iternext (hv))) 539 if (!(he = hv_iternext (hv)))
487 break; 540 break;
488 541
489 encode_comma (enc); 542 encode_comma (enc);
490 } 543 }
491 }
492 544
545 encode_nl (enc); --enc->indent; encode_indent (enc);
546 }
547 }
548
493 encode_nl (enc); 549 encode_ch (enc, '}');
494
495 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
496} 550}
497 551
498// encode objects, arrays and special \0=false and \1=true values. 552// encode objects, arrays and special \0=false and \1=true values.
499static void 553static void
500encode_rv (enc_t *enc, SV *sv) 554encode_rv (enc_t *enc, SV *sv)
608 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 662 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
609 enc->cur += strlen (enc->cur); 663 enc->cur += strlen (enc->cur);
610 } 664 }
611 else if (SvIOKp (sv)) 665 else if (SvIOKp (sv))
612 { 666 {
613 // we assume we can always read an IV as a UV 667 // we assume we can always read an IV as a UV and vice versa
614 if (SvUV (sv) & ~(UV)0x7fff) 668 // we assume two's complement
615 { 669 // we assume no aliasing issues in the union
616 // large integer, use the (rather slow) snprintf way. 670 if (SvIsUV (sv) ? SvUVX (sv) <= 59000
617 need (enc, sizeof (UV) * 3); 671 : 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 { 672 {
625 // optimise the "small number case" 673 // optimise the "small number case"
626 // code will likely be branchless and use only a single multiplication 674 // code will likely be branchless and use only a single multiplication
675 // works for numbers up to 59074
627 I32 i = SvIV (sv); 676 I32 i = SvIVX (sv);
628 U32 u; 677 U32 u;
629 char digit, nz = 0; 678 char digit, nz = 0;
630 679
631 need (enc, 6); 680 need (enc, 6);
632 681
638 687
639 // now output digit by digit, each time masking out the integer part 688 // 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, 689 // and multiplying by 5 while moving the decimal point one to the right,
641 // resulting in a net multiplication by 10. 690 // resulting in a net multiplication by 10.
642 // we always write the digit to memory but conditionally increment 691 // we always write the digit to memory but conditionally increment
643 // the pointer, to ease the usage of conditional move instructions. 692 // 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; 693 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; 694 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; 695 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; 696 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' 697 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
698 }
699 else
700 {
701 // large integer, use the (rather slow) snprintf way.
702 need (enc, IVUV_MAXCHARS);
703 enc->cur +=
704 SvIsUV(sv)
705 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
706 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
649 } 707 }
650 } 708 }
651 else if (SvROK (sv)) 709 else if (SvROK (sv))
652 encode_rv (enc, SvRV (sv)); 710 encode_rv (enc, SvRV (sv));
653 else if (!SvOK (sv)) 711 else if (!SvOK (sv))
669 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 727 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
670 enc.cur = SvPVX (enc.sv); 728 enc.cur = SvPVX (enc.sv);
671 enc.end = SvEND (enc.sv); 729 enc.end = SvEND (enc.sv);
672 enc.indent = 0; 730 enc.indent = 0;
673 enc.maxdepth = DEC_DEPTH (enc.json.flags); 731 enc.maxdepth = DEC_DEPTH (enc.json.flags);
732 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
733 : enc.json.flags & F_LATIN1 ? 0x000100UL
734 : 0x110000UL;
674 735
675 SvPOK_only (enc.sv); 736 SvPOK_only (enc.sv);
676 encode_sv (&enc, scalar); 737 encode_sv (&enc, scalar);
677 738
678 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 739 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
699 JSON json; 760 JSON json;
700 U32 depth; // recursion depth 761 U32 depth; // recursion depth
701 U32 maxdepth; // recursion depth limit 762 U32 maxdepth; // recursion depth limit
702} dec_t; 763} dec_t;
703 764
704inline void 765INLINE void
705decode_comment (dec_t *dec) 766decode_comment (dec_t *dec)
706{ 767{
707 // only '#'-style comments allowed a.t.m. 768 // only '#'-style comments allowed a.t.m.
708 769
709 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) 770 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
710 ++dec->cur; 771 ++dec->cur;
711} 772}
712 773
713inline void 774INLINE void
714decode_ws (dec_t *dec) 775decode_ws (dec_t *dec)
715{ 776{
716 for (;;) 777 for (;;)
717 { 778 {
718 char ch = *dec->cur; 779 char ch = *dec->cur;
844 905
845 if (hi >= 0x80) 906 if (hi >= 0x80)
846 { 907 {
847 utf8 = 1; 908 utf8 = 1;
848 909
849 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 910 cur = encode_utf8 (cur, hi);
850 } 911 }
851 else 912 else
852 *cur++ = hi; 913 *cur++ = hi;
853 } 914 }
854 break; 915 break;
856 default: 917 default:
857 --dec_cur; 918 --dec_cur;
858 ERR ("illegal backslash escape sequence in string"); 919 ERR ("illegal backslash escape sequence in string");
859 } 920 }
860 } 921 }
861 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 922 else if (expect_true (ch >= 0x20 && ch < 0x80))
862 *cur++ = ch; 923 *cur++ = ch;
863 else if (ch >= 0x80) 924 else if (ch >= 0x80)
864 { 925 {
865 STRLEN clen; 926 STRLEN clen;
866 UV uch; 927 UV uch;
989 1050
990 if (!is_nv) 1051 if (!is_nv)
991 { 1052 {
992 int len = dec->cur - start; 1053 int len = dec->cur - start;
993 1054
994 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1055 // special case the rather common 1..5-digit-int case
995 if (*start == '-') 1056 if (*start == '-')
996 switch (len) 1057 switch (len)
997 { 1058 {
998 case 2: return newSViv (-( start [1] - '0' * 1)); 1059 case 2: return newSViv (-( start [1] - '0' * 1));
999 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1060 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
1000 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1061 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1001 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1062 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1063 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1002 } 1064 }
1003 else 1065 else
1004 switch (len) 1066 switch (len)
1005 { 1067 {
1006 case 1: return newSViv ( start [0] - '0' * 1); 1068 case 1: return newSViv ( start [0] - '0' * 1);
1007 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1069 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1008 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1070 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1009 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1071 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1072 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1010 } 1073 }
1011 1074
1012 { 1075 {
1013 UV uv; 1076 UV uv;
1014 int numtype = grok_number (start, len, &uv); 1077 int numtype = grok_number (start, len, &uv);
1122 char *p = dec->cur; 1185 char *p = dec->cur;
1123 char *e = p + 24; // only try up to 24 bytes 1186 char *e = p + 24; // only try up to 24 bytes
1124 1187
1125 for (;;) 1188 for (;;)
1126 { 1189 {
1127 // the >= 0x80 is true on most architectures 1190 // the >= 0x80 is false on most architectures
1128 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') 1191 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
1129 { 1192 {
1130 // slow path, back up and use decode_str 1193 // slow path, back up and use decode_str
1131 SV *key = decode_str (dec); 1194 SV *key = decode_str (dec);
1132 if (!key) 1195 if (!key)
1263 1326
1264static SV * 1327static SV *
1265decode_sv (dec_t *dec) 1328decode_sv (dec_t *dec)
1266{ 1329{
1267 // the beauty of JSON: you need exactly one character lookahead 1330 // the beauty of JSON: you need exactly one character lookahead
1268 // to parse anything. 1331 // to parse everything.
1269 switch (*dec->cur) 1332 switch (*dec->cur)
1270 { 1333 {
1271 case '"': ++dec->cur; return decode_str (dec); 1334 case '"': ++dec->cur; return decode_str (dec);
1272 case '[': ++dec->cur; return decode_av (dec); 1335 case '[': ++dec->cur; return decode_av (dec);
1273 case '{': ++dec->cur; return decode_hv (dec); 1336 case '{': ++dec->cur; return decode_hv (dec);
1274 1337
1275 case '-': 1338 case '-':
1276 case '0': case '1': case '2': case '3': case '4': 1339 case '0': case '1': case '2': case '3': case '4':
1277 case '5': case '6': case '7': case '8': case '9': 1340 case '5': case '6': case '7': case '8': case '9':
1278 return decode_num (dec); 1341 return decode_num (dec);
1324fail: 1387fail:
1325 return 0; 1388 return 0;
1326} 1389}
1327 1390
1328static SV * 1391static SV *
1329decode_json (SV *string, JSON *json, UV *offset_return) 1392decode_json (SV *string, JSON *json, STRLEN *offset_return)
1330{ 1393{
1331 dec_t dec; 1394 dec_t dec;
1332 UV offset; 1395 STRLEN offset;
1333 SV *sv; 1396 SV *sv;
1334 1397
1335 SvGETMAGIC (string); 1398 SvGETMAGIC (string);
1336 SvUPGRADE (string, SVt_PV); 1399 SvUPGRADE (string, SVt_PV);
1337 1400
1410 1473
1411 return sv; 1474 return sv;
1412} 1475}
1413 1476
1414///////////////////////////////////////////////////////////////////////////// 1477/////////////////////////////////////////////////////////////////////////////
1478// incremental parser
1479
1480static void
1481incr_parse (JSON *self)
1482{
1483 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1484
1485 for (;;)
1486 {
1487 //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D
1488 switch (self->incr_mode)
1489 {
1490 // only used for intiial whitespace skipping
1491 case INCR_M_WS:
1492 for (;;)
1493 {
1494 if (*p > 0x20)
1495 {
1496 self->incr_mode = INCR_M_JSON;
1497 goto incr_m_json;
1498 }
1499 else if (!*p)
1500 goto interrupt;
1501
1502 ++p;
1503 }
1504
1505 // skip a single char inside a string (for \\-processing)
1506 case INCR_M_BS:
1507 if (!*p)
1508 goto interrupt;
1509
1510 ++p;
1511 self->incr_mode = INCR_M_STR;
1512 goto incr_m_str;
1513
1514 // inside a string
1515 case INCR_M_STR:
1516 incr_m_str:
1517 for (;;)
1518 {
1519 if (*p == '"')
1520 {
1521 ++p;
1522 self->incr_mode = INCR_M_JSON;
1523
1524 if (!self->incr_nest)
1525 goto interrupt;
1526
1527 goto incr_m_json;
1528 }
1529 else if (*p == '\\')
1530 {
1531 ++p; // "virtually" consumes character after \
1532
1533 if (!*p) // if at end of string we have to switch modes
1534 {
1535 self->incr_mode = INCR_M_BS;
1536 goto interrupt;
1537 }
1538 }
1539 else if (!*p)
1540 goto interrupt;
1541
1542 ++p;
1543 }
1544
1545 // after initial ws, outside string
1546 case INCR_M_JSON:
1547 incr_m_json:
1548 for (;;)
1549 {
1550 switch (*p++)
1551 {
1552 case 0:
1553 --p;
1554 goto interrupt;
1555
1556 case 0x09:
1557 case 0x0a:
1558 case 0x0d:
1559 case 0x20:
1560 if (!self->incr_nest)
1561 {
1562 --p; // do not eat the whitespace, let the next round do it
1563 goto interrupt;
1564 }
1565 break;
1566
1567 case '"':
1568 self->incr_mode = INCR_M_STR;
1569 goto incr_m_str;
1570
1571 case '[':
1572 case '{':
1573 ++self->incr_nest;
1574 break;
1575
1576 case ']':
1577 case '}':
1578 if (!--self->incr_nest)
1579 goto interrupt;
1580 }
1581 }
1582 }
1583
1584 modechange:
1585 ;
1586 }
1587
1588interrupt:
1589 self->incr_pos = p - SvPVX (self->incr_text);
1590 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1591}
1592
1593/////////////////////////////////////////////////////////////////////////////
1415// XS interface functions 1594// XS interface functions
1416 1595
1417MODULE = JSON::XS PACKAGE = JSON::XS 1596MODULE = JSON::XS PACKAGE = JSON::XS
1418 1597
1419BOOT: 1598BOOT:
1446{ 1625{
1447 SV *pv = NEWSV (0, sizeof (JSON)); 1626 SV *pv = NEWSV (0, sizeof (JSON));
1448 SvPOK_only (pv); 1627 SvPOK_only (pv);
1449 Zero (SvPVX (pv), 1, JSON); 1628 Zero (SvPVX (pv), 1, JSON);
1450 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1629 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1451 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1630 XPUSHs (sv_2mortal (sv_bless (
1631 newRV_noinc (pv),
1632 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1633 )));
1452} 1634}
1453 1635
1454void ascii (JSON *self, int enable = 1) 1636void ascii (JSON *self, int enable = 1)
1455 ALIAS: 1637 ALIAS:
1456 ascii = F_ASCII 1638 ascii = F_ASCII
1474 self->flags &= ~ix; 1656 self->flags &= ~ix;
1475 1657
1476 XPUSHs (ST (0)); 1658 XPUSHs (ST (0));
1477} 1659}
1478 1660
1661void get_ascii (JSON *self)
1662 ALIAS:
1663 get_ascii = F_ASCII
1664 get_latin1 = F_LATIN1
1665 get_utf8 = F_UTF8
1666 get_indent = F_INDENT
1667 get_canonical = F_CANONICAL
1668 get_space_before = F_SPACE_BEFORE
1669 get_space_after = F_SPACE_AFTER
1670 get_allow_nonref = F_ALLOW_NONREF
1671 get_shrink = F_SHRINK
1672 get_allow_blessed = F_ALLOW_BLESSED
1673 get_convert_blessed = F_CONV_BLESSED
1674 get_relaxed = F_RELAXED
1675 PPCODE:
1676 XPUSHs (boolSV (self->flags & ix));
1677
1479void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1678void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1480 PPCODE: 1679 PPCODE:
1481{ 1680{
1482 UV log2 = 0; 1681 UV log2 = 0;
1483 1682
1489 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1688 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1490 1689
1491 XPUSHs (ST (0)); 1690 XPUSHs (ST (0));
1492} 1691}
1493 1692
1693U32 get_max_depth (JSON *self)
1694 CODE:
1695 RETVAL = DEC_DEPTH (self->flags);
1696 OUTPUT:
1697 RETVAL
1698
1494void max_size (JSON *self, UV max_size = 0) 1699void max_size (JSON *self, UV max_size = 0)
1495 PPCODE: 1700 PPCODE:
1496{ 1701{
1497 UV log2 = 0; 1702 UV log2 = 0;
1498 1703
1504 1709
1505 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1710 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1506 1711
1507 XPUSHs (ST (0)); 1712 XPUSHs (ST (0));
1508} 1713}
1714
1715int get_max_size (JSON *self)
1716 CODE:
1717 RETVAL = DEC_SIZE (self->flags);
1718 OUTPUT:
1719 RETVAL
1509 1720
1510void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1721void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1511 PPCODE: 1722 PPCODE:
1512{ 1723{
1513 SvREFCNT_dec (self->cb_object); 1724 SvREFCNT_dec (self->cb_object);
1547 XPUSHs (decode_json (jsonstr, self, 0)); 1758 XPUSHs (decode_json (jsonstr, self, 0));
1548 1759
1549void decode_prefix (JSON *self, SV *jsonstr) 1760void decode_prefix (JSON *self, SV *jsonstr)
1550 PPCODE: 1761 PPCODE:
1551{ 1762{
1552 UV offset; 1763 STRLEN offset;
1553 EXTEND (SP, 2); 1764 EXTEND (SP, 2);
1554 PUSHs (decode_json (jsonstr, self, &offset)); 1765 PUSHs (decode_json (jsonstr, self, &offset));
1555 PUSHs (sv_2mortal (newSVuv (offset))); 1766 PUSHs (sv_2mortal (newSVuv (offset)));
1767}
1768
1769void incr_parse (JSON *self, SV *jsonstr = 0)
1770 PPCODE:
1771{
1772 if (!self->incr_text)
1773 self->incr_text = newSVpvn ("", 0);
1774
1775 // append data, if any
1776 if (jsonstr)
1777 {
1778 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text))
1779 {
1780 /* utf-8-ness differs, need to upgrade */
1781 sv_utf8_upgrade (self->incr_text);
1782
1783 if (self->incr_pos)
1784 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1785 - (U8 *)SvPVX (self->incr_text);
1786 }
1787
1788 {
1789 STRLEN len;
1790 const char *str = SvPV (jsonstr, len);
1791 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1);
1792 Move (str, SvEND (self->incr_text), len, char);
1793 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1794 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1795 }
1796 }
1797
1798 if (GIMME_V != G_VOID)
1799 do
1800 {
1801 STRLEN offset;
1802
1803 incr_parse (self);
1804
1805 if (!INCR_DONE (self))
1806 break;
1807
1808 XPUSHs (decode_json (self->incr_text, self, &offset));
1809
1810 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1811 self->incr_pos -= offset;
1812 self->incr_nest = 0;
1813 self->incr_mode = 0;
1814 }
1815 while (GIMME_V == G_ARRAY);
1816}
1817
1818SV *incr_text (JSON *self)
1819 ATTRS: lvalue
1820 CODE:
1821{
1822 if (self->incr_pos)
1823 croak ("incr_text can only be called after a successful incr_parse call in scalar context");
1824
1825 RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef;
1826}
1827 OUTPUT:
1828 RETVAL
1829
1830void incr_skip (JSON *self)
1831 CODE:
1832{
1833 if (!self->incr_pos || !INCR_DONE (self))
1834 croak ("incr_text can only be called after an unsuccessful incr_parse call in scalar context");//D
1835
1836 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos);
1837 self->incr_pos = 0;
1838 self->incr_nest = 0;
1839 self->incr_mode = 0;
1556} 1840}
1557 1841
1558void DESTROY (JSON *self) 1842void DESTROY (JSON *self)
1559 CODE: 1843 CODE:
1560 SvREFCNT_dec (self->cb_sk_object); 1844 SvREFCNT_dec (self->cb_sk_object);
1561 SvREFCNT_dec (self->cb_object); 1845 SvREFCNT_dec (self->cb_object);
1846 SvREFCNT_dec (self->incr_text);
1562 1847
1563PROTOTYPES: ENABLE 1848PROTOTYPES: ENABLE
1564 1849
1565void to_json (SV *scalar) 1850void encode_json (SV *scalar)
1851 ALIAS:
1852 to_json_ = 0
1853 encode_json = F_UTF8
1566 PPCODE: 1854 PPCODE:
1567{ 1855{
1568 JSON json = { F_DEFAULT | F_UTF8 }; 1856 JSON json = { F_DEFAULT | ix };
1569 XPUSHs (encode_json (scalar, &json)); 1857 XPUSHs (encode_json (scalar, &json));
1570} 1858}
1571 1859
1572void from_json (SV *jsonstr) 1860void decode_json (SV *jsonstr)
1861 ALIAS:
1862 from_json_ = 0
1863 decode_json = F_UTF8
1573 PPCODE: 1864 PPCODE:
1574{ 1865{
1575 JSON json = { F_DEFAULT | F_UTF8 }; 1866 JSON json = { F_DEFAULT | ix };
1576 XPUSHs (decode_json (jsonstr, &json, 0)); 1867 XPUSHs (decode_json (jsonstr, &json, 0));
1577} 1868}
1578 1869
1870

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines