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.100 by root, Sat Sep 5 23:00:56 2009 UTC vs.
Revision 1.118 by root, Fri Oct 25 20:27:57 2013 UTC

17// guarantees, though. if it breaks, you get to keep the pieces. 17// guarantees, though. if it breaks, you get to keep the pieces.
18#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
19# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
20#endif 20#endif
21 21
22// three extra for rounding, sign, and end of string
22#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) 23#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3)
23 24
24#define F_ASCII 0x00000001UL 25#define F_ASCII 0x00000001UL
25#define F_LATIN1 0x00000002UL 26#define F_LATIN1 0x00000002UL
26#define F_UTF8 0x00000004UL 27#define F_UTF8 0x00000004UL
27#define F_INDENT 0x00000008UL 28#define F_INDENT 0x00000008UL
41#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
42#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
43 44
44#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
45 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
46#define SB do { 49#define SB do {
47#define SE } while (0) 50#define SE } while (0)
48 51
49#if __GNUC__ >= 3 52#if __GNUC__ >= 3
50# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
69#else 72#else
70# define JSON_SLOW 0 73# define JSON_SLOW 0
71# define JSON_STASH json_stash 74# define JSON_STASH json_stash
72#endif 75#endif
73 76
77// the amount of HEs to allocate on the stack, when sorting keys
78#define STACK_HES 64
79
74static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
75static SV *json_true, *json_false; 81static SV *json_true, *json_false;
76 82
77enum { 83enum {
78 INCR_M_WS = 0, // initial whitespace skipping, must be 0 84 INCR_M_WS = 0, // initial whitespace skipping, must be 0
79 INCR_M_STR, // inside string 85 INCR_M_STR, // inside string
80 INCR_M_BS, // inside backslash 86 INCR_M_BS, // inside backslash
87 INCR_M_C0, // inside comment in initial whitespace sequence
88 INCR_M_C1, // inside comment in other places
81 INCR_M_JSON // outside anything, count nesting 89 INCR_M_JSON // outside anything, count nesting
82}; 90};
83 91
84#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) 92#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
85 93
184 return SvUTF8 (sv) 192 return SvUTF8 (sv)
185 ? utf8_distance (offset, SvPVX (sv)) 193 ? utf8_distance (offset, SvPVX (sv))
186 : offset - SvPVX (sv); 194 : offset - SvPVX (sv);
187} 195}
188 196
197/////////////////////////////////////////////////////////////////////////////
198// fp hell
199
200// scan a group of digits, and a trailing exponent
201static void
202json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
203{
204 UV uaccum = 0;
205 int eaccum = 0;
206
207 // if we recurse too deep, skip all remaining digits
208 // to avoid a stack overflow attack
209 if (expect_false (--maxdepth <= 0))
210 while (((U8)*s - '0') < 10)
211 ++s;
212
213 for (;;)
214 {
215 U8 dig = (U8)*s - '0';
216
217 if (expect_false (dig >= 10))
218 {
219 if (dig == (U8)((U8)'.' - (U8)'0'))
220 {
221 ++s;
222 json_atof_scan1 (s, accum, expo, 1, maxdepth);
223 }
224 else if ((dig | ' ') == 'e' - '0')
225 {
226 int exp2 = 0;
227 int neg = 0;
228
229 ++s;
230
231 if (*s == '-')
232 {
233 ++s;
234 neg = 1;
235 }
236 else if (*s == '+')
237 ++s;
238
239 while ((dig = (U8)*s - '0') < 10)
240 exp2 = exp2 * 10 + *s++ - '0';
241
242 *expo += neg ? -exp2 : exp2;
243 }
244
245 break;
246 }
247
248 ++s;
249
250 uaccum = uaccum * 10 + dig;
251 ++eaccum;
252
253 // if we have too many digits, then recurse for more
254 // we actually do this for rather few digits
255 if (uaccum >= (UV_MAX - 9) / 10)
256 {
257 if (postdp) *expo -= eaccum;
258 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
259 if (postdp) *expo += eaccum;
260
261 break;
262 }
263 }
264
265 // this relies greatly on the quality of the pow ()
266 // implementation of the platform, but a good
267 // implementation is hard to beat.
268 // (IEEE 754 conformant ones are required to be exact)
269 if (postdp) *expo -= eaccum;
270 *accum += uaccum * Perl_pow (10., *expo);
271 *expo += eaccum;
272}
273
274static NV
275json_atof (const char *s)
276{
277 NV accum = 0.;
278 int expo = 0;
279 int neg = 0;
280
281 if (*s == '-')
282 {
283 ++s;
284 neg = 1;
285 }
286
287 // a recursion depth of ten gives us >>500 bits
288 json_atof_scan1 (s, &accum, &expo, 0, 10);
289
290 return neg ? -accum : accum;
291}
189///////////////////////////////////////////////////////////////////////////// 292/////////////////////////////////////////////////////////////////////////////
190// encoder 293// encoder
191 294
192// structure used for encoding JSON 295// structure used for encoding JSON
193typedef struct 296typedef struct
380 483
381 if (enc->indent >= enc->json.max_depth) 484 if (enc->indent >= enc->json.max_depth)
382 croak (ERR_NESTING_EXCEEDED); 485 croak (ERR_NESTING_EXCEEDED);
383 486
384 encode_ch (enc, '['); 487 encode_ch (enc, '[');
385 488
386 if (len >= 0) 489 if (len >= 0)
387 { 490 {
388 encode_nl (enc); ++enc->indent; 491 encode_nl (enc); ++enc->indent;
389 492
390 for (i = 0; i <= len; ++i) 493 for (i = 0; i <= len; ++i)
402 encode_comma (enc); 505 encode_comma (enc);
403 } 506 }
404 507
405 encode_nl (enc); --enc->indent; encode_indent (enc); 508 encode_nl (enc); --enc->indent; encode_indent (enc);
406 } 509 }
407 510
408 encode_ch (enc, ']'); 511 encode_ch (enc, ']');
409} 512}
410 513
411static void 514static void
412encode_hk (enc_t *enc, HE *he) 515encode_hk (enc_t *enc, HE *he)
416 if (HeKLEN (he) == HEf_SVKEY) 519 if (HeKLEN (he) == HEf_SVKEY)
417 { 520 {
418 SV *sv = HeSVKEY (he); 521 SV *sv = HeSVKEY (he);
419 STRLEN len; 522 STRLEN len;
420 char *str; 523 char *str;
421 524
422 SvGETMAGIC (sv); 525 SvGETMAGIC (sv);
423 str = SvPV (sv, len); 526 str = SvPV (sv, len);
424 527
425 encode_str (enc, str, len, SvUTF8 (sv)); 528 encode_str (enc, str, len, SvUTF8 (sv));
426 } 529 }
492 } 595 }
493 596
494 if (count) 597 if (count)
495 { 598 {
496 int i, fast = 1; 599 int i, fast = 1;
497#if defined(__BORLANDC__) || defined(_MSC_VER) 600 HE *hes_stack [STACK_HES];
498 HE **hes = _alloca (count * sizeof (HE)); 601 HE **hes = hes_stack;
499#else 602
500 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 603 // allocate larger arrays on the heap
501#endif 604 if (count > STACK_HES)
605 {
606 SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes)));
607 hes = (HE **)SvPVX (sv);
608 }
502 609
503 i = 0; 610 i = 0;
504 while ((he = hv_iternext (hv))) 611 while ((he = hv_iternext (hv)))
505 { 612 {
506 hes [i++] = he; 613 hes [i++] = he;
725 } 832 }
726 else 833 else
727 { 834 {
728 // large integer, use the (rather slow) snprintf way. 835 // large integer, use the (rather slow) snprintf way.
729 need (enc, IVUV_MAXCHARS); 836 need (enc, IVUV_MAXCHARS);
730 enc->cur += 837 enc->cur +=
731 SvIsUV(sv) 838 SvIsUV(sv)
732 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) 839 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
733 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); 840 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
734 } 841 }
735 } 842 }
736 else if (SvROK (sv)) 843 else if (SvROK (sv))
737 encode_rv (enc, SvRV (sv)); 844 encode_rv (enc, SvRV (sv));
738 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 845 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
739 encode_str (enc, "null", 4, 0); 846 encode_str (enc, "null", 4, 0);
740 else 847 else
741 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 848 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data",
742 SvPV_nolen (sv), SvFLAGS (sv)); 849 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
743} 850}
744 851
745static SV * 852static SV *
746encode_json (SV *scalar, JSON *json) 853encode_json (SV *scalar, JSON *json)
747{ 854{
949 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1056 else if (expect_true (ch >= 0x20 && ch < 0x80))
950 *cur++ = ch; 1057 *cur++ = ch;
951 else if (ch >= 0x80) 1058 else if (ch >= 0x80)
952 { 1059 {
953 STRLEN clen; 1060 STRLEN clen;
954 UV uch;
955 1061
956 --dec_cur; 1062 --dec_cur;
957 1063
958 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1064 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
959 if (clen == (STRLEN)-1) 1065 if (clen == (STRLEN)-1)
960 ERR ("malformed UTF-8 character in JSON string"); 1066 ERR ("malformed UTF-8 character in JSON string");
961 1067
962 do 1068 do
963 *cur++ = *dec_cur++; 1069 *cur++ = *dec_cur++;
1117 } 1223 }
1118 1224
1119 len -= *start == '-' ? 1 : 0; 1225 len -= *start == '-' ? 1 : 0;
1120 1226
1121 // does not fit into IV or UV, try NV 1227 // does not fit into IV or UV, try NV
1122 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1228 if (len <= NV_DIG)
1123 #if defined (LDBL_DIG)
1124 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1125 #endif
1126 )
1127 // fits into NV without loss of precision 1229 // fits into NV without loss of precision
1128 return newSVnv (Atof (start)); 1230 return newSVnv (json_atof (start));
1129 1231
1130 // everything else fails, convert it to a string 1232 // everything else fails, convert it to a string
1131 return newSVpvn (start, dec->cur - start); 1233 return newSVpvn (start, dec->cur - start);
1132 } 1234 }
1133 1235
1134 // loss of precision here 1236 // loss of precision here
1135 return newSVnv (Atof (start)); 1237 return newSVnv (json_atof (start));
1136 1238
1137fail: 1239fail:
1138 return 0; 1240 return 0;
1139} 1241}
1140 1242
1164 if (*dec->cur == ']') 1266 if (*dec->cur == ']')
1165 { 1267 {
1166 ++dec->cur; 1268 ++dec->cur;
1167 break; 1269 break;
1168 } 1270 }
1169 1271
1170 if (*dec->cur != ',') 1272 if (*dec->cur != ',')
1171 ERR (", or ] expected while parsing array"); 1273 ERR (", or ] expected while parsing array");
1172 1274
1173 ++dec->cur; 1275 ++dec->cur;
1174 1276
1309 dSP; 1411 dSP;
1310 int count; 1412 int count;
1311 1413
1312 ENTER; SAVETMPS; PUSHMARK (SP); 1414 ENTER; SAVETMPS; PUSHMARK (SP);
1313 XPUSHs (HeVAL (he)); 1415 XPUSHs (HeVAL (he));
1416 sv_2mortal (sv);
1314 1417
1315 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1418 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1316 1419
1317 if (count == 1) 1420 if (count == 1)
1318 { 1421 {
1319 sv = newSVsv (POPs); 1422 sv = newSVsv (POPs);
1320 FREETMPS; LEAVE; 1423 FREETMPS; LEAVE;
1321 return sv; 1424 return sv;
1322 } 1425 }
1323 1426
1427 SvREFCNT_inc (sv);
1324 FREETMPS; LEAVE; 1428 FREETMPS; LEAVE;
1325 } 1429 }
1326 } 1430 }
1327 1431
1328 if (dec->json.cb_object) 1432 if (dec->json.cb_object)
1360{ 1464{
1361 // the beauty of JSON: you need exactly one character lookahead 1465 // the beauty of JSON: you need exactly one character lookahead
1362 // to parse everything. 1466 // to parse everything.
1363 switch (*dec->cur) 1467 switch (*dec->cur)
1364 { 1468 {
1365 case '"': ++dec->cur; return decode_str (dec); 1469 case '"': ++dec->cur; return decode_str (dec);
1366 case '[': ++dec->cur; return decode_av (dec); 1470 case '[': ++dec->cur; return decode_av (dec);
1367 case '{': ++dec->cur; return decode_hv (dec); 1471 case '{': ++dec->cur; return decode_hv (dec);
1368 1472
1369 case '-': 1473 case '-':
1370 case '0': case '1': case '2': case '3': case '4': 1474 case '0': case '1': case '2': case '3': case '4':
1371 case '5': case '6': case '7': case '8': case '9': 1475 case '5': case '6': case '7': case '8': case '9':
1424{ 1528{
1425 dec_t dec; 1529 dec_t dec;
1426 SV *sv; 1530 SV *sv;
1427 1531
1428 /* work around bugs in 5.10 where manipulating magic values 1532 /* work around bugs in 5.10 where manipulating magic values
1429 * will perl ignore the magic in subsequent accesses 1533 * makes perl ignore the magic in subsequent accesses.
1534 * also make a copy of non-PV values, to get them into a clean
1535 * state (SvPV should do that, but it's buggy, see below).
1430 */ 1536 */
1431 /*SvGETMAGIC (string);*/ 1537 /*SvGETMAGIC (string);*/
1432 if (SvMAGICAL (string)) 1538 if (SvMAGICAL (string) || !SvPOK (string))
1433 string = sv_2mortal (newSVsv (string)); 1539 string = sv_2mortal (newSVsv (string));
1434 1540
1435 SvUPGRADE (string, SVt_PV); 1541 SvUPGRADE (string, SVt_PV);
1436 1542
1437 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1543 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1454 if (offset > json->max_size && json->max_size) 1560 if (offset > json->max_size && json->max_size)
1455 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1561 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1456 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1562 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1457 } 1563 }
1458 1564
1459 if (json->flags & F_UTF8) 1565 if (DECODE_WANTS_OCTETS (json))
1460 sv_utf8_downgrade (string, 0); 1566 sv_utf8_downgrade (string, 0);
1461 else 1567 else
1462 sv_utf8_upgrade (string); 1568 sv_utf8_upgrade (string);
1463 1569
1464 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1570 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1506 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1612 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1507 LEAVE; 1613 LEAVE;
1508 1614
1509 croak ("%s, at character offset %d (before \"%s\")", 1615 croak ("%s, at character offset %d (before \"%s\")",
1510 dec.err, 1616 dec.err,
1511 ptr_to_index (string, dec.cur), 1617 (int)ptr_to_index (string, dec.cur),
1512 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1618 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1513 } 1619 }
1514 1620
1515 sv = sv_2mortal (sv); 1621 sv = sv_2mortal (sv);
1516 1622
1525 1631
1526static void 1632static void
1527incr_parse (JSON *self) 1633incr_parse (JSON *self)
1528{ 1634{
1529 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1635 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1636
1637 // the state machine here is a bit convoluted and could be simplified a lot
1638 // but this would make it slower, so...
1530 1639
1531 for (;;) 1640 for (;;)
1532 { 1641 {
1533 //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 1642 //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
1534 switch (self->incr_mode) 1643 switch (self->incr_mode)
1535 { 1644 {
1536 // only used for intiial whitespace skipping 1645 // only used for initial whitespace skipping
1537 case INCR_M_WS: 1646 case INCR_M_WS:
1538 for (;;) 1647 for (;;)
1539 { 1648 {
1540 if (*p > 0x20) 1649 if (*p > 0x20)
1541 { 1650 {
1651 if (*p == '#')
1652 {
1653 self->incr_mode = INCR_M_C0;
1654 goto incr_m_c;
1655 }
1656 else
1657 {
1542 self->incr_mode = INCR_M_JSON; 1658 self->incr_mode = INCR_M_JSON;
1543 goto incr_m_json; 1659 goto incr_m_json;
1660 }
1544 } 1661 }
1545 else if (!*p) 1662 else if (!*p)
1546 goto interrupt; 1663 goto interrupt;
1547 1664
1548 ++p; 1665 ++p;
1554 goto interrupt; 1671 goto interrupt;
1555 1672
1556 ++p; 1673 ++p;
1557 self->incr_mode = INCR_M_STR; 1674 self->incr_mode = INCR_M_STR;
1558 goto incr_m_str; 1675 goto incr_m_str;
1676
1677 // inside #-style comments
1678 case INCR_M_C0:
1679 case INCR_M_C1:
1680 incr_m_c:
1681 for (;;)
1682 {
1683 if (*p == '\n')
1684 {
1685 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1686 break;
1687 }
1688 else if (!*p)
1689 goto interrupt;
1690
1691 ++p;
1692 }
1693
1694 break;
1559 1695
1560 // inside a string 1696 // inside a string
1561 case INCR_M_STR: 1697 case INCR_M_STR:
1562 incr_m_str: 1698 incr_m_str:
1563 for (;;) 1699 for (;;)
1622 1758
1623 case ']': 1759 case ']':
1624 case '}': 1760 case '}':
1625 if (--self->incr_nest <= 0) 1761 if (--self->incr_nest <= 0)
1626 goto interrupt; 1762 goto interrupt;
1763 break;
1764
1765 case '#':
1766 self->incr_mode = INCR_M_C1;
1767 goto incr_m_c;
1627 } 1768 }
1628 } 1769 }
1629 } 1770 }
1630 1771
1631 modechange: 1772 modechange:
1632 ; 1773 ;
1633 } 1774 }
1634 1775
1635interrupt: 1776interrupt:
1636 self->incr_pos = p - SvPVX (self->incr_text); 1777 self->incr_pos = p - SvPVX (self->incr_text);
1778 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1637 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1779 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1638} 1780}
1639 1781
1640///////////////////////////////////////////////////////////////////////////// 1782/////////////////////////////////////////////////////////////////////////////
1641// XS interface functions 1783// XS interface functions
1670 json_boolean_stash = 0; 1812 json_boolean_stash = 0;
1671 1813
1672void new (char *klass) 1814void new (char *klass)
1673 PPCODE: 1815 PPCODE:
1674{ 1816{
1675 SV *pv = NEWSV (0, sizeof (JSON)); 1817 SV *pv = NEWSV (0, sizeof (JSON));
1676 SvPOK_only (pv); 1818 SvPOK_only (pv);
1677 json_init ((JSON *)SvPVX (pv)); 1819 json_init ((JSON *)SvPVX (pv));
1678 XPUSHs (sv_2mortal (sv_bless ( 1820 XPUSHs (sv_2mortal (sv_bless (
1679 newRV_noinc (pv), 1821 newRV_noinc (pv),
1680 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) 1822 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1757} 1899}
1758 1900
1759void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) 1901void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1760 PPCODE: 1902 PPCODE:
1761{ 1903{
1762 if (!self->cb_sk_object) 1904 if (!self->cb_sk_object)
1763 self->cb_sk_object = newHV (); 1905 self->cb_sk_object = newHV ();
1764 1906
1765 if (SvOK (cb)) 1907 if (SvOK (cb))
1766 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); 1908 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1767 else 1909 else
1778 XPUSHs (ST (0)); 1920 XPUSHs (ST (0));
1779} 1921}
1780 1922
1781void encode (JSON *self, SV *scalar) 1923void encode (JSON *self, SV *scalar)
1782 PPCODE: 1924 PPCODE:
1783 XPUSHs (encode_json (scalar, self)); 1925 PUTBACK; scalar = encode_json (scalar, self); SPAGAIN;
1926 XPUSHs (scalar);
1784 1927
1785void decode (JSON *self, SV *jsonstr) 1928void decode (JSON *self, SV *jsonstr)
1786 PPCODE: 1929 PPCODE:
1787 XPUSHs (decode_json (jsonstr, self, 0)); 1930 PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN;
1931 XPUSHs (jsonstr);
1788 1932
1789void decode_prefix (JSON *self, SV *jsonstr) 1933void decode_prefix (JSON *self, SV *jsonstr)
1790 PPCODE: 1934 PPCODE:
1791{ 1935{
1936 SV *sv;
1792 char *offset; 1937 char *offset;
1938 PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN;
1793 EXTEND (SP, 2); 1939 EXTEND (SP, 2);
1794 PUSHs (decode_json (jsonstr, self, &offset)); 1940 PUSHs (sv);
1795 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); 1941 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1796} 1942}
1797 1943
1798void incr_parse (JSON *self, SV *jsonstr = 0) 1944void incr_parse (JSON *self, SV *jsonstr = 0)
1799 PPCODE: 1945 PPCODE:
1800{ 1946{
1801 if (!self->incr_text) 1947 if (!self->incr_text)
1802 self->incr_text = newSVpvn ("", 0); 1948 self->incr_text = newSVpvn ("", 0);
1949
1950 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1951 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1952 if (DECODE_WANTS_OCTETS (self))
1953 {
1954 if (self->incr_pos)
1955 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1956 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1957
1958 sv_utf8_downgrade (self->incr_text, 0);
1959 }
1960 else
1961 {
1962 sv_utf8_upgrade (self->incr_text);
1963
1964 if (self->incr_pos)
1965 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1966 - (U8 *)SvPVX (self->incr_text);
1967 }
1803 1968
1804 // append data, if any 1969 // append data, if any
1805 if (jsonstr) 1970 if (jsonstr)
1806 { 1971 {
1972 /* make sure both strings have same encoding */
1973 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1807 if (SvUTF8 (jsonstr)) 1974 if (SvUTF8 (jsonstr))
1975 sv_utf8_downgrade (jsonstr, 0);
1808 { 1976 else
1809 if (!SvUTF8 (self->incr_text))
1810 {
1811 /* utf-8-ness differs, need to upgrade */
1812 sv_utf8_upgrade (self->incr_text);
1813
1814 if (self->incr_pos)
1815 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1816 - (U8 *)SvPVX (self->incr_text);
1817 }
1818 }
1819 else if (SvUTF8 (self->incr_text))
1820 sv_utf8_upgrade (jsonstr); 1977 sv_utf8_upgrade (jsonstr);
1821 1978
1979 /* and then just blindly append */
1822 { 1980 {
1823 STRLEN len; 1981 STRLEN len;
1824 const char *str = SvPV (jsonstr, len); 1982 const char *str = SvPV (jsonstr, len);
1825 STRLEN cur = SvCUR (self->incr_text); 1983 STRLEN cur = SvCUR (self->incr_text);
1826 1984
1834 } 1992 }
1835 1993
1836 if (GIMME_V != G_VOID) 1994 if (GIMME_V != G_VOID)
1837 do 1995 do
1838 { 1996 {
1997 SV *sv;
1839 char *offset; 1998 char *offset;
1840 1999
1841 if (!INCR_DONE (self)) 2000 if (!INCR_DONE (self))
1842 { 2001 {
1843 incr_parse (self); 2002 incr_parse (self);
1845 if (self->incr_pos > self->max_size && self->max_size) 2004 if (self->incr_pos > self->max_size && self->max_size)
1846 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 2005 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1847 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 2006 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1848 2007
1849 if (!INCR_DONE (self)) 2008 if (!INCR_DONE (self))
2009 {
2010 // as an optimisation, do not accumulate white space in the incr buffer
2011 if (self->incr_mode == INCR_M_WS && self->incr_pos)
2012 {
2013 self->incr_pos = 0;
2014 SvCUR_set (self->incr_text, 0);
2015 }
2016
1850 break; 2017 break;
2018 }
1851 } 2019 }
1852 2020
1853 XPUSHs (decode_json (self->incr_text, self, &offset)); 2021 PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN;
2022 XPUSHs (sv);
1854 2023
1855 self->incr_pos -= offset - SvPVX (self->incr_text); 2024 self->incr_pos -= offset - SvPVX (self->incr_text);
1856 self->incr_nest = 0; 2025 self->incr_nest = 0;
1857 self->incr_mode = 0; 2026 self->incr_mode = 0;
1858 2027
1902 SvREFCNT_dec (self->incr_text); 2071 SvREFCNT_dec (self->incr_text);
1903 2072
1904PROTOTYPES: ENABLE 2073PROTOTYPES: ENABLE
1905 2074
1906void encode_json (SV *scalar) 2075void encode_json (SV *scalar)
1907 ALIAS:
1908 to_json_ = 0
1909 encode_json = F_UTF8
1910 PPCODE: 2076 PPCODE:
1911{ 2077{
1912 JSON json; 2078 JSON json;
1913 json_init (&json); 2079 json_init (&json);
1914 json.flags |= ix; 2080 json.flags |= F_UTF8;
1915 XPUSHs (encode_json (scalar, &json)); 2081 PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN;
2082 XPUSHs (scalar);
1916} 2083}
1917 2084
1918void decode_json (SV *jsonstr) 2085void decode_json (SV *jsonstr)
1919 ALIAS:
1920 from_json_ = 0
1921 decode_json = F_UTF8
1922 PPCODE: 2086 PPCODE:
1923{ 2087{
1924 JSON json; 2088 JSON json;
1925 json_init (&json); 2089 json_init (&json);
1926 json.flags |= ix; 2090 json.flags |= F_UTF8;
1927 XPUSHs (decode_json (jsonstr, &json, 0)); 2091 PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN;
2092 XPUSHs (jsonstr);
1928} 2093}
1929 2094
1930

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines