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.93 by root, Mon Sep 29 03:09:27 2008 UTC vs.
Revision 1.107 by root, Wed Mar 17 01:45:43 2010 UTC

12#if defined(__BORLANDC__) || defined(_MSC_VER) 12#if defined(__BORLANDC__) || defined(_MSC_VER)
13# define snprintf _snprintf // C compilers have this in stdio.h 13# define snprintf _snprintf // C compilers have this in stdio.h
14#endif 14#endif
15 15
16// 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
17// guarentees, 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
76 77
77enum { 78enum {
78 INCR_M_WS = 0, // initial whitespace skipping, must be 0 79 INCR_M_WS = 0, // initial whitespace skipping, must be 0
79 INCR_M_STR, // inside string 80 INCR_M_STR, // inside string
80 INCR_M_BS, // inside backslash 81 INCR_M_BS, // inside backslash
82 INCR_M_C0, // inside comment in initial whitespace sequence
83 INCR_M_C1, // inside comment in other places
81 INCR_M_JSON // outside anything, count nesting 84 INCR_M_JSON // outside anything, count nesting
82}; 85};
83 86
84#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) 87#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
85 88
175 *s++ = 0x80 | ( ch & 0x3f); 178 *s++ = 0x80 | ( ch & 0x3f);
176 179
177 return s; 180 return s;
178} 181}
179 182
183// convert offset pointer to character index, sv must be string
184static STRLEN
185ptr_to_index (SV *sv, char *offset)
186{
187 return SvUTF8 (sv)
188 ? utf8_distance (offset, SvPVX (sv))
189 : offset - SvPVX (sv);
190}
191
192/////////////////////////////////////////////////////////////////////////////
193// fp hell
194
195// scan a group of digits, and a trailing exponent
196static void
197json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
198{
199 UV uaccum = 0;
200 int eaccum = 0;
201
202 // if we recurse too deep, skip all remaining digits
203 // to avoid a stack overflow attack
204 if (expect_false (--maxdepth <= 0))
205 while (((U8)*s - '0') < 10)
206 ++s;
207
208 for (;;)
209 {
210 U8 dig = (U8)*s - '0';
211
212 if (expect_false (dig >= 10))
213 {
214 if (dig == (U8)((U8)'.' - (U8)'0'))
215 {
216 ++s;
217 json_atof_scan1 (s, accum, expo, 1, maxdepth);
218 }
219 else if ((dig | ' ') == 'e' - '0')
220 {
221 int exp2 = 0;
222 int neg = 0;
223
224 ++s;
225
226 if (*s == '-')
227 {
228 ++s;
229 neg = 1;
230 }
231 else if (*s == '+')
232 ++s;
233
234 while ((dig = (U8)*s - '0') < 10)
235 exp2 = exp2 * 10 + *s++ - '0';
236
237 *expo += neg ? -exp2 : exp2;
238 }
239
240 break;
241 }
242
243 ++s;
244
245 uaccum = uaccum * 10 + dig;
246 ++eaccum;
247
248 // if we have too many digits, then recurse for more
249 // we actually do this for rather few digits
250 if (uaccum >= (UV_MAX - 9) / 10)
251 {
252 if (postdp) *expo -= eaccum;
253 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
254 if (postdp) *expo += eaccum;
255
256 break;
257 }
258 }
259
260 // this relies greatly on the quality of the pow ()
261 // implementation of the platform, but a good
262 // implementation is hard to beat.
263 if (postdp) *expo -= eaccum;
264 *accum += uaccum * Perl_pow (10., *expo);
265 *expo += eaccum;
266}
267
268static NV
269json_atof (const char *s)
270{
271 NV accum = 0.;
272 int expo = 0;
273 int neg = 0;
274
275 if (*s == '-')
276 {
277 ++s;
278 neg = 1;
279 }
280
281 // a recursion depth of ten gives us >>500 bits
282 json_atof_scan1 (s, &accum, &expo, 0, 10);
283
284 return neg ? -accum : accum;
285}
180///////////////////////////////////////////////////////////////////////////// 286/////////////////////////////////////////////////////////////////////////////
181// encoder 287// encoder
182 288
183// structure used for encoding JSON 289// structure used for encoding JSON
184typedef struct 290typedef struct
195need (enc_t *enc, STRLEN len) 301need (enc_t *enc, STRLEN len)
196{ 302{
197 if (expect_false (enc->cur + len >= enc->end)) 303 if (expect_false (enc->cur + len >= enc->end))
198 { 304 {
199 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 305 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 306 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 307 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 308 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 309 }
204} 310}
205 311
462 568
463 // for canonical output we have to sort by keys first 569 // for canonical output we have to sort by keys first
464 // actually, this is mostly due to the stupid so-called 570 // actually, this is mostly due to the stupid so-called
465 // security workaround added somewhere in 5.8.x 571 // security workaround added somewhere in 5.8.x
466 // that randomises hash orderings 572 // that randomises hash orderings
467 if (enc->json.flags & F_CANONICAL) 573 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
468 { 574 {
469 int count = hv_iterinit (hv); 575 int count = hv_iterinit (hv);
470 576
471 if (SvMAGICAL (hv)) 577 if (SvMAGICAL (hv))
472 { 578 {
750 : enc.json.flags & F_LATIN1 ? 0x000100UL 856 : enc.json.flags & F_LATIN1 ? 0x000100UL
751 : 0x110000UL; 857 : 0x110000UL;
752 858
753 SvPOK_only (enc.sv); 859 SvPOK_only (enc.sv);
754 encode_sv (&enc, scalar); 860 encode_sv (&enc, scalar);
861 encode_nl (&enc);
755 862
756 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 863 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
757 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 864 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
758 865
759 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 866 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
939 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1046 else if (expect_true (ch >= 0x20 && ch < 0x80))
940 *cur++ = ch; 1047 *cur++ = ch;
941 else if (ch >= 0x80) 1048 else if (ch >= 0x80)
942 { 1049 {
943 STRLEN clen; 1050 STRLEN clen;
944 UV uch;
945 1051
946 --dec_cur; 1052 --dec_cur;
947 1053
948 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1054 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
949 if (clen == (STRLEN)-1) 1055 if (clen == (STRLEN)-1)
950 ERR ("malformed UTF-8 character in JSON string"); 1056 ERR ("malformed UTF-8 character in JSON string");
951 1057
952 do 1058 do
953 *cur++ = *dec_cur++; 1059 *cur++ = *dec_cur++;
970 { 1076 {
971 STRLEN len = cur - buf; 1077 STRLEN len = cur - buf;
972 1078
973 if (sv) 1079 if (sv)
974 { 1080 {
975 SvGROW (sv, SvCUR (sv) + len + 1); 1081 STRLEN cur = SvCUR (sv);
1082
1083 if (SvLEN (sv) <= cur + len)
1084 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1085
976 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 1086 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
977 SvCUR_set (sv, SvCUR (sv) + len); 1087 SvCUR_set (sv, SvCUR (sv) + len);
978 } 1088 }
979 else 1089 else
980 sv = newSVpvn (buf, len); 1090 sv = newSVpvn (buf, len);
1071 1181
1072 // special case the rather common 1..5-digit-int case 1182 // special case the rather common 1..5-digit-int case
1073 if (*start == '-') 1183 if (*start == '-')
1074 switch (len) 1184 switch (len)
1075 { 1185 {
1076 case 2: return newSViv (-( start [1] - '0' * 1)); 1186 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1077 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1187 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1078 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1188 case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1079 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1189 case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1080 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); 1190 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1081 } 1191 }
1082 else 1192 else
1083 switch (len) 1193 switch (len)
1084 { 1194 {
1085 case 1: return newSViv ( start [0] - '0' * 1); 1195 case 1: return newSViv ( start [0] - '0' * 1);
1086 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1196 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1087 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1197 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1088 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1198 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1089 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); 1199 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1090 } 1200 }
1091 1201
1092 { 1202 {
1093 UV uv; 1203 UV uv;
1094 int numtype = grok_number (start, len, &uv); 1204 int numtype = grok_number (start, len, &uv);
1103 } 1213 }
1104 1214
1105 len -= *start == '-' ? 1 : 0; 1215 len -= *start == '-' ? 1 : 0;
1106 1216
1107 // does not fit into IV or UV, try NV 1217 // does not fit into IV or UV, try NV
1108 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1218 if (len <= NV_DIG)
1109 #if defined (LDBL_DIG)
1110 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1111 #endif
1112 )
1113 // fits into NV without loss of precision 1219 // fits into NV without loss of precision
1114 return newSVnv (Atof (start)); 1220 return newSVnv (json_atof (start));
1115 1221
1116 // everything else fails, convert it to a string 1222 // everything else fails, convert it to a string
1117 return newSVpvn (start, dec->cur - start); 1223 return newSVpvn (start, dec->cur - start);
1118 } 1224 }
1119 1225
1120 // loss of precision here 1226 // loss of precision here
1121 return newSVnv (Atof (start)); 1227 return newSVnv (json_atof (start));
1122 1228
1123fail: 1229fail:
1124 return 0; 1230 return 0;
1125} 1231}
1126 1232
1295 dSP; 1401 dSP;
1296 int count; 1402 int count;
1297 1403
1298 ENTER; SAVETMPS; PUSHMARK (SP); 1404 ENTER; SAVETMPS; PUSHMARK (SP);
1299 XPUSHs (HeVAL (he)); 1405 XPUSHs (HeVAL (he));
1406 sv_2mortal (sv);
1300 1407
1301 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1408 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1302 1409
1303 if (count == 1) 1410 if (count == 1)
1304 { 1411 {
1305 sv = newSVsv (POPs); 1412 sv = newSVsv (POPs);
1306 FREETMPS; LEAVE; 1413 FREETMPS; LEAVE;
1307 return sv; 1414 return sv;
1308 } 1415 }
1309 1416
1417 SvREFCNT_inc (sv);
1310 FREETMPS; LEAVE; 1418 FREETMPS; LEAVE;
1311 } 1419 }
1312 } 1420 }
1313 1421
1314 if (dec->json.cb_object) 1422 if (dec->json.cb_object)
1404fail: 1512fail:
1405 return 0; 1513 return 0;
1406} 1514}
1407 1515
1408static SV * 1516static SV *
1409decode_json (SV *string, JSON *json, STRLEN *offset_return) 1517decode_json (SV *string, JSON *json, char **offset_return)
1410{ 1518{
1411 dec_t dec; 1519 dec_t dec;
1412 STRLEN offset;
1413 SV *sv; 1520 SV *sv;
1414 1521
1522 /* work around bugs in 5.10 where manipulating magic values
1523 * will perl ignore the magic in subsequent accesses
1524 */
1415 SvGETMAGIC (string); 1525 /*SvGETMAGIC (string);*/
1526 if (SvMAGICAL (string))
1527 string = sv_2mortal (newSVsv (string));
1528
1416 SvUPGRADE (string, SVt_PV); 1529 SvUPGRADE (string, SVt_PV);
1417 1530
1418 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1531 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1419 * assertion with -DDEBUGGING, although SvCUR is documented to 1532 * assertion with -DDEBUGGING, although SvCUR is documented to
1420 * return the xpv_cur field which certainly exists after upgrading. 1533 * return the xpv_cur field which certainly exists after upgrading.
1423 * and hope for the best. 1536 * and hope for the best.
1424 * Damnit, SvPV_nolen still trips over yet another assertion. This 1537 * Damnit, SvPV_nolen still trips over yet another assertion. This
1425 * assertion business is seriously broken, try yet another workaround 1538 * assertion business is seriously broken, try yet another workaround
1426 * for the broken -DDEBUGGING. 1539 * for the broken -DDEBUGGING.
1427 */ 1540 */
1541 {
1428#ifdef DEBUGGING 1542#ifdef DEBUGGING
1429 offset = SvOK (string) ? sv_len (string) : 0; 1543 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1430#else 1544#else
1431 offset = SvCUR (string); 1545 STRLEN offset = SvCUR (string);
1432#endif 1546#endif
1433 1547
1434 if (offset > json->max_size && json->max_size) 1548 if (offset > json->max_size && json->max_size)
1435 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1549 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1436 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1550 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1551 }
1437 1552
1438 if (json->flags & F_UTF8) 1553 if (json->flags & F_UTF8)
1439 sv_utf8_downgrade (string, 0); 1554 sv_utf8_downgrade (string, 0);
1440 else 1555 else
1441 sv_utf8_upgrade (string); 1556 sv_utf8_upgrade (string);
1453 1568
1454 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1569 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1455 1570
1456 decode_ws (&dec); 1571 decode_ws (&dec);
1457 sv = decode_sv (&dec); 1572 sv = decode_sv (&dec);
1573
1574 if (offset_return)
1575 *offset_return = dec.cur;
1458 1576
1459 if (!(offset_return || !sv)) 1577 if (!(offset_return || !sv))
1460 { 1578 {
1461 // check for trailing garbage 1579 // check for trailing garbage
1462 decode_ws (&dec); 1580 decode_ws (&dec);
1465 { 1583 {
1466 dec.err = "garbage after JSON object"; 1584 dec.err = "garbage after JSON object";
1467 SvREFCNT_dec (sv); 1585 SvREFCNT_dec (sv);
1468 sv = 0; 1586 sv = 0;
1469 } 1587 }
1470 }
1471
1472 if (offset_return || !sv)
1473 {
1474 offset = dec.json.flags & F_UTF8
1475 ? dec.cur - SvPVX (string)
1476 : utf8_distance (dec.cur, SvPVX (string));
1477
1478 if (offset_return)
1479 *offset_return = offset;
1480 } 1588 }
1481 1589
1482 if (!sv) 1590 if (!sv)
1483 { 1591 {
1484 SV *uni = sv_newmortal (); 1592 SV *uni = sv_newmortal ();
1490 SAVEVPTR (PL_curcop); 1598 SAVEVPTR (PL_curcop);
1491 PL_curcop = &cop; 1599 PL_curcop = &cop;
1492 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1600 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1493 LEAVE; 1601 LEAVE;
1494 1602
1495 croak ("%s, at character offset %d [\"%s\"]", 1603 croak ("%s, at character offset %d (before \"%s\")",
1496 dec.err, 1604 dec.err,
1497 (int)offset, 1605 ptr_to_index (string, dec.cur),
1498 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1606 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1499 } 1607 }
1500 1608
1501 sv = sv_2mortal (sv); 1609 sv = sv_2mortal (sv);
1502 1610
1511 1619
1512static void 1620static void
1513incr_parse (JSON *self) 1621incr_parse (JSON *self)
1514{ 1622{
1515 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1623 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1624
1625 // the state machine here is a bit convoluted and could be simplified a lot
1626 // but this would make it slower, so...
1516 1627
1517 for (;;) 1628 for (;;)
1518 { 1629 {
1519 //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 1630 //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
1520 switch (self->incr_mode) 1631 switch (self->incr_mode)
1521 { 1632 {
1522 // only used for intiial whitespace skipping 1633 // only used for initial whitespace skipping
1523 case INCR_M_WS: 1634 case INCR_M_WS:
1524 for (;;) 1635 for (;;)
1525 { 1636 {
1526 if (*p > 0x20) 1637 if (*p > 0x20)
1527 { 1638 {
1639 if (*p == '#')
1640 {
1641 self->incr_mode = INCR_M_C0;
1642 goto incr_m_c;
1643 }
1644 else
1645 {
1528 self->incr_mode = INCR_M_JSON; 1646 self->incr_mode = INCR_M_JSON;
1529 goto incr_m_json; 1647 goto incr_m_json;
1648 }
1530 } 1649 }
1531 else if (!*p) 1650 else if (!*p)
1532 goto interrupt; 1651 goto interrupt;
1533 1652
1534 ++p; 1653 ++p;
1540 goto interrupt; 1659 goto interrupt;
1541 1660
1542 ++p; 1661 ++p;
1543 self->incr_mode = INCR_M_STR; 1662 self->incr_mode = INCR_M_STR;
1544 goto incr_m_str; 1663 goto incr_m_str;
1664
1665 // inside #-style comments
1666 case INCR_M_C0:
1667 case INCR_M_C1:
1668 incr_m_c:
1669 for (;;)
1670 {
1671 if (*p == '\n')
1672 {
1673 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1674 break;
1675 }
1676 else if (!*p)
1677 goto interrupt;
1678
1679 ++p;
1680 }
1681
1682 break;
1545 1683
1546 // inside a string 1684 // inside a string
1547 case INCR_M_STR: 1685 case INCR_M_STR:
1548 incr_m_str: 1686 incr_m_str:
1549 for (;;) 1687 for (;;)
1608 1746
1609 case ']': 1747 case ']':
1610 case '}': 1748 case '}':
1611 if (--self->incr_nest <= 0) 1749 if (--self->incr_nest <= 0)
1612 goto interrupt; 1750 goto interrupt;
1751 break;
1752
1753 case '#':
1754 self->incr_mode = INCR_M_C1;
1755 goto incr_m_c;
1613 } 1756 }
1614 } 1757 }
1615 } 1758 }
1616 1759
1617 modechange: 1760 modechange:
1618 ; 1761 ;
1619 } 1762 }
1620 1763
1621interrupt: 1764interrupt:
1622 self->incr_pos = p - SvPVX (self->incr_text); 1765 self->incr_pos = p - SvPVX (self->incr_text);
1766 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1623 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1767 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1624} 1768}
1625 1769
1626///////////////////////////////////////////////////////////////////////////// 1770/////////////////////////////////////////////////////////////////////////////
1627// XS interface functions 1771// XS interface functions
1642 json_stash = gv_stashpv ("JSON::XS" , 1); 1786 json_stash = gv_stashpv ("JSON::XS" , 1);
1643 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1787 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1644 1788
1645 json_true = get_bool ("JSON::XS::true"); 1789 json_true = get_bool ("JSON::XS::true");
1646 json_false = get_bool ("JSON::XS::false"); 1790 json_false = get_bool ("JSON::XS::false");
1791
1792 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1647} 1793}
1648 1794
1649PROTOTYPES: DISABLE 1795PROTOTYPES: DISABLE
1650 1796
1651void CLONE (...) 1797void CLONE (...)
1771 XPUSHs (decode_json (jsonstr, self, 0)); 1917 XPUSHs (decode_json (jsonstr, self, 0));
1772 1918
1773void decode_prefix (JSON *self, SV *jsonstr) 1919void decode_prefix (JSON *self, SV *jsonstr)
1774 PPCODE: 1920 PPCODE:
1775{ 1921{
1776 STRLEN offset; 1922 char *offset;
1777 EXTEND (SP, 2); 1923 EXTEND (SP, 2);
1778 PUSHs (decode_json (jsonstr, self, &offset)); 1924 PUSHs (decode_json (jsonstr, self, &offset));
1779 PUSHs (sv_2mortal (newSVuv (offset))); 1925 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1780} 1926}
1781 1927
1782void incr_parse (JSON *self, SV *jsonstr = 0) 1928void incr_parse (JSON *self, SV *jsonstr = 0)
1783 PPCODE: 1929 PPCODE:
1784{ 1930{
1786 self->incr_text = newSVpvn ("", 0); 1932 self->incr_text = newSVpvn ("", 0);
1787 1933
1788 // append data, if any 1934 // append data, if any
1789 if (jsonstr) 1935 if (jsonstr)
1790 { 1936 {
1791 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1937 if (SvUTF8 (jsonstr))
1792 { 1938 {
1939 if (!SvUTF8 (self->incr_text))
1940 {
1793 /* utf-8-ness differs, need to upgrade */ 1941 /* utf-8-ness differs, need to upgrade */
1794 sv_utf8_upgrade (self->incr_text); 1942 sv_utf8_upgrade (self->incr_text);
1795 1943
1796 if (self->incr_pos) 1944 if (self->incr_pos)
1797 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1945 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1798 - (U8 *)SvPVX (self->incr_text); 1946 - (U8 *)SvPVX (self->incr_text);
1947 }
1799 } 1948 }
1949 else if (SvUTF8 (self->incr_text))
1950 sv_utf8_upgrade (jsonstr);
1800 1951
1801 { 1952 {
1802 STRLEN len; 1953 STRLEN len;
1803 const char *str = SvPV (jsonstr, len); 1954 const char *str = SvPV (jsonstr, len);
1804 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1955 STRLEN cur = SvCUR (self->incr_text);
1956
1957 if (SvLEN (self->incr_text) <= cur + len)
1958 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1959
1805 Move (str, SvEND (self->incr_text), len, char); 1960 Move (str, SvEND (self->incr_text), len, char);
1806 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1961 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1807 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1962 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1808 } 1963 }
1809 } 1964 }
1810 1965
1811 if (GIMME_V != G_VOID) 1966 if (GIMME_V != G_VOID)
1812 do 1967 do
1813 { 1968 {
1814 STRLEN offset; 1969 char *offset;
1815 1970
1816 if (!INCR_DONE (self)) 1971 if (!INCR_DONE (self))
1817 { 1972 {
1818 incr_parse (self); 1973 incr_parse (self);
1819 1974
1825 break; 1980 break;
1826 } 1981 }
1827 1982
1828 XPUSHs (decode_json (self->incr_text, self, &offset)); 1983 XPUSHs (decode_json (self->incr_text, self, &offset));
1829 1984
1830 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1831 self->incr_pos -= offset; 1985 self->incr_pos -= offset - SvPVX (self->incr_text);
1832 self->incr_nest = 0; 1986 self->incr_nest = 0;
1833 self->incr_mode = 0; 1987 self->incr_mode = 0;
1988
1989 sv_chop (self->incr_text, offset);
1834 } 1990 }
1835 while (GIMME_V == G_ARRAY); 1991 while (GIMME_V == G_ARRAY);
1836} 1992}
1837 1993
1838SV *incr_text (JSON *self) 1994SV *incr_text (JSON *self)
1899 json_init (&json); 2055 json_init (&json);
1900 json.flags |= ix; 2056 json.flags |= ix;
1901 XPUSHs (decode_json (jsonstr, &json, 0)); 2057 XPUSHs (decode_json (jsonstr, &json, 0));
1902} 2058}
1903 2059
1904

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines