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.83 by root, Wed Mar 26 22:54:38 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
27#define F_SPACE_AFTER 0x00000040UL 30#define F_SPACE_AFTER 0x00000040UL
28#define F_ALLOW_NONREF 0x00000100UL 31#define F_ALLOW_NONREF 0x00000100UL
29#define F_SHRINK 0x00000200UL 32#define F_SHRINK 0x00000200UL
30#define F_ALLOW_BLESSED 0x00000400UL 33#define F_ALLOW_BLESSED 0x00000400UL
31#define F_CONV_BLESSED 0x00000800UL 34#define F_CONV_BLESSED 0x00000800UL
35#define F_RELAXED 0x00001000UL
36
32#define F_MAXDEPTH 0xf8000000UL 37#define F_MAXDEPTH 0xf8000000UL
33#define S_MAXDEPTH 27 38#define S_MAXDEPTH 27
34#define F_MAXSIZE 0x01f00000UL 39#define F_MAXSIZE 0x01f00000UL
35#define S_MAXSIZE 20 40#define S_MAXSIZE 20
36#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 41#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
48 53
49#define SB do { 54#define SB do {
50#define SE } while (0) 55#define SE } while (0)
51 56
52#if __GNUC__ >= 3 57#if __GNUC__ >= 3
53# define expect(expr,value) __builtin_expect ((expr),(value)) 58# define expect(expr,value) __builtin_expect ((expr), (value))
54# define inline inline 59# define INLINE static inline
55#else 60#else
56# define expect(expr,value) (expr) 61# define expect(expr,value) (expr)
57# define inline static 62# define INLINE static
58#endif 63#endif
59 64
60#define expect_false(expr) expect ((expr) != 0, 0) 65#define expect_false(expr) expect ((expr) != 0, 0)
61#define expect_true(expr) expect ((expr) != 0, 1) 66#define expect_true(expr) expect ((expr) != 0, 1)
62 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)))
71
63#ifdef USE_ITHREADS 72#ifdef USE_ITHREADS
64# define JSON_SLOW 1 73# define JSON_SLOW 1
74# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
65#else 75#else
66# define JSON_SLOW 1 76# define JSON_SLOW 0
77# define JSON_STASH json_stash
67#endif 78#endif
68 79
69static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
70static SV *json_true, *json_false; 81static SV *json_true, *json_false;
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)
71 91
72typedef struct { 92typedef struct {
73 U32 flags; 93 U32 flags;
74 SV *cb_object; 94 SV *cb_object;
75 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;
76} JSON; 102} JSON;
77 103
78///////////////////////////////////////////////////////////////////////////// 104/////////////////////////////////////////////////////////////////////////////
79// utility functions 105// utility functions
80 106
81inline void 107INLINE SV *
108get_bool (const char *name)
109{
110 SV *sv = get_sv (name, 1);
111
112 SvREADONLY_on (sv);
113 SvREADONLY_on (SvRV (sv));
114
115 return sv;
116}
117
118INLINE void
82shrink (SV *sv) 119shrink (SV *sv)
83{ 120{
84 sv_utf8_downgrade (sv, 1); 121 sv_utf8_downgrade (sv, 1);
85 if (SvLEN (sv) > SvCUR (sv) + 1) 122 if (SvLEN (sv) > SvCUR (sv) + 1)
86 { 123 {
95// decode an utf-8 character and return it, or (UV)-1 in 132// decode an utf-8 character and return it, or (UV)-1 in
96// case of an error. 133// case of an error.
97// we special-case "safe" characters from U+80 .. U+7FF, 134// we special-case "safe" characters from U+80 .. U+7FF,
98// but use the very good perl function to parse anything else. 135// but use the very good perl function to parse anything else.
99// note that we never call this function for a ascii codepoints 136// note that we never call this function for a ascii codepoints
100inline UV 137INLINE UV
101decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 138decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
102{ 139{
103 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 140 if (expect_true (len >= 2
104 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 141 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
105 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 142 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
106 { 143 {
107 *clen = 2; 144 *clen = 2;
108 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 145 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
109 } 146 }
110 else 147 else
111 { 148 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
112 *clen = (STRLEN)-1; 149}
113 return (UV)-1; 150
114 } 151// likewise for encoding, also never called for ascii codepoints
152// this function takes advantage of this fact, although current gccs
153// seem to optimise the check for >= 0x80 away anyways
154INLINE unsigned char *
155encode_utf8 (unsigned char *s, UV ch)
156{
157 if (expect_false (ch < 0x000080))
158 *s++ = ch;
159 else if (expect_true (ch < 0x000800))
160 *s++ = 0xc0 | ( ch >> 6),
161 *s++ = 0x80 | ( ch & 0x3f);
162 else if ( ch < 0x010000)
163 *s++ = 0xe0 | ( ch >> 12),
164 *s++ = 0x80 | ((ch >> 6) & 0x3f),
165 *s++ = 0x80 | ( ch & 0x3f);
166 else if ( ch < 0x110000)
167 *s++ = 0xf0 | ( ch >> 18),
168 *s++ = 0x80 | ((ch >> 12) & 0x3f),
169 *s++ = 0x80 | ((ch >> 6) & 0x3f),
170 *s++ = 0x80 | ( ch & 0x3f);
171
172 return s;
115} 173}
116 174
117///////////////////////////////////////////////////////////////////////////// 175/////////////////////////////////////////////////////////////////////////////
118// encoder 176// encoder
119 177
124 char *end; // SvEND (sv) 182 char *end; // SvEND (sv)
125 SV *sv; // result scalar 183 SV *sv; // result scalar
126 JSON json; 184 JSON json;
127 U32 indent; // indentation level 185 U32 indent; // indentation level
128 U32 maxdepth; // max. indentation/recursion level 186 U32 maxdepth; // max. indentation/recursion level
187 UV limit; // escape character values >= this value when encoding
129} enc_t; 188} enc_t;
130 189
131inline void 190INLINE void
132need (enc_t *enc, STRLEN len) 191need (enc_t *enc, STRLEN len)
133{ 192{
134 if (expect_false (enc->cur + len >= enc->end)) 193 if (expect_false (enc->cur + len >= enc->end))
135 { 194 {
136 STRLEN cur = enc->cur - SvPVX (enc->sv); 195 STRLEN cur = enc->cur - SvPVX (enc->sv);
138 enc->cur = SvPVX (enc->sv) + cur; 197 enc->cur = SvPVX (enc->sv) + cur;
139 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 198 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
140 } 199 }
141} 200}
142 201
143inline void 202INLINE void
144encode_ch (enc_t *enc, char ch) 203encode_ch (enc_t *enc, char ch)
145{ 204{
146 need (enc, 1); 205 need (enc, 1);
147 *enc->cur++ = ch; 206 *enc->cur++ = ch;
148} 207}
202 { 261 {
203 uch = ch; 262 uch = ch;
204 clen = 1; 263 clen = 1;
205 } 264 }
206 265
207 if (uch > 0x10FFFFUL) 266 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 { 267 {
212 if (uch > 0xFFFFUL) 268 if (uch >= 0x10000UL)
213 { 269 {
270 if (uch >= 0x110000UL)
271 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
272
214 need (enc, len += 11); 273 need (enc, len += 11);
215 sprintf (enc->cur, "\\u%04x\\u%04x", 274 sprintf (enc->cur, "\\u%04x\\u%04x",
216 (int)((uch - 0x10000) / 0x400 + 0xD800), 275 (int)((uch - 0x10000) / 0x400 + 0xD800),
217 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 276 (int)((uch - 0x10000) % 0x400 + 0xDC00));
218 enc->cur += 12; 277 enc->cur += 12;
246 while (--clen); 305 while (--clen);
247 } 306 }
248 else 307 else
249 { 308 {
250 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 309 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
251 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 310 enc->cur = encode_utf8 (enc->cur, uch);
252 ++str; 311 ++str;
253 } 312 }
254 } 313 }
255 } 314 }
256 } 315 }
257 316
258 --len; 317 --len;
259 } 318 }
260} 319}
261 320
262inline void 321INLINE void
263encode_indent (enc_t *enc) 322encode_indent (enc_t *enc)
264{ 323{
265 if (enc->json.flags & F_INDENT) 324 if (enc->json.flags & F_INDENT)
266 { 325 {
267 int spaces = enc->indent * INDENT_STEP; 326 int spaces = enc->indent * INDENT_STEP;
270 memset (enc->cur, ' ', spaces); 329 memset (enc->cur, ' ', spaces);
271 enc->cur += spaces; 330 enc->cur += spaces;
272 } 331 }
273} 332}
274 333
275inline void 334INLINE void
276encode_space (enc_t *enc) 335encode_space (enc_t *enc)
277{ 336{
278 need (enc, 1); 337 need (enc, 1);
279 encode_ch (enc, ' '); 338 encode_ch (enc, ' ');
280} 339}
281 340
282inline void 341INLINE void
283encode_nl (enc_t *enc) 342encode_nl (enc_t *enc)
284{ 343{
285 if (enc->json.flags & F_INDENT) 344 if (enc->json.flags & F_INDENT)
286 { 345 {
287 need (enc, 1); 346 need (enc, 1);
288 encode_ch (enc, '\n'); 347 encode_ch (enc, '\n');
289 } 348 }
290} 349}
291 350
292inline void 351INLINE void
293encode_comma (enc_t *enc) 352encode_comma (enc_t *enc)
294{ 353{
295 encode_ch (enc, ','); 354 encode_ch (enc, ',');
296 355
297 if (enc->json.flags & F_INDENT) 356 if (enc->json.flags & F_INDENT)
308 int i, len = av_len (av); 367 int i, len = av_len (av);
309 368
310 if (enc->indent >= enc->maxdepth) 369 if (enc->indent >= enc->maxdepth)
311 croak ("data structure too deep (hit recursion limit)"); 370 croak ("data structure too deep (hit recursion limit)");
312 371
313 encode_ch (enc, '['); encode_nl (enc); 372 encode_ch (enc, '[');
314 ++enc->indent; 373
374 if (len >= 0)
375 {
376 encode_nl (enc); ++enc->indent;
315 377
316 for (i = 0; i <= len; ++i) 378 for (i = 0; i <= len; ++i)
317 { 379 {
318 SV **svp = av_fetch (av, i, 0); 380 SV **svp = av_fetch (av, i, 0);
319 381
320 encode_indent (enc); 382 encode_indent (enc);
321 383
322 if (svp) 384 if (svp)
323 encode_sv (enc, *svp); 385 encode_sv (enc, *svp);
324 else 386 else
325 encode_str (enc, "null", 4, 0); 387 encode_str (enc, "null", 4, 0);
326 388
327 if (i < len) 389 if (i < len)
328 encode_comma (enc); 390 encode_comma (enc);
329 } 391 }
330 392
393 encode_nl (enc); --enc->indent; encode_indent (enc);
394 }
395
331 encode_nl (enc); 396 encode_ch (enc, ']');
332
333 --enc->indent;
334 encode_indent (enc); encode_ch (enc, ']');
335} 397}
336 398
337static void 399static void
338encode_he (enc_t *enc, HE *he) 400encode_hk (enc_t *enc, HE *he)
339{ 401{
340 encode_ch (enc, '"'); 402 encode_ch (enc, '"');
341 403
342 if (HeKLEN (he) == HEf_SVKEY) 404 if (HeKLEN (he) == HEf_SVKEY)
343 { 405 {
356 encode_ch (enc, '"'); 418 encode_ch (enc, '"');
357 419
358 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 420 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
359 encode_ch (enc, ':'); 421 encode_ch (enc, ':');
360 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 422 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
361 encode_sv (enc, HeVAL (he));
362} 423}
363 424
364// compare hash entries, used when all keys are bytestrings 425// compare hash entries, used when all keys are bytestrings
365static int 426static int
366he_cmp_fast (const void *a_, const void *b_) 427he_cmp_fast (const void *a_, const void *b_)
371 HE *b = *(HE **)b_; 432 HE *b = *(HE **)b_;
372 433
373 STRLEN la = HeKLEN (a); 434 STRLEN la = HeKLEN (a);
374 STRLEN lb = HeKLEN (b); 435 STRLEN lb = HeKLEN (b);
375 436
376 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 437 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
377 cmp = la - lb; 438 cmp = lb - la;
378 439
379 return cmp; 440 return cmp;
380} 441}
381 442
382// compare hash entries, used when some keys are sv's or utf-x 443// compare hash entries, used when some keys are sv's or utf-x
383static int 444static int
384he_cmp_slow (const void *a, const void *b) 445he_cmp_slow (const void *a, const void *b)
385{ 446{
386 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 447 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
387} 448}
388 449
389static void 450static void
390encode_hv (enc_t *enc, HV *hv) 451encode_hv (enc_t *enc, HV *hv)
391{ 452{
392 int count, i; 453 HE *he;
393 454
394 if (enc->indent >= enc->maxdepth) 455 if (enc->indent >= enc->maxdepth)
395 croak ("data structure too deep (hit recursion limit)"); 456 croak ("data structure too deep (hit recursion limit)");
396 457
397 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 458 encode_ch (enc, '{');
398 459
399 if ((count = hv_iterinit (hv)))
400 {
401 // for canonical output we have to sort by keys first 460 // for canonical output we have to sort by keys first
402 // actually, this is mostly due to the stupid so-called 461 // actually, this is mostly due to the stupid so-called
403 // security workaround added somewhere in 5.8.x. 462 // security workaround added somewhere in 5.8.x.
404 // that randomises hash orderings 463 // that randomises hash orderings
405 if (enc->json.flags & F_CANONICAL) 464 if (enc->json.flags & F_CANONICAL)
465 {
466 int count = hv_iterinit (hv);
467
468 if (SvMAGICAL (hv))
406 { 469 {
470 // need to count by iterating. could improve by dynamically building the vector below
471 // but I don't care for the speed of this special case.
472 // note also that we will run into undefined behaviour when the two iterations
473 // do not result in the same count, something I might care for in some later release.
474
475 count = 0;
476 while (hv_iternext (hv))
477 ++count;
478
479 hv_iterinit (hv);
480 }
481
482 if (count)
483 {
407 int fast = 1; 484 int i, fast = 1;
408 HE *he;
409#if defined(__BORLANDC__) || defined(_MSC_VER) 485#if defined(__BORLANDC__) || defined(_MSC_VER)
410 HE **hes = _alloca (count * sizeof (HE)); 486 HE **hes = _alloca (count * sizeof (HE));
411#else 487#else
412 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 488 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
413#endif 489#endif
440 516
441 FREETMPS; 517 FREETMPS;
442 LEAVE; 518 LEAVE;
443 } 519 }
444 520
445 for (i = 0; i < count; ++i) 521 encode_nl (enc); ++enc->indent;
522
523 while (count--)
446 { 524 {
447 encode_indent (enc); 525 encode_indent (enc);
526 he = hes [count];
448 encode_he (enc, hes [i]); 527 encode_hk (enc, he);
528 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
449 529
450 if (i < count - 1) 530 if (count)
451 encode_comma (enc); 531 encode_comma (enc);
452 } 532 }
453 533
454 encode_nl (enc); 534 encode_nl (enc); --enc->indent; encode_indent (enc);
455 } 535 }
536 }
456 else 537 else
538 {
539 if (hv_iterinit (hv) || SvMAGICAL (hv))
540 if ((he = hv_iternext (hv)))
457 { 541 {
458 HE *he = hv_iternext (hv); 542 encode_nl (enc); ++enc->indent;
459 543
460 for (;;) 544 for (;;)
461 { 545 {
462 encode_indent (enc); 546 encode_indent (enc);
463 encode_he (enc, he); 547 encode_hk (enc, he);
548 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
464 549
465 if (!(he = hv_iternext (hv))) 550 if (!(he = hv_iternext (hv)))
466 break; 551 break;
467 552
468 encode_comma (enc); 553 encode_comma (enc);
469 } 554 }
470 555
471 encode_nl (enc); 556 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 557 }
473 } 558 }
474 559
475 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 560 encode_ch (enc, '}');
476} 561}
477 562
478// encode objects, arrays and special \0=false and \1=true values. 563// encode objects, arrays and special \0=false and \1=true values.
479static void 564static void
480encode_rv (enc_t *enc, SV *sv) 565encode_rv (enc_t *enc, SV *sv)
510 // we re-bless the reference to get overload and other niceties right 595 // we re-bless the reference to get overload and other niceties right
511 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 596 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
512 597
513 if (to_json) 598 if (to_json)
514 { 599 {
515 int count;
516 dSP; 600 dSP;
517 601
518 ENTER; SAVETMPS; PUSHMARK (SP); 602 ENTER; SAVETMPS; PUSHMARK (SP);
519 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 603 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
520 604
589 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 673 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
590 enc->cur += strlen (enc->cur); 674 enc->cur += strlen (enc->cur);
591 } 675 }
592 else if (SvIOKp (sv)) 676 else if (SvIOKp (sv))
593 { 677 {
594 // we assume we can always read an IV as a UV 678 // we assume we can always read an IV as a UV and vice versa
595 if (SvUV (sv) & ~(UV)0x7fff) 679 // we assume two's complement
596 { 680 // we assume no aliasing issues in the union
597 // large integer, use the (rather slow) snprintf way. 681 if (SvIsUV (sv) ? SvUVX (sv) <= 59000
598 need (enc, sizeof (UV) * 3); 682 : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000)
599 enc->cur +=
600 SvIsUV(sv)
601 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
602 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
603 }
604 else
605 { 683 {
606 // optimise the "small number case" 684 // optimise the "small number case"
607 // code will likely be branchless and use only a single multiplication 685 // code will likely be branchless and use only a single multiplication
686 // works for numbers up to 59074
608 I32 i = SvIV (sv); 687 I32 i = SvIVX (sv);
609 U32 u; 688 U32 u;
610 char digit, nz = 0; 689 char digit, nz = 0;
611 690
612 need (enc, 6); 691 need (enc, 6);
613 692
619 698
620 // now output digit by digit, each time masking out the integer part 699 // now output digit by digit, each time masking out the integer part
621 // and multiplying by 5 while moving the decimal point one to the right, 700 // and multiplying by 5 while moving the decimal point one to the right,
622 // resulting in a net multiplication by 10. 701 // resulting in a net multiplication by 10.
623 // we always write the digit to memory but conditionally increment 702 // we always write the digit to memory but conditionally increment
624 // the pointer, to ease the usage of conditional move instructions. 703 // the pointer, to enable the use of conditional move instructions.
625 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; 704 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5;
626 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; 705 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5;
627 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; 706 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5;
628 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; 707 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5;
629 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' 708 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
709 }
710 else
711 {
712 // large integer, use the (rather slow) snprintf way.
713 need (enc, IVUV_MAXCHARS);
714 enc->cur +=
715 SvIsUV(sv)
716 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
717 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
630 } 718 }
631 } 719 }
632 else if (SvROK (sv)) 720 else if (SvROK (sv))
633 encode_rv (enc, SvRV (sv)); 721 encode_rv (enc, SvRV (sv));
634 else if (!SvOK (sv)) 722 else if (!SvOK (sv))
650 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 738 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
651 enc.cur = SvPVX (enc.sv); 739 enc.cur = SvPVX (enc.sv);
652 enc.end = SvEND (enc.sv); 740 enc.end = SvEND (enc.sv);
653 enc.indent = 0; 741 enc.indent = 0;
654 enc.maxdepth = DEC_DEPTH (enc.json.flags); 742 enc.maxdepth = DEC_DEPTH (enc.json.flags);
743 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
744 : enc.json.flags & F_LATIN1 ? 0x000100UL
745 : 0x110000UL;
655 746
656 SvPOK_only (enc.sv); 747 SvPOK_only (enc.sv);
657 encode_sv (&enc, scalar); 748 encode_sv (&enc, scalar);
658 749
659 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 750 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
680 JSON json; 771 JSON json;
681 U32 depth; // recursion depth 772 U32 depth; // recursion depth
682 U32 maxdepth; // recursion depth limit 773 U32 maxdepth; // recursion depth limit
683} dec_t; 774} dec_t;
684 775
685inline void 776INLINE void
777decode_comment (dec_t *dec)
778{
779 // only '#'-style comments allowed a.t.m.
780
781 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
782 ++dec->cur;
783}
784
785INLINE void
686decode_ws (dec_t *dec) 786decode_ws (dec_t *dec)
687{ 787{
688 for (;;) 788 for (;;)
689 { 789 {
690 char ch = *dec->cur; 790 char ch = *dec->cur;
691 791
692 if (ch > 0x20 792 if (ch > 0x20)
793 {
794 if (expect_false (ch == '#'))
795 {
796 if (dec->json.flags & F_RELAXED)
797 decode_comment (dec);
798 else
799 break;
800 }
801 else
802 break;
803 }
693 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 804 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
694 break; 805 break; // parse error, but let higher level handle it, gives better error messages
695 806
696 ++dec->cur; 807 ++dec->cur;
697 } 808 }
698} 809}
699 810
805 916
806 if (hi >= 0x80) 917 if (hi >= 0x80)
807 { 918 {
808 utf8 = 1; 919 utf8 = 1;
809 920
810 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 921 cur = encode_utf8 (cur, hi);
811 } 922 }
812 else 923 else
813 *cur++ = hi; 924 *cur++ = hi;
814 } 925 }
815 break; 926 break;
817 default: 928 default:
818 --dec_cur; 929 --dec_cur;
819 ERR ("illegal backslash escape sequence in string"); 930 ERR ("illegal backslash escape sequence in string");
820 } 931 }
821 } 932 }
822 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 933 else if (expect_true (ch >= 0x20 && ch < 0x80))
823 *cur++ = ch; 934 *cur++ = ch;
824 else if (ch >= 0x80) 935 else if (ch >= 0x80)
825 { 936 {
826 STRLEN clen; 937 STRLEN clen;
827 UV uch; 938 UV uch;
950 1061
951 if (!is_nv) 1062 if (!is_nv)
952 { 1063 {
953 int len = dec->cur - start; 1064 int len = dec->cur - start;
954 1065
955 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1066 // special case the rather common 1..5-digit-int case
956 if (*start == '-') 1067 if (*start == '-')
957 switch (len) 1068 switch (len)
958 { 1069 {
959 case 2: return newSViv (-( start [1] - '0' * 1)); 1070 case 2: return newSViv (-( start [1] - '0' * 1));
960 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1071 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)); 1072 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)); 1073 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1074 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
963 } 1075 }
964 else 1076 else
965 switch (len) 1077 switch (len)
966 { 1078 {
967 case 1: return newSViv ( start [0] - '0' * 1); 1079 case 1: return newSViv ( start [0] - '0' * 1);
968 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1080 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); 1081 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); 1082 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1083 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
971 } 1084 }
972 1085
973 { 1086 {
974 UV uv; 1087 UV uv;
975 int numtype = grok_number (start, len, &uv); 1088 int numtype = grok_number (start, len, &uv);
1036 1149
1037 if (*dec->cur != ',') 1150 if (*dec->cur != ',')
1038 ERR (", or ] expected while parsing array"); 1151 ERR (", or ] expected while parsing array");
1039 1152
1040 ++dec->cur; 1153 ++dec->cur;
1154
1155 decode_ws (dec);
1156
1157 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1158 {
1159 ++dec->cur;
1160 break;
1161 }
1041 } 1162 }
1042 1163
1043 DEC_DEC_DEPTH; 1164 DEC_DEC_DEPTH;
1044 return newRV_noinc ((SV *)av); 1165 return newRV_noinc ((SV *)av);
1045 1166
1061 if (*dec->cur == '}') 1182 if (*dec->cur == '}')
1062 ++dec->cur; 1183 ++dec->cur;
1063 else 1184 else
1064 for (;;) 1185 for (;;)
1065 { 1186 {
1066 decode_ws (dec); EXPECT_CH ('"'); 1187 EXPECT_CH ('"');
1067 1188
1068 // heuristic: assume that 1189 // heuristic: assume that
1069 // a) decode_str + hv_store_ent are abysmally slow. 1190 // a) decode_str + hv_store_ent are abysmally slow.
1070 // b) most hash keys are short, simple ascii text. 1191 // b) most hash keys are short, simple ascii text.
1071 // => try to "fast-match" such strings to avoid 1192 // => try to "fast-match" such strings to avoid
1075 char *p = dec->cur; 1196 char *p = dec->cur;
1076 char *e = p + 24; // only try up to 24 bytes 1197 char *e = p + 24; // only try up to 24 bytes
1077 1198
1078 for (;;) 1199 for (;;)
1079 { 1200 {
1080 // the >= 0x80 is true on most architectures 1201 // the >= 0x80 is false on most architectures
1081 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') 1202 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
1082 { 1203 {
1083 // slow path, back up and use decode_str 1204 // slow path, back up and use decode_str
1084 SV *key = decode_str (dec); 1205 SV *key = decode_str (dec);
1085 if (!key) 1206 if (!key)
1086 goto fail; 1207 goto fail;
1087 1208
1088 decode_ws (dec); EXPECT_CH (':'); 1209 decode_ws (dec); EXPECT_CH (':');
1089 1210
1211 decode_ws (dec);
1090 value = decode_sv (dec); 1212 value = decode_sv (dec);
1091 if (!value) 1213 if (!value)
1092 { 1214 {
1093 SvREFCNT_dec (key); 1215 SvREFCNT_dec (key);
1094 goto fail; 1216 goto fail;
1106 int len = p - key; 1228 int len = p - key;
1107 dec->cur = p + 1; 1229 dec->cur = p + 1;
1108 1230
1109 decode_ws (dec); EXPECT_CH (':'); 1231 decode_ws (dec); EXPECT_CH (':');
1110 1232
1233 decode_ws (dec);
1111 value = decode_sv (dec); 1234 value = decode_sv (dec);
1112 if (!value) 1235 if (!value)
1113 goto fail; 1236 goto fail;
1114 1237
1115 hv_store (hv, key, len, value, 0); 1238 hv_store (hv, key, len, value, 0);
1131 1254
1132 if (*dec->cur != ',') 1255 if (*dec->cur != ',')
1133 ERR (", or } expected while parsing object/hash"); 1256 ERR (", or } expected while parsing object/hash");
1134 1257
1135 ++dec->cur; 1258 ++dec->cur;
1259
1260 decode_ws (dec);
1261
1262 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1263 {
1264 ++dec->cur;
1265 break;
1266 }
1136 } 1267 }
1137 1268
1138 DEC_DEC_DEPTH; 1269 DEC_DEC_DEPTH;
1139 sv = newRV_noinc ((SV *)hv); 1270 sv = newRV_noinc ((SV *)hv);
1140 1271
1205} 1336}
1206 1337
1207static SV * 1338static SV *
1208decode_sv (dec_t *dec) 1339decode_sv (dec_t *dec)
1209{ 1340{
1210 decode_ws (dec);
1211
1212 // the beauty of JSON: you need exactly one character lookahead 1341 // the beauty of JSON: you need exactly one character lookahead
1213 // to parse anything. 1342 // to parse everything.
1214 switch (*dec->cur) 1343 switch (*dec->cur)
1215 { 1344 {
1216 case '"': ++dec->cur; return decode_str (dec); 1345 case '"': ++dec->cur; return decode_str (dec);
1217 case '[': ++dec->cur; return decode_av (dec); 1346 case '[': ++dec->cur; return decode_av (dec);
1218 case '{': ++dec->cur; return decode_hv (dec); 1347 case '{': ++dec->cur; return decode_hv (dec);
1219 1348
1220 case '-': 1349 case '-':
1221 case '0': case '1': case '2': case '3': case '4': 1350 case '0': case '1': case '2': case '3': case '4':
1222 case '5': case '6': case '7': case '8': case '9': 1351 case '5': case '6': case '7': case '8': case '9':
1223 return decode_num (dec); 1352 return decode_num (dec);
1224 1353
1225 case 't': 1354 case 't':
1226 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1355 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1227 { 1356 {
1228 dec->cur += 4; 1357 dec->cur += 4;
1358#if JSON_SLOW
1359 json_true = get_bool ("JSON::XS::true");
1360#endif
1229 return SvREFCNT_inc (json_true); 1361 return newSVsv (json_true);
1230 } 1362 }
1231 else 1363 else
1232 ERR ("'true' expected"); 1364 ERR ("'true' expected");
1233 1365
1234 break; 1366 break;
1235 1367
1236 case 'f': 1368 case 'f':
1237 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1369 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1238 { 1370 {
1239 dec->cur += 5; 1371 dec->cur += 5;
1372#if JSON_SLOW
1373 json_false = get_bool ("JSON::XS::false");
1374#endif
1240 return SvREFCNT_inc (json_false); 1375 return newSVsv (json_false);
1241 } 1376 }
1242 else 1377 else
1243 ERR ("'false' expected"); 1378 ERR ("'false' expected");
1244 1379
1245 break; 1380 break;
1263fail: 1398fail:
1264 return 0; 1399 return 0;
1265} 1400}
1266 1401
1267static SV * 1402static SV *
1268decode_json (SV *string, JSON *json, UV *offset_return) 1403decode_json (SV *string, JSON *json, STRLEN *offset_return)
1269{ 1404{
1270 dec_t dec; 1405 dec_t dec;
1271 UV offset; 1406 STRLEN offset;
1272 SV *sv; 1407 SV *sv;
1273 1408
1274 SvGETMAGIC (string); 1409 SvGETMAGIC (string);
1275 SvUPGRADE (string, SVt_PV); 1410 SvUPGRADE (string, SVt_PV);
1276 1411
1294 1429
1295 if (dec.json.cb_object || dec.json.cb_sk_object) 1430 if (dec.json.cb_object || dec.json.cb_sk_object)
1296 dec.json.flags |= F_HOOK; 1431 dec.json.flags |= F_HOOK;
1297 1432
1298 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1433 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1434
1435 decode_ws (&dec);
1299 sv = decode_sv (&dec); 1436 sv = decode_sv (&dec);
1300 1437
1301 if (!(offset_return || !sv)) 1438 if (!(offset_return || !sv))
1302 { 1439 {
1303 // check for trailing garbage 1440 // check for trailing garbage
1347 1484
1348 return sv; 1485 return sv;
1349} 1486}
1350 1487
1351///////////////////////////////////////////////////////////////////////////// 1488/////////////////////////////////////////////////////////////////////////////
1489// incremental parser
1490
1491static void
1492incr_parse (JSON *self)
1493{
1494 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1495
1496 for (;;)
1497 {
1498 //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
1499 switch (self->incr_mode)
1500 {
1501 // only used for intiial whitespace skipping
1502 case INCR_M_WS:
1503 for (;;)
1504 {
1505 if (*p > 0x20)
1506 {
1507 self->incr_mode = INCR_M_JSON;
1508 goto incr_m_json;
1509 }
1510 else if (!*p)
1511 goto interrupt;
1512
1513 ++p;
1514 }
1515
1516 // skip a single char inside a string (for \\-processing)
1517 case INCR_M_BS:
1518 if (!*p)
1519 goto interrupt;
1520
1521 ++p;
1522 self->incr_mode = INCR_M_STR;
1523 goto incr_m_str;
1524
1525 // inside a string
1526 case INCR_M_STR:
1527 incr_m_str:
1528 for (;;)
1529 {
1530 if (*p == '"')
1531 {
1532 ++p;
1533 self->incr_mode = INCR_M_JSON;
1534
1535 if (!self->incr_nest)
1536 goto interrupt;
1537
1538 goto incr_m_json;
1539 }
1540 else if (*p == '\\')
1541 {
1542 ++p; // "virtually" consumes character after \
1543
1544 if (!*p) // if at end of string we have to switch modes
1545 {
1546 self->incr_mode = INCR_M_BS;
1547 goto interrupt;
1548 }
1549 }
1550 else if (!*p)
1551 goto interrupt;
1552
1553 ++p;
1554 }
1555
1556 // after initial ws, outside string
1557 case INCR_M_JSON:
1558 incr_m_json:
1559 for (;;)
1560 {
1561 switch (*p++)
1562 {
1563 case 0:
1564 --p;
1565 goto interrupt;
1566
1567 case 0x09:
1568 case 0x0a:
1569 case 0x0d:
1570 case 0x20:
1571 if (!self->incr_nest)
1572 {
1573 --p; // do not eat the whitespace, let the next round do it
1574 goto interrupt;
1575 }
1576 break;
1577
1578 case '"':
1579 self->incr_mode = INCR_M_STR;
1580 goto incr_m_str;
1581
1582 case '[':
1583 case '{':
1584 ++self->incr_nest;
1585 break;
1586
1587 case ']':
1588 case '}':
1589 if (!--self->incr_nest)
1590 goto interrupt;
1591 }
1592 }
1593 }
1594
1595 modechange:
1596 ;
1597 }
1598
1599interrupt:
1600 self->incr_pos = p - SvPVX (self->incr_text);
1601 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1602}
1603
1604/////////////////////////////////////////////////////////////////////////////
1352// XS interface functions 1605// XS interface functions
1353 1606
1354MODULE = JSON::XS PACKAGE = JSON::XS 1607MODULE = JSON::XS PACKAGE = JSON::XS
1355 1608
1356BOOT: 1609BOOT:
1365 : -1; 1618 : -1;
1366 1619
1367 json_stash = gv_stashpv ("JSON::XS" , 1); 1620 json_stash = gv_stashpv ("JSON::XS" , 1);
1368 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1621 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1369 1622
1370 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); 1623 json_true = get_bool ("JSON::XS::true");
1371 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1624 json_false = get_bool ("JSON::XS::false");
1372} 1625}
1373 1626
1374PROTOTYPES: DISABLE 1627PROTOTYPES: DISABLE
1375 1628
1376void CLONE (...) 1629void CLONE (...)
1379 json_boolean_stash = 0; 1632 json_boolean_stash = 0;
1380 1633
1381void new (char *klass) 1634void new (char *klass)
1382 PPCODE: 1635 PPCODE:
1383{ 1636{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1387 SV *pv = NEWSV (0, sizeof (JSON)); 1637 SV *pv = NEWSV (0, sizeof (JSON));
1388 SvPOK_only (pv); 1638 SvPOK_only (pv);
1389 Zero (SvPVX (pv), 1, JSON); 1639 Zero (SvPVX (pv), 1, JSON);
1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1640 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); 1641 XPUSHs (sv_2mortal (sv_bless (
1642 newRV_noinc (pv),
1643 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1644 )));
1392} 1645}
1393 1646
1394void ascii (JSON *self, int enable = 1) 1647void ascii (JSON *self, int enable = 1)
1395 ALIAS: 1648 ALIAS:
1396 ascii = F_ASCII 1649 ascii = F_ASCII
1403 pretty = F_PRETTY 1656 pretty = F_PRETTY
1404 allow_nonref = F_ALLOW_NONREF 1657 allow_nonref = F_ALLOW_NONREF
1405 shrink = F_SHRINK 1658 shrink = F_SHRINK
1406 allow_blessed = F_ALLOW_BLESSED 1659 allow_blessed = F_ALLOW_BLESSED
1407 convert_blessed = F_CONV_BLESSED 1660 convert_blessed = F_CONV_BLESSED
1661 relaxed = F_RELAXED
1408 PPCODE: 1662 PPCODE:
1409{ 1663{
1410 if (enable) 1664 if (enable)
1411 self->flags |= ix; 1665 self->flags |= ix;
1412 else 1666 else
1413 self->flags &= ~ix; 1667 self->flags &= ~ix;
1414 1668
1415 XPUSHs (ST (0)); 1669 XPUSHs (ST (0));
1416} 1670}
1417 1671
1672void get_ascii (JSON *self)
1673 ALIAS:
1674 get_ascii = F_ASCII
1675 get_latin1 = F_LATIN1
1676 get_utf8 = F_UTF8
1677 get_indent = F_INDENT
1678 get_canonical = F_CANONICAL
1679 get_space_before = F_SPACE_BEFORE
1680 get_space_after = F_SPACE_AFTER
1681 get_allow_nonref = F_ALLOW_NONREF
1682 get_shrink = F_SHRINK
1683 get_allow_blessed = F_ALLOW_BLESSED
1684 get_convert_blessed = F_CONV_BLESSED
1685 get_relaxed = F_RELAXED
1686 PPCODE:
1687 XPUSHs (boolSV (self->flags & ix));
1688
1418void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1689void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1419 PPCODE: 1690 PPCODE:
1420{ 1691{
1421 UV log2 = 0; 1692 UV log2 = 0;
1422 1693
1428 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1699 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1429 1700
1430 XPUSHs (ST (0)); 1701 XPUSHs (ST (0));
1431} 1702}
1432 1703
1704U32 get_max_depth (JSON *self)
1705 CODE:
1706 RETVAL = DEC_DEPTH (self->flags);
1707 OUTPUT:
1708 RETVAL
1709
1433void max_size (JSON *self, UV max_size = 0) 1710void max_size (JSON *self, UV max_size = 0)
1434 PPCODE: 1711 PPCODE:
1435{ 1712{
1436 UV log2 = 0; 1713 UV log2 = 0;
1437 1714
1443 1720
1444 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1721 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1445 1722
1446 XPUSHs (ST (0)); 1723 XPUSHs (ST (0));
1447} 1724}
1725
1726int get_max_size (JSON *self)
1727 CODE:
1728 RETVAL = DEC_SIZE (self->flags);
1729 OUTPUT:
1730 RETVAL
1448 1731
1449void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1732void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1450 PPCODE: 1733 PPCODE:
1451{ 1734{
1452 SvREFCNT_dec (self->cb_object); 1735 SvREFCNT_dec (self->cb_object);
1486 XPUSHs (decode_json (jsonstr, self, 0)); 1769 XPUSHs (decode_json (jsonstr, self, 0));
1487 1770
1488void decode_prefix (JSON *self, SV *jsonstr) 1771void decode_prefix (JSON *self, SV *jsonstr)
1489 PPCODE: 1772 PPCODE:
1490{ 1773{
1491 UV offset; 1774 STRLEN offset;
1492 EXTEND (SP, 2); 1775 EXTEND (SP, 2);
1493 PUSHs (decode_json (jsonstr, self, &offset)); 1776 PUSHs (decode_json (jsonstr, self, &offset));
1494 PUSHs (sv_2mortal (newSVuv (offset))); 1777 PUSHs (sv_2mortal (newSVuv (offset)));
1778}
1779
1780void incr_parse (JSON *self, SV *jsonstr = 0)
1781 PPCODE:
1782{
1783 if (!self->incr_text)
1784 self->incr_text = newSVpvn ("", 0);
1785
1786 // append data, if any
1787 if (jsonstr)
1788 {
1789 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text))
1790 {
1791 /* utf-8-ness differs, need to upgrade */
1792 sv_utf8_upgrade (self->incr_text);
1793
1794 if (self->incr_pos)
1795 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1796 - (U8 *)SvPVX (self->incr_text);
1797 }
1798
1799 {
1800 STRLEN len;
1801 const char *str = SvPV (jsonstr, len);
1802 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1);
1803 Move (str, SvEND (self->incr_text), len, char);
1804 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1805 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1806 }
1807 }
1808
1809 if (GIMME_V != G_VOID)
1810 do
1811 {
1812 STRLEN offset;
1813
1814 if (!INCR_DONE (self))
1815 {
1816 incr_parse (self);
1817 if (!INCR_DONE (self))
1818 break;
1819 }
1820
1821 XPUSHs (decode_json (self->incr_text, self, &offset));
1822
1823 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1824 self->incr_pos -= offset;
1825 self->incr_nest = 0;
1826 self->incr_mode = 0;
1827 }
1828 while (GIMME_V == G_ARRAY);
1829}
1830
1831SV *incr_text (JSON *self)
1832 ATTRS: lvalue
1833 CODE:
1834{
1835 if (self->incr_pos)
1836 croak ("incr_text can not be called when the incremental parser already started parsing");
1837
1838 RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef;
1839}
1840 OUTPUT:
1841 RETVAL
1842
1843void incr_skip (JSON *self)
1844 CODE:
1845{
1846 if (self->incr_pos)
1847 {
1848 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + self->incr_pos);
1849 self->incr_pos = 0;
1850 self->incr_nest = 0;
1851 self->incr_mode = 0;
1852 }
1495} 1853}
1496 1854
1497void DESTROY (JSON *self) 1855void DESTROY (JSON *self)
1498 CODE: 1856 CODE:
1499 SvREFCNT_dec (self->cb_sk_object); 1857 SvREFCNT_dec (self->cb_sk_object);
1500 SvREFCNT_dec (self->cb_object); 1858 SvREFCNT_dec (self->cb_object);
1859 SvREFCNT_dec (self->incr_text);
1501 1860
1502PROTOTYPES: ENABLE 1861PROTOTYPES: ENABLE
1503 1862
1504void to_json (SV *scalar) 1863void encode_json (SV *scalar)
1864 ALIAS:
1865 to_json_ = 0
1866 encode_json = F_UTF8
1505 PPCODE: 1867 PPCODE:
1506{ 1868{
1507 JSON json = { F_DEFAULT | F_UTF8 }; 1869 JSON json = { F_DEFAULT | ix };
1508 XPUSHs (encode_json (scalar, &json)); 1870 XPUSHs (encode_json (scalar, &json));
1509} 1871}
1510 1872
1511void from_json (SV *jsonstr) 1873void decode_json (SV *jsonstr)
1874 ALIAS:
1875 from_json_ = 0
1876 decode_json = F_UTF8
1512 PPCODE: 1877 PPCODE:
1513{ 1878{
1514 JSON json = { F_DEFAULT | F_UTF8 }; 1879 JSON json = { F_DEFAULT | ix };
1515 XPUSHs (decode_json (jsonstr, &json, 0)); 1880 XPUSHs (decode_json (jsonstr, &json, 0));
1516} 1881}
1517 1882
1883

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines