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.90 by root, Sun Jul 20 17:55:19 2008 UTC vs.
Revision 1.104 by root, Tue Jan 19 00:31:13 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)
198{
199 UV uaccum = 0;
200 int eaccum = 0;
201
202 for (;;)
203 {
204 U8 dig = (U8)*s - '0';
205
206 if (expect_false (dig >= 10))
207 {
208 if (dig == (U8)((U8)'.' - (U8)'0'))
209 {
210 ++s;
211 json_atof_scan1 (s, accum, expo, 1);
212 }
213 else if ((dig | ' ') == 'e' - '0')
214 {
215 int exp2 = 0;
216 int neg = 0;
217
218 ++s;
219
220 if (*s == '-')
221 {
222 ++s;
223 neg = 1;
224 }
225 else if (*s == '+')
226 ++s;
227
228 while ((dig = (U8)*s - '0') < 10)
229 exp2 = exp2 * 10 + *s++ - '0';
230
231 *expo += neg ? -exp2 : exp2;
232 }
233
234 break;
235 }
236
237 ++s;
238
239 uaccum = uaccum * 10 + dig;
240 ++eaccum;
241
242 // if we have too many digits, then recurse for more
243 // we actually do this for rather few digits
244 if (uaccum >= (UV_MAX - 9) / 10)
245 {
246 if (postdp) *expo -= eaccum;
247 json_atof_scan1 (s, accum, expo, postdp);
248 if (postdp) *expo += eaccum;
249
250 break;
251 }
252 }
253
254 if (postdp) *expo -= eaccum;
255 *accum += uaccum * pow (10., *expo);
256 *expo += eaccum;
257}
258
259static NV
260json_atof (const char *s)
261{
262 NV accum = 0.;
263 int expo = 0;
264 int neg = 0;
265
266 if (*s == '-')
267 {
268 ++s;
269 neg = 1;
270 }
271
272 json_atof_scan1 (s, &accum, &expo, 0);
273
274 return neg ? -accum : accum;
275}
180///////////////////////////////////////////////////////////////////////////// 276/////////////////////////////////////////////////////////////////////////////
181// encoder 277// encoder
182 278
183// structure used for encoding JSON 279// structure used for encoding JSON
184typedef struct 280typedef struct
194INLINE void 290INLINE void
195need (enc_t *enc, STRLEN len) 291need (enc_t *enc, STRLEN len)
196{ 292{
197 if (expect_false (enc->cur + len >= enc->end)) 293 if (expect_false (enc->cur + len >= enc->end))
198 { 294 {
199 STRLEN cur = enc->cur - SvPVX (enc->sv); 295 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 296 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 297 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 298 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 299 }
204} 300}
205 301
280 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 376 (int)((uch - 0x10000) % 0x400 + 0xDC00));
281 enc->cur += 12; 377 enc->cur += 12;
282 } 378 }
283 else 379 else
284 { 380 {
285 static char hexdigit [16] = "0123456789abcdef";
286 need (enc, len += 5); 381 need (enc, len += 5);
287 *enc->cur++ = '\\'; 382 *enc->cur++ = '\\';
288 *enc->cur++ = 'u'; 383 *enc->cur++ = 'u';
289 *enc->cur++ = hexdigit [ uch >> 12 ]; 384 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
290 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 385 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
291 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 386 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
292 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 387 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
293 } 388 }
294 389
295 str += clen; 390 str += clen;
296 } 391 }
297 else if (enc->json.flags & F_LATIN1) 392 else if (enc->json.flags & F_LATIN1)
461 556
462 encode_ch (enc, '{'); 557 encode_ch (enc, '{');
463 558
464 // for canonical output we have to sort by keys first 559 // for canonical output we have to sort by keys first
465 // actually, this is mostly due to the stupid so-called 560 // actually, this is mostly due to the stupid so-called
466 // security workaround added somewhere in 5.8.x. 561 // security workaround added somewhere in 5.8.x
467 // that randomises hash orderings 562 // that randomises hash orderings
468 if (enc->json.flags & F_CANONICAL) 563 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
469 { 564 {
470 int count = hv_iterinit (hv); 565 int count = hv_iterinit (hv);
471 566
472 if (SvMAGICAL (hv)) 567 if (SvMAGICAL (hv))
473 { 568 {
751 : enc.json.flags & F_LATIN1 ? 0x000100UL 846 : enc.json.flags & F_LATIN1 ? 0x000100UL
752 : 0x110000UL; 847 : 0x110000UL;
753 848
754 SvPOK_only (enc.sv); 849 SvPOK_only (enc.sv);
755 encode_sv (&enc, scalar); 850 encode_sv (&enc, scalar);
851 encode_nl (&enc);
756 852
757 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 853 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
758 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 854 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
759 855
760 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 856 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
940 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1036 else if (expect_true (ch >= 0x20 && ch < 0x80))
941 *cur++ = ch; 1037 *cur++ = ch;
942 else if (ch >= 0x80) 1038 else if (ch >= 0x80)
943 { 1039 {
944 STRLEN clen; 1040 STRLEN clen;
945 UV uch;
946 1041
947 --dec_cur; 1042 --dec_cur;
948 1043
949 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1044 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
950 if (clen == (STRLEN)-1) 1045 if (clen == (STRLEN)-1)
951 ERR ("malformed UTF-8 character in JSON string"); 1046 ERR ("malformed UTF-8 character in JSON string");
952 1047
953 do 1048 do
954 *cur++ = *dec_cur++; 1049 *cur++ = *dec_cur++;
971 { 1066 {
972 STRLEN len = cur - buf; 1067 STRLEN len = cur - buf;
973 1068
974 if (sv) 1069 if (sv)
975 { 1070 {
976 SvGROW (sv, SvCUR (sv) + len + 1); 1071 STRLEN cur = SvCUR (sv);
1072
1073 if (SvLEN (sv) <= cur + len)
1074 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1075
977 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 1076 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
978 SvCUR_set (sv, SvCUR (sv) + len); 1077 SvCUR_set (sv, SvCUR (sv) + len);
979 } 1078 }
980 else 1079 else
981 sv = newSVpvn (buf, len); 1080 sv = newSVpvn (buf, len);
1072 1171
1073 // special case the rather common 1..5-digit-int case 1172 // special case the rather common 1..5-digit-int case
1074 if (*start == '-') 1173 if (*start == '-')
1075 switch (len) 1174 switch (len)
1076 { 1175 {
1077 case 2: return newSViv (-( start [1] - '0' * 1)); 1176 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1078 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1177 case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11));
1079 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1178 case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
1080 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1179 case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1081 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); 1180 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1082 } 1181 }
1083 else 1182 else
1084 switch (len) 1183 switch (len)
1085 { 1184 {
1086 case 1: return newSViv ( start [0] - '0' * 1); 1185 case 1: return newSViv ( start [0] - '0' * 1);
1087 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1186 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
1088 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1187 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
1089 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1188 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1090 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); 1189 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1091 } 1190 }
1092 1191
1093 { 1192 {
1094 UV uv; 1193 UV uv;
1095 int numtype = grok_number (start, len, &uv); 1194 int numtype = grok_number (start, len, &uv);
1104 } 1203 }
1105 1204
1106 len -= *start == '-' ? 1 : 0; 1205 len -= *start == '-' ? 1 : 0;
1107 1206
1108 // does not fit into IV or UV, try NV 1207 // does not fit into IV or UV, try NV
1109 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1208 if (len <= NV_DIG)
1110 #if defined (LDBL_DIG)
1111 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1112 #endif
1113 )
1114 // fits into NV without loss of precision 1209 // fits into NV without loss of precision
1115 return newSVnv (Atof (start)); 1210 return newSVnv (json_atof (start));
1116 1211
1117 // everything else fails, convert it to a string 1212 // everything else fails, convert it to a string
1118 return newSVpvn (start, dec->cur - start); 1213 return newSVpvn (start, dec->cur - start);
1119 } 1214 }
1120 1215
1121 // loss of precision here 1216 // loss of precision here
1122 return newSVnv (Atof (start)); 1217 return newSVnv (json_atof (start));
1123 1218
1124fail: 1219fail:
1125 return 0; 1220 return 0;
1126} 1221}
1127 1222
1405fail: 1500fail:
1406 return 0; 1501 return 0;
1407} 1502}
1408 1503
1409static SV * 1504static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1505decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1506{
1412 dec_t dec; 1507 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1508 SV *sv;
1415 1509
1510 /* work around bugs in 5.10 where manipulating magic values
1511 * will perl ignore the magic in subsequent accesses
1512 */
1416 SvGETMAGIC (string); 1513 /*SvGETMAGIC (string);*/
1514 if (SvMAGICAL (string))
1515 string = sv_2mortal (newSVsv (string));
1516
1417 SvUPGRADE (string, SVt_PV); 1517 SvUPGRADE (string, SVt_PV);
1418 1518
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1519 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1520 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1521 * return the xpv_cur field which certainly exists after upgrading.
1424 * and hope for the best. 1524 * and hope for the best.
1425 * Damnit, SvPV_nolen still trips over yet another assertion. This 1525 * Damnit, SvPV_nolen still trips over yet another assertion. This
1426 * assertion business is seriously broken, try yet another workaround 1526 * assertion business is seriously broken, try yet another workaround
1427 * for the broken -DDEBUGGING. 1527 * for the broken -DDEBUGGING.
1428 */ 1528 */
1529 {
1429#ifdef DEBUGGING 1530#ifdef DEBUGGING
1430 offset = SvOK (string) ? sv_len (string) : 0; 1531 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1431#else 1532#else
1432 offset = SvCUR (string); 1533 STRLEN offset = SvCUR (string);
1433#endif 1534#endif
1434 1535
1435 if (offset > json->max_size && json->max_size) 1536 if (offset > json->max_size && json->max_size)
1436 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1537 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1437 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1538 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1539 }
1438 1540
1439 if (json->flags & F_UTF8) 1541 if (json->flags & F_UTF8)
1440 sv_utf8_downgrade (string, 0); 1542 sv_utf8_downgrade (string, 0);
1441 else 1543 else
1442 sv_utf8_upgrade (string); 1544 sv_utf8_upgrade (string);
1454 1556
1455 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1557 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1456 1558
1457 decode_ws (&dec); 1559 decode_ws (&dec);
1458 sv = decode_sv (&dec); 1560 sv = decode_sv (&dec);
1561
1562 if (offset_return)
1563 *offset_return = dec.cur;
1459 1564
1460 if (!(offset_return || !sv)) 1565 if (!(offset_return || !sv))
1461 { 1566 {
1462 // check for trailing garbage 1567 // check for trailing garbage
1463 decode_ws (&dec); 1568 decode_ws (&dec);
1466 { 1571 {
1467 dec.err = "garbage after JSON object"; 1572 dec.err = "garbage after JSON object";
1468 SvREFCNT_dec (sv); 1573 SvREFCNT_dec (sv);
1469 sv = 0; 1574 sv = 0;
1470 } 1575 }
1471 }
1472
1473 if (offset_return || !sv)
1474 {
1475 offset = dec.json.flags & F_UTF8
1476 ? dec.cur - SvPVX (string)
1477 : utf8_distance (dec.cur, SvPVX (string));
1478
1479 if (offset_return)
1480 *offset_return = offset;
1481 } 1576 }
1482 1577
1483 if (!sv) 1578 if (!sv)
1484 { 1579 {
1485 SV *uni = sv_newmortal (); 1580 SV *uni = sv_newmortal ();
1491 SAVEVPTR (PL_curcop); 1586 SAVEVPTR (PL_curcop);
1492 PL_curcop = &cop; 1587 PL_curcop = &cop;
1493 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1588 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1494 LEAVE; 1589 LEAVE;
1495 1590
1496 croak ("%s, at character offset %d [\"%s\"]", 1591 croak ("%s, at character offset %d (before \"%s\")",
1497 dec.err, 1592 dec.err,
1498 (int)offset, 1593 ptr_to_index (string, dec.cur),
1499 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1594 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1500 } 1595 }
1501 1596
1502 sv = sv_2mortal (sv); 1597 sv = sv_2mortal (sv);
1503 1598
1512 1607
1513static void 1608static void
1514incr_parse (JSON *self) 1609incr_parse (JSON *self)
1515{ 1610{
1516 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1611 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1612
1613 // the state machine here is a bit convoluted and could be simplified a lot
1614 // but this would make it slower, so...
1517 1615
1518 for (;;) 1616 for (;;)
1519 { 1617 {
1520 //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 1618 //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
1521 switch (self->incr_mode) 1619 switch (self->incr_mode)
1522 { 1620 {
1523 // only used for intiial whitespace skipping 1621 // only used for initial whitespace skipping
1524 case INCR_M_WS: 1622 case INCR_M_WS:
1525 for (;;) 1623 for (;;)
1526 { 1624 {
1527 if (*p > 0x20) 1625 if (*p > 0x20)
1528 { 1626 {
1627 if (*p == '#')
1628 {
1629 self->incr_mode = INCR_M_C0;
1630 goto incr_m_c;
1631 }
1632 else
1633 {
1529 self->incr_mode = INCR_M_JSON; 1634 self->incr_mode = INCR_M_JSON;
1530 goto incr_m_json; 1635 goto incr_m_json;
1636 }
1531 } 1637 }
1532 else if (!*p) 1638 else if (!*p)
1533 goto interrupt; 1639 goto interrupt;
1534 1640
1535 ++p; 1641 ++p;
1541 goto interrupt; 1647 goto interrupt;
1542 1648
1543 ++p; 1649 ++p;
1544 self->incr_mode = INCR_M_STR; 1650 self->incr_mode = INCR_M_STR;
1545 goto incr_m_str; 1651 goto incr_m_str;
1652
1653 // inside #-style comments
1654 case INCR_M_C0:
1655 case INCR_M_C1:
1656 incr_m_c:
1657 for (;;)
1658 {
1659 if (*p == '\n')
1660 {
1661 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1662 break;
1663 }
1664 else if (!*p)
1665 goto interrupt;
1666
1667 ++p;
1668 }
1669
1670 break;
1546 1671
1547 // inside a string 1672 // inside a string
1548 case INCR_M_STR: 1673 case INCR_M_STR:
1549 incr_m_str: 1674 incr_m_str:
1550 for (;;) 1675 for (;;)
1609 1734
1610 case ']': 1735 case ']':
1611 case '}': 1736 case '}':
1612 if (--self->incr_nest <= 0) 1737 if (--self->incr_nest <= 0)
1613 goto interrupt; 1738 goto interrupt;
1739 break;
1740
1741 case '#':
1742 self->incr_mode = INCR_M_C1;
1743 goto incr_m_c;
1614 } 1744 }
1615 } 1745 }
1616 } 1746 }
1617 1747
1618 modechange: 1748 modechange:
1619 ; 1749 ;
1620 } 1750 }
1621 1751
1622interrupt: 1752interrupt:
1623 self->incr_pos = p - SvPVX (self->incr_text); 1753 self->incr_pos = p - SvPVX (self->incr_text);
1754 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1624 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1755 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1625} 1756}
1626 1757
1627///////////////////////////////////////////////////////////////////////////// 1758/////////////////////////////////////////////////////////////////////////////
1628// XS interface functions 1759// XS interface functions
1643 json_stash = gv_stashpv ("JSON::XS" , 1); 1774 json_stash = gv_stashpv ("JSON::XS" , 1);
1644 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1775 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1645 1776
1646 json_true = get_bool ("JSON::XS::true"); 1777 json_true = get_bool ("JSON::XS::true");
1647 json_false = get_bool ("JSON::XS::false"); 1778 json_false = get_bool ("JSON::XS::false");
1779
1780 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1648} 1781}
1649 1782
1650PROTOTYPES: DISABLE 1783PROTOTYPES: DISABLE
1651 1784
1652void CLONE (...) 1785void CLONE (...)
1772 XPUSHs (decode_json (jsonstr, self, 0)); 1905 XPUSHs (decode_json (jsonstr, self, 0));
1773 1906
1774void decode_prefix (JSON *self, SV *jsonstr) 1907void decode_prefix (JSON *self, SV *jsonstr)
1775 PPCODE: 1908 PPCODE:
1776{ 1909{
1777 STRLEN offset; 1910 char *offset;
1778 EXTEND (SP, 2); 1911 EXTEND (SP, 2);
1779 PUSHs (decode_json (jsonstr, self, &offset)); 1912 PUSHs (decode_json (jsonstr, self, &offset));
1780 PUSHs (sv_2mortal (newSVuv (offset))); 1913 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1781} 1914}
1782 1915
1783void incr_parse (JSON *self, SV *jsonstr = 0) 1916void incr_parse (JSON *self, SV *jsonstr = 0)
1784 PPCODE: 1917 PPCODE:
1785{ 1918{
1787 self->incr_text = newSVpvn ("", 0); 1920 self->incr_text = newSVpvn ("", 0);
1788 1921
1789 // append data, if any 1922 // append data, if any
1790 if (jsonstr) 1923 if (jsonstr)
1791 { 1924 {
1792 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1925 if (SvUTF8 (jsonstr))
1793 { 1926 {
1927 if (!SvUTF8 (self->incr_text))
1928 {
1794 /* utf-8-ness differs, need to upgrade */ 1929 /* utf-8-ness differs, need to upgrade */
1795 sv_utf8_upgrade (self->incr_text); 1930 sv_utf8_upgrade (self->incr_text);
1796 1931
1797 if (self->incr_pos) 1932 if (self->incr_pos)
1798 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1933 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1799 - (U8 *)SvPVX (self->incr_text); 1934 - (U8 *)SvPVX (self->incr_text);
1935 }
1800 } 1936 }
1937 else if (SvUTF8 (self->incr_text))
1938 sv_utf8_upgrade (jsonstr);
1801 1939
1802 { 1940 {
1803 STRLEN len; 1941 STRLEN len;
1804 const char *str = SvPV (jsonstr, len); 1942 const char *str = SvPV (jsonstr, len);
1805 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1943 STRLEN cur = SvCUR (self->incr_text);
1944
1945 if (SvLEN (self->incr_text) <= cur + len)
1946 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1947
1806 Move (str, SvEND (self->incr_text), len, char); 1948 Move (str, SvEND (self->incr_text), len, char);
1807 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1949 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1808 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1950 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1809 } 1951 }
1810 } 1952 }
1811 1953
1812 if (GIMME_V != G_VOID) 1954 if (GIMME_V != G_VOID)
1813 do 1955 do
1814 { 1956 {
1815 STRLEN offset; 1957 char *offset;
1816 1958
1817 if (!INCR_DONE (self)) 1959 if (!INCR_DONE (self))
1818 { 1960 {
1819 incr_parse (self); 1961 incr_parse (self);
1820 1962
1826 break; 1968 break;
1827 } 1969 }
1828 1970
1829 XPUSHs (decode_json (self->incr_text, self, &offset)); 1971 XPUSHs (decode_json (self->incr_text, self, &offset));
1830 1972
1831 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1832 self->incr_pos -= offset; 1973 self->incr_pos -= offset - SvPVX (self->incr_text);
1833 self->incr_nest = 0; 1974 self->incr_nest = 0;
1834 self->incr_mode = 0; 1975 self->incr_mode = 0;
1976
1977 sv_chop (self->incr_text, offset);
1835 } 1978 }
1836 while (GIMME_V == G_ARRAY); 1979 while (GIMME_V == G_ARRAY);
1837} 1980}
1838 1981
1839SV *incr_text (JSON *self) 1982SV *incr_text (JSON *self)
1900 json_init (&json); 2043 json_init (&json);
1901 json.flags |= ix; 2044 json.flags |= ix;
1902 XPUSHs (decode_json (jsonstr, &json, 0)); 2045 XPUSHs (decode_json (jsonstr, &json, 0));
1903} 2046}
1904 2047
1905

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines