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.106 by root, Tue Jan 19 01:36:34 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
1404fail: 1510fail:
1405 return 0; 1511 return 0;
1406} 1512}
1407 1513
1408static SV * 1514static SV *
1409decode_json (SV *string, JSON *json, STRLEN *offset_return) 1515decode_json (SV *string, JSON *json, char **offset_return)
1410{ 1516{
1411 dec_t dec; 1517 dec_t dec;
1412 STRLEN offset;
1413 SV *sv; 1518 SV *sv;
1414 1519
1520 /* work around bugs in 5.10 where manipulating magic values
1521 * will perl ignore the magic in subsequent accesses
1522 */
1415 SvGETMAGIC (string); 1523 /*SvGETMAGIC (string);*/
1524 if (SvMAGICAL (string))
1525 string = sv_2mortal (newSVsv (string));
1526
1416 SvUPGRADE (string, SVt_PV); 1527 SvUPGRADE (string, SVt_PV);
1417 1528
1418 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1529 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1419 * assertion with -DDEBUGGING, although SvCUR is documented to 1530 * assertion with -DDEBUGGING, although SvCUR is documented to
1420 * return the xpv_cur field which certainly exists after upgrading. 1531 * return the xpv_cur field which certainly exists after upgrading.
1423 * and hope for the best. 1534 * and hope for the best.
1424 * Damnit, SvPV_nolen still trips over yet another assertion. This 1535 * Damnit, SvPV_nolen still trips over yet another assertion. This
1425 * assertion business is seriously broken, try yet another workaround 1536 * assertion business is seriously broken, try yet another workaround
1426 * for the broken -DDEBUGGING. 1537 * for the broken -DDEBUGGING.
1427 */ 1538 */
1539 {
1428#ifdef DEBUGGING 1540#ifdef DEBUGGING
1429 offset = SvOK (string) ? sv_len (string) : 0; 1541 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1430#else 1542#else
1431 offset = SvCUR (string); 1543 STRLEN offset = SvCUR (string);
1432#endif 1544#endif
1433 1545
1434 if (offset > json->max_size && json->max_size) 1546 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", 1547 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); 1548 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1549 }
1437 1550
1438 if (json->flags & F_UTF8) 1551 if (json->flags & F_UTF8)
1439 sv_utf8_downgrade (string, 0); 1552 sv_utf8_downgrade (string, 0);
1440 else 1553 else
1441 sv_utf8_upgrade (string); 1554 sv_utf8_upgrade (string);
1453 1566
1454 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1567 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1455 1568
1456 decode_ws (&dec); 1569 decode_ws (&dec);
1457 sv = decode_sv (&dec); 1570 sv = decode_sv (&dec);
1571
1572 if (offset_return)
1573 *offset_return = dec.cur;
1458 1574
1459 if (!(offset_return || !sv)) 1575 if (!(offset_return || !sv))
1460 { 1576 {
1461 // check for trailing garbage 1577 // check for trailing garbage
1462 decode_ws (&dec); 1578 decode_ws (&dec);
1465 { 1581 {
1466 dec.err = "garbage after JSON object"; 1582 dec.err = "garbage after JSON object";
1467 SvREFCNT_dec (sv); 1583 SvREFCNT_dec (sv);
1468 sv = 0; 1584 sv = 0;
1469 } 1585 }
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 } 1586 }
1481 1587
1482 if (!sv) 1588 if (!sv)
1483 { 1589 {
1484 SV *uni = sv_newmortal (); 1590 SV *uni = sv_newmortal ();
1490 SAVEVPTR (PL_curcop); 1596 SAVEVPTR (PL_curcop);
1491 PL_curcop = &cop; 1597 PL_curcop = &cop;
1492 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1598 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1493 LEAVE; 1599 LEAVE;
1494 1600
1495 croak ("%s, at character offset %d [\"%s\"]", 1601 croak ("%s, at character offset %d (before \"%s\")",
1496 dec.err, 1602 dec.err,
1497 (int)offset, 1603 ptr_to_index (string, dec.cur),
1498 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1604 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1499 } 1605 }
1500 1606
1501 sv = sv_2mortal (sv); 1607 sv = sv_2mortal (sv);
1502 1608
1511 1617
1512static void 1618static void
1513incr_parse (JSON *self) 1619incr_parse (JSON *self)
1514{ 1620{
1515 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1621 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1622
1623 // the state machine here is a bit convoluted and could be simplified a lot
1624 // but this would make it slower, so...
1516 1625
1517 for (;;) 1626 for (;;)
1518 { 1627 {
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 1628 //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) 1629 switch (self->incr_mode)
1521 { 1630 {
1522 // only used for intiial whitespace skipping 1631 // only used for initial whitespace skipping
1523 case INCR_M_WS: 1632 case INCR_M_WS:
1524 for (;;) 1633 for (;;)
1525 { 1634 {
1526 if (*p > 0x20) 1635 if (*p > 0x20)
1527 { 1636 {
1637 if (*p == '#')
1638 {
1639 self->incr_mode = INCR_M_C0;
1640 goto incr_m_c;
1641 }
1642 else
1643 {
1528 self->incr_mode = INCR_M_JSON; 1644 self->incr_mode = INCR_M_JSON;
1529 goto incr_m_json; 1645 goto incr_m_json;
1646 }
1530 } 1647 }
1531 else if (!*p) 1648 else if (!*p)
1532 goto interrupt; 1649 goto interrupt;
1533 1650
1534 ++p; 1651 ++p;
1540 goto interrupt; 1657 goto interrupt;
1541 1658
1542 ++p; 1659 ++p;
1543 self->incr_mode = INCR_M_STR; 1660 self->incr_mode = INCR_M_STR;
1544 goto incr_m_str; 1661 goto incr_m_str;
1662
1663 // inside #-style comments
1664 case INCR_M_C0:
1665 case INCR_M_C1:
1666 incr_m_c:
1667 for (;;)
1668 {
1669 if (*p == '\n')
1670 {
1671 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1672 break;
1673 }
1674 else if (!*p)
1675 goto interrupt;
1676
1677 ++p;
1678 }
1679
1680 break;
1545 1681
1546 // inside a string 1682 // inside a string
1547 case INCR_M_STR: 1683 case INCR_M_STR:
1548 incr_m_str: 1684 incr_m_str:
1549 for (;;) 1685 for (;;)
1608 1744
1609 case ']': 1745 case ']':
1610 case '}': 1746 case '}':
1611 if (--self->incr_nest <= 0) 1747 if (--self->incr_nest <= 0)
1612 goto interrupt; 1748 goto interrupt;
1749 break;
1750
1751 case '#':
1752 self->incr_mode = INCR_M_C1;
1753 goto incr_m_c;
1613 } 1754 }
1614 } 1755 }
1615 } 1756 }
1616 1757
1617 modechange: 1758 modechange:
1618 ; 1759 ;
1619 } 1760 }
1620 1761
1621interrupt: 1762interrupt:
1622 self->incr_pos = p - SvPVX (self->incr_text); 1763 self->incr_pos = p - SvPVX (self->incr_text);
1764 //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 1765 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1624} 1766}
1625 1767
1626///////////////////////////////////////////////////////////////////////////// 1768/////////////////////////////////////////////////////////////////////////////
1627// XS interface functions 1769// XS interface functions
1642 json_stash = gv_stashpv ("JSON::XS" , 1); 1784 json_stash = gv_stashpv ("JSON::XS" , 1);
1643 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1785 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1644 1786
1645 json_true = get_bool ("JSON::XS::true"); 1787 json_true = get_bool ("JSON::XS::true");
1646 json_false = get_bool ("JSON::XS::false"); 1788 json_false = get_bool ("JSON::XS::false");
1789
1790 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1647} 1791}
1648 1792
1649PROTOTYPES: DISABLE 1793PROTOTYPES: DISABLE
1650 1794
1651void CLONE (...) 1795void CLONE (...)
1771 XPUSHs (decode_json (jsonstr, self, 0)); 1915 XPUSHs (decode_json (jsonstr, self, 0));
1772 1916
1773void decode_prefix (JSON *self, SV *jsonstr) 1917void decode_prefix (JSON *self, SV *jsonstr)
1774 PPCODE: 1918 PPCODE:
1775{ 1919{
1776 STRLEN offset; 1920 char *offset;
1777 EXTEND (SP, 2); 1921 EXTEND (SP, 2);
1778 PUSHs (decode_json (jsonstr, self, &offset)); 1922 PUSHs (decode_json (jsonstr, self, &offset));
1779 PUSHs (sv_2mortal (newSVuv (offset))); 1923 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1780} 1924}
1781 1925
1782void incr_parse (JSON *self, SV *jsonstr = 0) 1926void incr_parse (JSON *self, SV *jsonstr = 0)
1783 PPCODE: 1927 PPCODE:
1784{ 1928{
1786 self->incr_text = newSVpvn ("", 0); 1930 self->incr_text = newSVpvn ("", 0);
1787 1931
1788 // append data, if any 1932 // append data, if any
1789 if (jsonstr) 1933 if (jsonstr)
1790 { 1934 {
1791 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1935 if (SvUTF8 (jsonstr))
1792 { 1936 {
1937 if (!SvUTF8 (self->incr_text))
1938 {
1793 /* utf-8-ness differs, need to upgrade */ 1939 /* utf-8-ness differs, need to upgrade */
1794 sv_utf8_upgrade (self->incr_text); 1940 sv_utf8_upgrade (self->incr_text);
1795 1941
1796 if (self->incr_pos) 1942 if (self->incr_pos)
1797 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1943 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1798 - (U8 *)SvPVX (self->incr_text); 1944 - (U8 *)SvPVX (self->incr_text);
1945 }
1799 } 1946 }
1947 else if (SvUTF8 (self->incr_text))
1948 sv_utf8_upgrade (jsonstr);
1800 1949
1801 { 1950 {
1802 STRLEN len; 1951 STRLEN len;
1803 const char *str = SvPV (jsonstr, len); 1952 const char *str = SvPV (jsonstr, len);
1804 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1953 STRLEN cur = SvCUR (self->incr_text);
1954
1955 if (SvLEN (self->incr_text) <= cur + len)
1956 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1957
1805 Move (str, SvEND (self->incr_text), len, char); 1958 Move (str, SvEND (self->incr_text), len, char);
1806 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1959 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 1960 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1808 } 1961 }
1809 } 1962 }
1810 1963
1811 if (GIMME_V != G_VOID) 1964 if (GIMME_V != G_VOID)
1812 do 1965 do
1813 { 1966 {
1814 STRLEN offset; 1967 char *offset;
1815 1968
1816 if (!INCR_DONE (self)) 1969 if (!INCR_DONE (self))
1817 { 1970 {
1818 incr_parse (self); 1971 incr_parse (self);
1819 1972
1825 break; 1978 break;
1826 } 1979 }
1827 1980
1828 XPUSHs (decode_json (self->incr_text, self, &offset)); 1981 XPUSHs (decode_json (self->incr_text, self, &offset));
1829 1982
1830 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1831 self->incr_pos -= offset; 1983 self->incr_pos -= offset - SvPVX (self->incr_text);
1832 self->incr_nest = 0; 1984 self->incr_nest = 0;
1833 self->incr_mode = 0; 1985 self->incr_mode = 0;
1986
1987 sv_chop (self->incr_text, offset);
1834 } 1988 }
1835 while (GIMME_V == G_ARRAY); 1989 while (GIMME_V == G_ARRAY);
1836} 1990}
1837 1991
1838SV *incr_text (JSON *self) 1992SV *incr_text (JSON *self)
1899 json_init (&json); 2053 json_init (&json);
1900 json.flags |= ix; 2054 json.flags |= ix;
1901 XPUSHs (decode_json (jsonstr, &json, 0)); 2055 XPUSHs (decode_json (jsonstr, &json, 0));
1902} 2056}
1903 2057
1904

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines