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.95 by root, Sun Feb 22 06:55:28 2009 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
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++;
1075 1181
1076 // special case the rather common 1..5-digit-int case 1182 // special case the rather common 1..5-digit-int case
1077 if (*start == '-') 1183 if (*start == '-')
1078 switch (len) 1184 switch (len)
1079 { 1185 {
1080 case 2: return newSViv (-( start [1] - '0' * 1)); 1186 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1081 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1187 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1082 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));
1083 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));
1084 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));
1085 } 1191 }
1086 else 1192 else
1087 switch (len) 1193 switch (len)
1088 { 1194 {
1089 case 1: return newSViv ( start [0] - '0' * 1); 1195 case 1: return newSViv ( start [0] - '0' * 1);
1090 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1196 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1091 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);
1092 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);
1093 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);
1094 } 1200 }
1095 1201
1096 { 1202 {
1097 UV uv; 1203 UV uv;
1098 int numtype = grok_number (start, len, &uv); 1204 int numtype = grok_number (start, len, &uv);
1107 } 1213 }
1108 1214
1109 len -= *start == '-' ? 1 : 0; 1215 len -= *start == '-' ? 1 : 0;
1110 1216
1111 // does not fit into IV or UV, try NV 1217 // does not fit into IV or UV, try NV
1112 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1218 if (len <= NV_DIG)
1113 #if defined (LDBL_DIG)
1114 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1115 #endif
1116 )
1117 // fits into NV without loss of precision 1219 // fits into NV without loss of precision
1118 return newSVnv (Atof (start)); 1220 return newSVnv (json_atof (start));
1119 1221
1120 // everything else fails, convert it to a string 1222 // everything else fails, convert it to a string
1121 return newSVpvn (start, dec->cur - start); 1223 return newSVpvn (start, dec->cur - start);
1122 } 1224 }
1123 1225
1124 // loss of precision here 1226 // loss of precision here
1125 return newSVnv (Atof (start)); 1227 return newSVnv (json_atof (start));
1126 1228
1127fail: 1229fail:
1128 return 0; 1230 return 0;
1129} 1231}
1130 1232
1408fail: 1510fail:
1409 return 0; 1511 return 0;
1410} 1512}
1411 1513
1412static SV * 1514static SV *
1413decode_json (SV *string, JSON *json, STRLEN *offset_return) 1515decode_json (SV *string, JSON *json, char **offset_return)
1414{ 1516{
1415 dec_t dec; 1517 dec_t dec;
1416 STRLEN offset;
1417 SV *sv; 1518 SV *sv;
1418 1519
1419 /* work around bugs in 5.10 where manipulating magic values 1520 /* work around bugs in 5.10 where manipulating magic values
1420 * will perl ignore the magic in subsequent accesses 1521 * will perl ignore the magic in subsequent accesses
1421 */ 1522 */
1433 * and hope for the best. 1534 * and hope for the best.
1434 * Damnit, SvPV_nolen still trips over yet another assertion. This 1535 * Damnit, SvPV_nolen still trips over yet another assertion. This
1435 * assertion business is seriously broken, try yet another workaround 1536 * assertion business is seriously broken, try yet another workaround
1436 * for the broken -DDEBUGGING. 1537 * for the broken -DDEBUGGING.
1437 */ 1538 */
1539 {
1438#ifdef DEBUGGING 1540#ifdef DEBUGGING
1439 offset = SvOK (string) ? sv_len (string) : 0; 1541 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1440#else 1542#else
1441 offset = SvCUR (string); 1543 STRLEN offset = SvCUR (string);
1442#endif 1544#endif
1443 1545
1444 if (offset > json->max_size && json->max_size) 1546 if (offset > json->max_size && json->max_size)
1445 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",
1446 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1548 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1549 }
1447 1550
1448 if (json->flags & F_UTF8) 1551 if (json->flags & F_UTF8)
1449 sv_utf8_downgrade (string, 0); 1552 sv_utf8_downgrade (string, 0);
1450 else 1553 else
1451 sv_utf8_upgrade (string); 1554 sv_utf8_upgrade (string);
1463 1566
1464 *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
1465 1568
1466 decode_ws (&dec); 1569 decode_ws (&dec);
1467 sv = decode_sv (&dec); 1570 sv = decode_sv (&dec);
1571
1572 if (offset_return)
1573 *offset_return = dec.cur;
1468 1574
1469 if (!(offset_return || !sv)) 1575 if (!(offset_return || !sv))
1470 { 1576 {
1471 // check for trailing garbage 1577 // check for trailing garbage
1472 decode_ws (&dec); 1578 decode_ws (&dec);
1475 { 1581 {
1476 dec.err = "garbage after JSON object"; 1582 dec.err = "garbage after JSON object";
1477 SvREFCNT_dec (sv); 1583 SvREFCNT_dec (sv);
1478 sv = 0; 1584 sv = 0;
1479 } 1585 }
1480 }
1481
1482 if (offset_return || !sv)
1483 {
1484 offset = dec.json.flags & F_UTF8
1485 ? dec.cur - SvPVX (string)
1486 : utf8_distance (dec.cur, SvPVX (string));
1487
1488 if (offset_return)
1489 *offset_return = offset;
1490 } 1586 }
1491 1587
1492 if (!sv) 1588 if (!sv)
1493 { 1589 {
1494 SV *uni = sv_newmortal (); 1590 SV *uni = sv_newmortal ();
1500 SAVEVPTR (PL_curcop); 1596 SAVEVPTR (PL_curcop);
1501 PL_curcop = &cop; 1597 PL_curcop = &cop;
1502 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);
1503 LEAVE; 1599 LEAVE;
1504 1600
1505 croak ("%s, at character offset %d [\"%s\"]", 1601 croak ("%s, at character offset %d (before \"%s\")",
1506 dec.err, 1602 dec.err,
1507 (int)offset, 1603 ptr_to_index (string, dec.cur),
1508 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1604 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1509 } 1605 }
1510 1606
1511 sv = sv_2mortal (sv); 1607 sv = sv_2mortal (sv);
1512 1608
1521 1617
1522static void 1618static void
1523incr_parse (JSON *self) 1619incr_parse (JSON *self)
1524{ 1620{
1525 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...
1526 1625
1527 for (;;) 1626 for (;;)
1528 { 1627 {
1529 //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
1530 switch (self->incr_mode) 1629 switch (self->incr_mode)
1531 { 1630 {
1532 // only used for intiial whitespace skipping 1631 // only used for initial whitespace skipping
1533 case INCR_M_WS: 1632 case INCR_M_WS:
1534 for (;;) 1633 for (;;)
1535 { 1634 {
1536 if (*p > 0x20) 1635 if (*p > 0x20)
1537 { 1636 {
1637 if (*p == '#')
1638 {
1639 self->incr_mode = INCR_M_C0;
1640 goto incr_m_c;
1641 }
1642 else
1643 {
1538 self->incr_mode = INCR_M_JSON; 1644 self->incr_mode = INCR_M_JSON;
1539 goto incr_m_json; 1645 goto incr_m_json;
1646 }
1540 } 1647 }
1541 else if (!*p) 1648 else if (!*p)
1542 goto interrupt; 1649 goto interrupt;
1543 1650
1544 ++p; 1651 ++p;
1550 goto interrupt; 1657 goto interrupt;
1551 1658
1552 ++p; 1659 ++p;
1553 self->incr_mode = INCR_M_STR; 1660 self->incr_mode = INCR_M_STR;
1554 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;
1555 1681
1556 // inside a string 1682 // inside a string
1557 case INCR_M_STR: 1683 case INCR_M_STR:
1558 incr_m_str: 1684 incr_m_str:
1559 for (;;) 1685 for (;;)
1618 1744
1619 case ']': 1745 case ']':
1620 case '}': 1746 case '}':
1621 if (--self->incr_nest <= 0) 1747 if (--self->incr_nest <= 0)
1622 goto interrupt; 1748 goto interrupt;
1749 break;
1750
1751 case '#':
1752 self->incr_mode = INCR_M_C1;
1753 goto incr_m_c;
1623 } 1754 }
1624 } 1755 }
1625 } 1756 }
1626 1757
1627 modechange: 1758 modechange:
1628 ; 1759 ;
1629 } 1760 }
1630 1761
1631interrupt: 1762interrupt:
1632 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
1633 //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
1634} 1766}
1635 1767
1636///////////////////////////////////////////////////////////////////////////// 1768/////////////////////////////////////////////////////////////////////////////
1637// XS interface functions 1769// XS interface functions
1652 json_stash = gv_stashpv ("JSON::XS" , 1); 1784 json_stash = gv_stashpv ("JSON::XS" , 1);
1653 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1785 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1654 1786
1655 json_true = get_bool ("JSON::XS::true"); 1787 json_true = get_bool ("JSON::XS::true");
1656 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 */
1657} 1791}
1658 1792
1659PROTOTYPES: DISABLE 1793PROTOTYPES: DISABLE
1660 1794
1661void CLONE (...) 1795void CLONE (...)
1781 XPUSHs (decode_json (jsonstr, self, 0)); 1915 XPUSHs (decode_json (jsonstr, self, 0));
1782 1916
1783void decode_prefix (JSON *self, SV *jsonstr) 1917void decode_prefix (JSON *self, SV *jsonstr)
1784 PPCODE: 1918 PPCODE:
1785{ 1919{
1786 STRLEN offset; 1920 char *offset;
1787 EXTEND (SP, 2); 1921 EXTEND (SP, 2);
1788 PUSHs (decode_json (jsonstr, self, &offset)); 1922 PUSHs (decode_json (jsonstr, self, &offset));
1789 PUSHs (sv_2mortal (newSVuv (offset))); 1923 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1790} 1924}
1791 1925
1792void incr_parse (JSON *self, SV *jsonstr = 0) 1926void incr_parse (JSON *self, SV *jsonstr = 0)
1793 PPCODE: 1927 PPCODE:
1794{ 1928{
1796 self->incr_text = newSVpvn ("", 0); 1930 self->incr_text = newSVpvn ("", 0);
1797 1931
1798 // append data, if any 1932 // append data, if any
1799 if (jsonstr) 1933 if (jsonstr)
1800 { 1934 {
1801 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1935 if (SvUTF8 (jsonstr))
1802 { 1936 {
1937 if (!SvUTF8 (self->incr_text))
1938 {
1803 /* utf-8-ness differs, need to upgrade */ 1939 /* utf-8-ness differs, need to upgrade */
1804 sv_utf8_upgrade (self->incr_text); 1940 sv_utf8_upgrade (self->incr_text);
1805 1941
1806 if (self->incr_pos) 1942 if (self->incr_pos)
1807 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)
1808 - (U8 *)SvPVX (self->incr_text); 1944 - (U8 *)SvPVX (self->incr_text);
1945 }
1809 } 1946 }
1947 else if (SvUTF8 (self->incr_text))
1948 sv_utf8_upgrade (jsonstr);
1810 1949
1811 { 1950 {
1812 STRLEN len; 1951 STRLEN len;
1813 const char *str = SvPV (jsonstr, len); 1952 const char *str = SvPV (jsonstr, len);
1814 STRLEN cur = SvCUR (self->incr_text); 1953 STRLEN cur = SvCUR (self->incr_text);
1823 } 1962 }
1824 1963
1825 if (GIMME_V != G_VOID) 1964 if (GIMME_V != G_VOID)
1826 do 1965 do
1827 { 1966 {
1828 STRLEN offset; 1967 char *offset;
1829 1968
1830 if (!INCR_DONE (self)) 1969 if (!INCR_DONE (self))
1831 { 1970 {
1832 incr_parse (self); 1971 incr_parse (self);
1833 1972
1839 break; 1978 break;
1840 } 1979 }
1841 1980
1842 XPUSHs (decode_json (self->incr_text, self, &offset)); 1981 XPUSHs (decode_json (self->incr_text, self, &offset));
1843 1982
1844 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1845 self->incr_pos -= offset; 1983 self->incr_pos -= offset - SvPVX (self->incr_text);
1846 self->incr_nest = 0; 1984 self->incr_nest = 0;
1847 self->incr_mode = 0; 1985 self->incr_mode = 0;
1986
1987 sv_chop (self->incr_text, offset);
1848 } 1988 }
1849 while (GIMME_V == G_ARRAY); 1989 while (GIMME_V == G_ARRAY);
1850} 1990}
1851 1991
1852SV *incr_text (JSON *self) 1992SV *incr_text (JSON *self)
1913 json_init (&json); 2053 json_init (&json);
1914 json.flags |= ix; 2054 json.flags |= ix;
1915 XPUSHs (decode_json (jsonstr, &json, 0)); 2055 XPUSHs (decode_json (jsonstr, &json, 0));
1916} 2056}
1917 2057
1918

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines