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.94 by root, Thu Nov 20 03:59:53 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
1415 /* work around bugs in 5.10 where manipulating magic values 1522 /* work around bugs in 5.10 where manipulating magic values
1416 * will perl ignore the magic in subsequent accesses 1523 * will perl ignore the magic in subsequent accesses
1417 */ 1524 */
1429 * and hope for the best. 1536 * and hope for the best.
1430 * Damnit, SvPV_nolen still trips over yet another assertion. This 1537 * Damnit, SvPV_nolen still trips over yet another assertion. This
1431 * assertion business is seriously broken, try yet another workaround 1538 * assertion business is seriously broken, try yet another workaround
1432 * for the broken -DDEBUGGING. 1539 * for the broken -DDEBUGGING.
1433 */ 1540 */
1541 {
1434#ifdef DEBUGGING 1542#ifdef DEBUGGING
1435 offset = SvOK (string) ? sv_len (string) : 0; 1543 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1436#else 1544#else
1437 offset = SvCUR (string); 1545 STRLEN offset = SvCUR (string);
1438#endif 1546#endif
1439 1547
1440 if (offset > json->max_size && json->max_size) 1548 if (offset > json->max_size && json->max_size)
1441 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",
1442 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1550 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1551 }
1443 1552
1444 if (json->flags & F_UTF8) 1553 if (json->flags & F_UTF8)
1445 sv_utf8_downgrade (string, 0); 1554 sv_utf8_downgrade (string, 0);
1446 else 1555 else
1447 sv_utf8_upgrade (string); 1556 sv_utf8_upgrade (string);
1459 1568
1460 *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
1461 1570
1462 decode_ws (&dec); 1571 decode_ws (&dec);
1463 sv = decode_sv (&dec); 1572 sv = decode_sv (&dec);
1573
1574 if (offset_return)
1575 *offset_return = dec.cur;
1464 1576
1465 if (!(offset_return || !sv)) 1577 if (!(offset_return || !sv))
1466 { 1578 {
1467 // check for trailing garbage 1579 // check for trailing garbage
1468 decode_ws (&dec); 1580 decode_ws (&dec);
1471 { 1583 {
1472 dec.err = "garbage after JSON object"; 1584 dec.err = "garbage after JSON object";
1473 SvREFCNT_dec (sv); 1585 SvREFCNT_dec (sv);
1474 sv = 0; 1586 sv = 0;
1475 } 1587 }
1476 }
1477
1478 if (offset_return || !sv)
1479 {
1480 offset = dec.json.flags & F_UTF8
1481 ? dec.cur - SvPVX (string)
1482 : utf8_distance (dec.cur, SvPVX (string));
1483
1484 if (offset_return)
1485 *offset_return = offset;
1486 } 1588 }
1487 1589
1488 if (!sv) 1590 if (!sv)
1489 { 1591 {
1490 SV *uni = sv_newmortal (); 1592 SV *uni = sv_newmortal ();
1496 SAVEVPTR (PL_curcop); 1598 SAVEVPTR (PL_curcop);
1497 PL_curcop = &cop; 1599 PL_curcop = &cop;
1498 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);
1499 LEAVE; 1601 LEAVE;
1500 1602
1501 croak ("%s, at character offset %d [\"%s\"]", 1603 croak ("%s, at character offset %d (before \"%s\")",
1502 dec.err, 1604 dec.err,
1503 (int)offset, 1605 ptr_to_index (string, dec.cur),
1504 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1606 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1505 } 1607 }
1506 1608
1507 sv = sv_2mortal (sv); 1609 sv = sv_2mortal (sv);
1508 1610
1517 1619
1518static void 1620static void
1519incr_parse (JSON *self) 1621incr_parse (JSON *self)
1520{ 1622{
1521 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...
1522 1627
1523 for (;;) 1628 for (;;)
1524 { 1629 {
1525 //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
1526 switch (self->incr_mode) 1631 switch (self->incr_mode)
1527 { 1632 {
1528 // only used for intiial whitespace skipping 1633 // only used for initial whitespace skipping
1529 case INCR_M_WS: 1634 case INCR_M_WS:
1530 for (;;) 1635 for (;;)
1531 { 1636 {
1532 if (*p > 0x20) 1637 if (*p > 0x20)
1533 { 1638 {
1639 if (*p == '#')
1640 {
1641 self->incr_mode = INCR_M_C0;
1642 goto incr_m_c;
1643 }
1644 else
1645 {
1534 self->incr_mode = INCR_M_JSON; 1646 self->incr_mode = INCR_M_JSON;
1535 goto incr_m_json; 1647 goto incr_m_json;
1648 }
1536 } 1649 }
1537 else if (!*p) 1650 else if (!*p)
1538 goto interrupt; 1651 goto interrupt;
1539 1652
1540 ++p; 1653 ++p;
1546 goto interrupt; 1659 goto interrupt;
1547 1660
1548 ++p; 1661 ++p;
1549 self->incr_mode = INCR_M_STR; 1662 self->incr_mode = INCR_M_STR;
1550 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;
1551 1683
1552 // inside a string 1684 // inside a string
1553 case INCR_M_STR: 1685 case INCR_M_STR:
1554 incr_m_str: 1686 incr_m_str:
1555 for (;;) 1687 for (;;)
1614 1746
1615 case ']': 1747 case ']':
1616 case '}': 1748 case '}':
1617 if (--self->incr_nest <= 0) 1749 if (--self->incr_nest <= 0)
1618 goto interrupt; 1750 goto interrupt;
1751 break;
1752
1753 case '#':
1754 self->incr_mode = INCR_M_C1;
1755 goto incr_m_c;
1619 } 1756 }
1620 } 1757 }
1621 } 1758 }
1622 1759
1623 modechange: 1760 modechange:
1624 ; 1761 ;
1625 } 1762 }
1626 1763
1627interrupt: 1764interrupt:
1628 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
1629 //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
1630} 1768}
1631 1769
1632///////////////////////////////////////////////////////////////////////////// 1770/////////////////////////////////////////////////////////////////////////////
1633// XS interface functions 1771// XS interface functions
1648 json_stash = gv_stashpv ("JSON::XS" , 1); 1786 json_stash = gv_stashpv ("JSON::XS" , 1);
1649 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1787 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1650 1788
1651 json_true = get_bool ("JSON::XS::true"); 1789 json_true = get_bool ("JSON::XS::true");
1652 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 */
1653} 1793}
1654 1794
1655PROTOTYPES: DISABLE 1795PROTOTYPES: DISABLE
1656 1796
1657void CLONE (...) 1797void CLONE (...)
1777 XPUSHs (decode_json (jsonstr, self, 0)); 1917 XPUSHs (decode_json (jsonstr, self, 0));
1778 1918
1779void decode_prefix (JSON *self, SV *jsonstr) 1919void decode_prefix (JSON *self, SV *jsonstr)
1780 PPCODE: 1920 PPCODE:
1781{ 1921{
1782 STRLEN offset; 1922 char *offset;
1783 EXTEND (SP, 2); 1923 EXTEND (SP, 2);
1784 PUSHs (decode_json (jsonstr, self, &offset)); 1924 PUSHs (decode_json (jsonstr, self, &offset));
1785 PUSHs (sv_2mortal (newSVuv (offset))); 1925 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1786} 1926}
1787 1927
1788void incr_parse (JSON *self, SV *jsonstr = 0) 1928void incr_parse (JSON *self, SV *jsonstr = 0)
1789 PPCODE: 1929 PPCODE:
1790{ 1930{
1792 self->incr_text = newSVpvn ("", 0); 1932 self->incr_text = newSVpvn ("", 0);
1793 1933
1794 // append data, if any 1934 // append data, if any
1795 if (jsonstr) 1935 if (jsonstr)
1796 { 1936 {
1797 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1937 if (SvUTF8 (jsonstr))
1798 { 1938 {
1939 if (!SvUTF8 (self->incr_text))
1940 {
1799 /* utf-8-ness differs, need to upgrade */ 1941 /* utf-8-ness differs, need to upgrade */
1800 sv_utf8_upgrade (self->incr_text); 1942 sv_utf8_upgrade (self->incr_text);
1801 1943
1802 if (self->incr_pos) 1944 if (self->incr_pos)
1803 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)
1804 - (U8 *)SvPVX (self->incr_text); 1946 - (U8 *)SvPVX (self->incr_text);
1947 }
1805 } 1948 }
1949 else if (SvUTF8 (self->incr_text))
1950 sv_utf8_upgrade (jsonstr);
1806 1951
1807 { 1952 {
1808 STRLEN len; 1953 STRLEN len;
1809 const char *str = SvPV (jsonstr, len); 1954 const char *str = SvPV (jsonstr, len);
1810 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
1811 Move (str, SvEND (self->incr_text), len, char); 1960 Move (str, SvEND (self->incr_text), len, char);
1812 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1961 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1813 *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
1814 } 1963 }
1815 } 1964 }
1816 1965
1817 if (GIMME_V != G_VOID) 1966 if (GIMME_V != G_VOID)
1818 do 1967 do
1819 { 1968 {
1820 STRLEN offset; 1969 char *offset;
1821 1970
1822 if (!INCR_DONE (self)) 1971 if (!INCR_DONE (self))
1823 { 1972 {
1824 incr_parse (self); 1973 incr_parse (self);
1825 1974
1831 break; 1980 break;
1832 } 1981 }
1833 1982
1834 XPUSHs (decode_json (self->incr_text, self, &offset)); 1983 XPUSHs (decode_json (self->incr_text, self, &offset));
1835 1984
1836 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1837 self->incr_pos -= offset; 1985 self->incr_pos -= offset - SvPVX (self->incr_text);
1838 self->incr_nest = 0; 1986 self->incr_nest = 0;
1839 self->incr_mode = 0; 1987 self->incr_mode = 0;
1988
1989 sv_chop (self->incr_text, offset);
1840 } 1990 }
1841 while (GIMME_V == G_ARRAY); 1991 while (GIMME_V == G_ARRAY);
1842} 1992}
1843 1993
1844SV *incr_text (JSON *self) 1994SV *incr_text (JSON *self)
1905 json_init (&json); 2055 json_init (&json);
1906 json.flags |= ix; 2056 json.flags |= ix;
1907 XPUSHs (decode_json (jsonstr, &json, 0)); 2057 XPUSHs (decode_json (jsonstr, &json, 0));
1908} 2058}
1909 2059
1910

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines