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.109 by root, Tue Mar 8 10:18:50 2011 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
41#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
42#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
43 44
44#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
45 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
46#define SB do { 49#define SB do {
47#define SE } while (0) 50#define SE } while (0)
48 51
49#if __GNUC__ >= 3 52#if __GNUC__ >= 3
50# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
76 79
77enum { 80enum {
78 INCR_M_WS = 0, // initial whitespace skipping, must be 0 81 INCR_M_WS = 0, // initial whitespace skipping, must be 0
79 INCR_M_STR, // inside string 82 INCR_M_STR, // inside string
80 INCR_M_BS, // inside backslash 83 INCR_M_BS, // inside backslash
84 INCR_M_C0, // inside comment in initial whitespace sequence
85 INCR_M_C1, // inside comment in other places
81 INCR_M_JSON // outside anything, count nesting 86 INCR_M_JSON // outside anything, count nesting
82}; 87};
83 88
84#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) 89#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
85 90
175 *s++ = 0x80 | ( ch & 0x3f); 180 *s++ = 0x80 | ( ch & 0x3f);
176 181
177 return s; 182 return s;
178} 183}
179 184
185// convert offset pointer to character index, sv must be string
186static STRLEN
187ptr_to_index (SV *sv, char *offset)
188{
189 return SvUTF8 (sv)
190 ? utf8_distance (offset, SvPVX (sv))
191 : offset - SvPVX (sv);
192}
193
194/////////////////////////////////////////////////////////////////////////////
195// fp hell
196
197// scan a group of digits, and a trailing exponent
198static void
199json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
200{
201 UV uaccum = 0;
202 int eaccum = 0;
203
204 // if we recurse too deep, skip all remaining digits
205 // to avoid a stack overflow attack
206 if (expect_false (--maxdepth <= 0))
207 while (((U8)*s - '0') < 10)
208 ++s;
209
210 for (;;)
211 {
212 U8 dig = (U8)*s - '0';
213
214 if (expect_false (dig >= 10))
215 {
216 if (dig == (U8)((U8)'.' - (U8)'0'))
217 {
218 ++s;
219 json_atof_scan1 (s, accum, expo, 1, maxdepth);
220 }
221 else if ((dig | ' ') == 'e' - '0')
222 {
223 int exp2 = 0;
224 int neg = 0;
225
226 ++s;
227
228 if (*s == '-')
229 {
230 ++s;
231 neg = 1;
232 }
233 else if (*s == '+')
234 ++s;
235
236 while ((dig = (U8)*s - '0') < 10)
237 exp2 = exp2 * 10 + *s++ - '0';
238
239 *expo += neg ? -exp2 : exp2;
240 }
241
242 break;
243 }
244
245 ++s;
246
247 uaccum = uaccum * 10 + dig;
248 ++eaccum;
249
250 // if we have too many digits, then recurse for more
251 // we actually do this for rather few digits
252 if (uaccum >= (UV_MAX - 9) / 10)
253 {
254 if (postdp) *expo -= eaccum;
255 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
256 if (postdp) *expo += eaccum;
257
258 break;
259 }
260 }
261
262 // this relies greatly on the quality of the pow ()
263 // implementation of the platform, but a good
264 // implementation is hard to beat.
265 if (postdp) *expo -= eaccum;
266 *accum += uaccum * Perl_pow (10., *expo);
267 *expo += eaccum;
268}
269
270static NV
271json_atof (const char *s)
272{
273 NV accum = 0.;
274 int expo = 0;
275 int neg = 0;
276
277 if (*s == '-')
278 {
279 ++s;
280 neg = 1;
281 }
282
283 // a recursion depth of ten gives us >>500 bits
284 json_atof_scan1 (s, &accum, &expo, 0, 10);
285
286 return neg ? -accum : accum;
287}
180///////////////////////////////////////////////////////////////////////////// 288/////////////////////////////////////////////////////////////////////////////
181// encoder 289// encoder
182 290
183// structure used for encoding JSON 291// structure used for encoding JSON
184typedef struct 292typedef struct
462 570
463 // for canonical output we have to sort by keys first 571 // for canonical output we have to sort by keys first
464 // actually, this is mostly due to the stupid so-called 572 // actually, this is mostly due to the stupid so-called
465 // security workaround added somewhere in 5.8.x 573 // security workaround added somewhere in 5.8.x
466 // that randomises hash orderings 574 // that randomises hash orderings
467 if (enc->json.flags & F_CANONICAL) 575 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
468 { 576 {
469 int count = hv_iterinit (hv); 577 int count = hv_iterinit (hv);
470 578
471 if (SvMAGICAL (hv)) 579 if (SvMAGICAL (hv))
472 { 580 {
750 : enc.json.flags & F_LATIN1 ? 0x000100UL 858 : enc.json.flags & F_LATIN1 ? 0x000100UL
751 : 0x110000UL; 859 : 0x110000UL;
752 860
753 SvPOK_only (enc.sv); 861 SvPOK_only (enc.sv);
754 encode_sv (&enc, scalar); 862 encode_sv (&enc, scalar);
863 encode_nl (&enc);
755 864
756 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 865 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
757 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 866 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
758 867
759 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 868 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
939 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1048 else if (expect_true (ch >= 0x20 && ch < 0x80))
940 *cur++ = ch; 1049 *cur++ = ch;
941 else if (ch >= 0x80) 1050 else if (ch >= 0x80)
942 { 1051 {
943 STRLEN clen; 1052 STRLEN clen;
944 UV uch;
945 1053
946 --dec_cur; 1054 --dec_cur;
947 1055
948 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1056 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
949 if (clen == (STRLEN)-1) 1057 if (clen == (STRLEN)-1)
950 ERR ("malformed UTF-8 character in JSON string"); 1058 ERR ("malformed UTF-8 character in JSON string");
951 1059
952 do 1060 do
953 *cur++ = *dec_cur++; 1061 *cur++ = *dec_cur++;
1075 1183
1076 // special case the rather common 1..5-digit-int case 1184 // special case the rather common 1..5-digit-int case
1077 if (*start == '-') 1185 if (*start == '-')
1078 switch (len) 1186 switch (len)
1079 { 1187 {
1080 case 2: return newSViv (-( start [1] - '0' * 1)); 1188 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1081 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1189 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)); 1190 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)); 1191 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)); 1192 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1085 } 1193 }
1086 else 1194 else
1087 switch (len) 1195 switch (len)
1088 { 1196 {
1089 case 1: return newSViv ( start [0] - '0' * 1); 1197 case 1: return newSViv ( start [0] - '0' * 1);
1090 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1198 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); 1199 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); 1200 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); 1201 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1094 } 1202 }
1095 1203
1096 { 1204 {
1097 UV uv; 1205 UV uv;
1098 int numtype = grok_number (start, len, &uv); 1206 int numtype = grok_number (start, len, &uv);
1107 } 1215 }
1108 1216
1109 len -= *start == '-' ? 1 : 0; 1217 len -= *start == '-' ? 1 : 0;
1110 1218
1111 // does not fit into IV or UV, try NV 1219 // does not fit into IV or UV, try NV
1112 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1220 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 1221 // fits into NV without loss of precision
1118 return newSVnv (Atof (start)); 1222 return newSVnv (json_atof (start));
1119 1223
1120 // everything else fails, convert it to a string 1224 // everything else fails, convert it to a string
1121 return newSVpvn (start, dec->cur - start); 1225 return newSVpvn (start, dec->cur - start);
1122 } 1226 }
1123 1227
1124 // loss of precision here 1228 // loss of precision here
1125 return newSVnv (Atof (start)); 1229 return newSVnv (json_atof (start));
1126 1230
1127fail: 1231fail:
1128 return 0; 1232 return 0;
1129} 1233}
1130 1234
1299 dSP; 1403 dSP;
1300 int count; 1404 int count;
1301 1405
1302 ENTER; SAVETMPS; PUSHMARK (SP); 1406 ENTER; SAVETMPS; PUSHMARK (SP);
1303 XPUSHs (HeVAL (he)); 1407 XPUSHs (HeVAL (he));
1408 sv_2mortal (sv);
1304 1409
1305 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1410 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1306 1411
1307 if (count == 1) 1412 if (count == 1)
1308 { 1413 {
1309 sv = newSVsv (POPs); 1414 sv = newSVsv (POPs);
1310 FREETMPS; LEAVE; 1415 FREETMPS; LEAVE;
1311 return sv; 1416 return sv;
1312 } 1417 }
1313 1418
1419 SvREFCNT_inc (sv);
1314 FREETMPS; LEAVE; 1420 FREETMPS; LEAVE;
1315 } 1421 }
1316 } 1422 }
1317 1423
1318 if (dec->json.cb_object) 1424 if (dec->json.cb_object)
1408fail: 1514fail:
1409 return 0; 1515 return 0;
1410} 1516}
1411 1517
1412static SV * 1518static SV *
1413decode_json (SV *string, JSON *json, STRLEN *offset_return) 1519decode_json (SV *string, JSON *json, char **offset_return)
1414{ 1520{
1415 dec_t dec; 1521 dec_t dec;
1416 STRLEN offset;
1417 SV *sv; 1522 SV *sv;
1418 1523
1419 /* work around bugs in 5.10 where manipulating magic values 1524 /* work around bugs in 5.10 where manipulating magic values
1420 * will perl ignore the magic in subsequent accesses 1525 * will perl ignore the magic in subsequent accesses.
1526 * also make a copy of non-PV values, to get them into a clean
1527 * state (SvPV should do that, but it's buggy, see below).
1421 */ 1528 */
1422 /*SvGETMAGIC (string);*/ 1529 /*SvGETMAGIC (string);*/
1423 if (SvMAGICAL (string)) 1530 if (SvMAGICAL (string) || !SvPOK (string))
1424 string = sv_2mortal (newSVsv (string)); 1531 string = sv_2mortal (newSVsv (string));
1425 1532
1426 SvUPGRADE (string, SVt_PV); 1533 SvUPGRADE (string, SVt_PV);
1427 1534
1428 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1535 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1433 * and hope for the best. 1540 * and hope for the best.
1434 * Damnit, SvPV_nolen still trips over yet another assertion. This 1541 * Damnit, SvPV_nolen still trips over yet another assertion. This
1435 * assertion business is seriously broken, try yet another workaround 1542 * assertion business is seriously broken, try yet another workaround
1436 * for the broken -DDEBUGGING. 1543 * for the broken -DDEBUGGING.
1437 */ 1544 */
1545 {
1438#ifdef DEBUGGING 1546#ifdef DEBUGGING
1439 offset = SvOK (string) ? sv_len (string) : 0; 1547 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1440#else 1548#else
1441 offset = SvCUR (string); 1549 STRLEN offset = SvCUR (string);
1442#endif 1550#endif
1443 1551
1444 if (offset > json->max_size && json->max_size) 1552 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", 1553 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); 1554 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1555 }
1447 1556
1448 if (json->flags & F_UTF8) 1557 if (DECODE_WANTS_OCTETS (json))
1449 sv_utf8_downgrade (string, 0); 1558 sv_utf8_downgrade (string, 0);
1450 else 1559 else
1451 sv_utf8_upgrade (string); 1560 sv_utf8_upgrade (string);
1452 1561
1453 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1562 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1463 1572
1464 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1573 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1465 1574
1466 decode_ws (&dec); 1575 decode_ws (&dec);
1467 sv = decode_sv (&dec); 1576 sv = decode_sv (&dec);
1577
1578 if (offset_return)
1579 *offset_return = dec.cur;
1468 1580
1469 if (!(offset_return || !sv)) 1581 if (!(offset_return || !sv))
1470 { 1582 {
1471 // check for trailing garbage 1583 // check for trailing garbage
1472 decode_ws (&dec); 1584 decode_ws (&dec);
1475 { 1587 {
1476 dec.err = "garbage after JSON object"; 1588 dec.err = "garbage after JSON object";
1477 SvREFCNT_dec (sv); 1589 SvREFCNT_dec (sv);
1478 sv = 0; 1590 sv = 0;
1479 } 1591 }
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 } 1592 }
1491 1593
1492 if (!sv) 1594 if (!sv)
1493 { 1595 {
1494 SV *uni = sv_newmortal (); 1596 SV *uni = sv_newmortal ();
1500 SAVEVPTR (PL_curcop); 1602 SAVEVPTR (PL_curcop);
1501 PL_curcop = &cop; 1603 PL_curcop = &cop;
1502 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1604 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1503 LEAVE; 1605 LEAVE;
1504 1606
1505 croak ("%s, at character offset %d [\"%s\"]", 1607 croak ("%s, at character offset %d (before \"%s\")",
1506 dec.err, 1608 dec.err,
1507 (int)offset, 1609 ptr_to_index (string, dec.cur),
1508 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1610 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1509 } 1611 }
1510 1612
1511 sv = sv_2mortal (sv); 1613 sv = sv_2mortal (sv);
1512 1614
1521 1623
1522static void 1624static void
1523incr_parse (JSON *self) 1625incr_parse (JSON *self)
1524{ 1626{
1525 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1627 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1628
1629 // the state machine here is a bit convoluted and could be simplified a lot
1630 // but this would make it slower, so...
1526 1631
1527 for (;;) 1632 for (;;)
1528 { 1633 {
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 1634 //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) 1635 switch (self->incr_mode)
1531 { 1636 {
1532 // only used for intiial whitespace skipping 1637 // only used for initial whitespace skipping
1533 case INCR_M_WS: 1638 case INCR_M_WS:
1534 for (;;) 1639 for (;;)
1535 { 1640 {
1536 if (*p > 0x20) 1641 if (*p > 0x20)
1537 { 1642 {
1643 if (*p == '#')
1644 {
1645 self->incr_mode = INCR_M_C0;
1646 goto incr_m_c;
1647 }
1648 else
1649 {
1538 self->incr_mode = INCR_M_JSON; 1650 self->incr_mode = INCR_M_JSON;
1539 goto incr_m_json; 1651 goto incr_m_json;
1652 }
1540 } 1653 }
1541 else if (!*p) 1654 else if (!*p)
1542 goto interrupt; 1655 goto interrupt;
1543 1656
1544 ++p; 1657 ++p;
1550 goto interrupt; 1663 goto interrupt;
1551 1664
1552 ++p; 1665 ++p;
1553 self->incr_mode = INCR_M_STR; 1666 self->incr_mode = INCR_M_STR;
1554 goto incr_m_str; 1667 goto incr_m_str;
1668
1669 // inside #-style comments
1670 case INCR_M_C0:
1671 case INCR_M_C1:
1672 incr_m_c:
1673 for (;;)
1674 {
1675 if (*p == '\n')
1676 {
1677 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1678 break;
1679 }
1680 else if (!*p)
1681 goto interrupt;
1682
1683 ++p;
1684 }
1685
1686 break;
1555 1687
1556 // inside a string 1688 // inside a string
1557 case INCR_M_STR: 1689 case INCR_M_STR:
1558 incr_m_str: 1690 incr_m_str:
1559 for (;;) 1691 for (;;)
1618 1750
1619 case ']': 1751 case ']':
1620 case '}': 1752 case '}':
1621 if (--self->incr_nest <= 0) 1753 if (--self->incr_nest <= 0)
1622 goto interrupt; 1754 goto interrupt;
1755 break;
1756
1757 case '#':
1758 self->incr_mode = INCR_M_C1;
1759 goto incr_m_c;
1623 } 1760 }
1624 } 1761 }
1625 } 1762 }
1626 1763
1627 modechange: 1764 modechange:
1628 ; 1765 ;
1629 } 1766 }
1630 1767
1631interrupt: 1768interrupt:
1632 self->incr_pos = p - SvPVX (self->incr_text); 1769 self->incr_pos = p - SvPVX (self->incr_text);
1770 //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 1771 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1634} 1772}
1635 1773
1636///////////////////////////////////////////////////////////////////////////// 1774/////////////////////////////////////////////////////////////////////////////
1637// XS interface functions 1775// XS interface functions
1652 json_stash = gv_stashpv ("JSON::XS" , 1); 1790 json_stash = gv_stashpv ("JSON::XS" , 1);
1653 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1791 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1654 1792
1655 json_true = get_bool ("JSON::XS::true"); 1793 json_true = get_bool ("JSON::XS::true");
1656 json_false = get_bool ("JSON::XS::false"); 1794 json_false = get_bool ("JSON::XS::false");
1795
1796 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1657} 1797}
1658 1798
1659PROTOTYPES: DISABLE 1799PROTOTYPES: DISABLE
1660 1800
1661void CLONE (...) 1801void CLONE (...)
1781 XPUSHs (decode_json (jsonstr, self, 0)); 1921 XPUSHs (decode_json (jsonstr, self, 0));
1782 1922
1783void decode_prefix (JSON *self, SV *jsonstr) 1923void decode_prefix (JSON *self, SV *jsonstr)
1784 PPCODE: 1924 PPCODE:
1785{ 1925{
1786 STRLEN offset; 1926 char *offset;
1787 EXTEND (SP, 2); 1927 EXTEND (SP, 2);
1788 PUSHs (decode_json (jsonstr, self, &offset)); 1928 PUSHs (decode_json (jsonstr, self, &offset));
1789 PUSHs (sv_2mortal (newSVuv (offset))); 1929 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1790} 1930}
1791 1931
1792void incr_parse (JSON *self, SV *jsonstr = 0) 1932void incr_parse (JSON *self, SV *jsonstr = 0)
1793 PPCODE: 1933 PPCODE:
1794{ 1934{
1795 if (!self->incr_text) 1935 if (!self->incr_text)
1796 self->incr_text = newSVpvn ("", 0); 1936 self->incr_text = newSVpvn ("", 0);
1937
1938 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1939 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1940 if (DECODE_WANTS_OCTETS (self))
1941 {
1942 if (self->incr_pos)
1943 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1944 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1945
1946 sv_utf8_downgrade (self->incr_text, 0);
1947 }
1948 else
1949 {
1950 sv_utf8_upgrade (self->incr_text);
1951
1952 if (self->incr_pos)
1953 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1954 - (U8 *)SvPVX (self->incr_text);
1955 }
1797 1956
1798 // append data, if any 1957 // append data, if any
1799 if (jsonstr) 1958 if (jsonstr)
1800 { 1959 {
1960 /* make sure both strings have same encoding */
1801 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1961 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1962 if (SvUTF8 (jsonstr))
1963 sv_utf8_downgrade (jsonstr, 0);
1802 { 1964 else
1803 /* utf-8-ness differs, need to upgrade */
1804 sv_utf8_upgrade (self->incr_text); 1965 sv_utf8_upgrade (jsonstr);
1805 1966
1806 if (self->incr_pos) 1967 /* and then just blindly append */
1807 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1808 - (U8 *)SvPVX (self->incr_text);
1809 }
1810
1811 { 1968 {
1812 STRLEN len; 1969 STRLEN len;
1813 const char *str = SvPV (jsonstr, len); 1970 const char *str = SvPV (jsonstr, len);
1814 STRLEN cur = SvCUR (self->incr_text); 1971 STRLEN cur = SvCUR (self->incr_text);
1815 1972
1823 } 1980 }
1824 1981
1825 if (GIMME_V != G_VOID) 1982 if (GIMME_V != G_VOID)
1826 do 1983 do
1827 { 1984 {
1828 STRLEN offset; 1985 char *offset;
1829 1986
1830 if (!INCR_DONE (self)) 1987 if (!INCR_DONE (self))
1831 { 1988 {
1832 incr_parse (self); 1989 incr_parse (self);
1833 1990
1839 break; 1996 break;
1840 } 1997 }
1841 1998
1842 XPUSHs (decode_json (self->incr_text, self, &offset)); 1999 XPUSHs (decode_json (self->incr_text, self, &offset));
1843 2000
1844 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1845 self->incr_pos -= offset; 2001 self->incr_pos -= offset - SvPVX (self->incr_text);
1846 self->incr_nest = 0; 2002 self->incr_nest = 0;
1847 self->incr_mode = 0; 2003 self->incr_mode = 0;
2004
2005 sv_chop (self->incr_text, offset);
1848 } 2006 }
1849 while (GIMME_V == G_ARRAY); 2007 while (GIMME_V == G_ARRAY);
1850} 2008}
1851 2009
1852SV *incr_text (JSON *self) 2010SV *incr_text (JSON *self)
1913 json_init (&json); 2071 json_init (&json);
1914 json.flags |= ix; 2072 json.flags |= ix;
1915 XPUSHs (decode_json (jsonstr, &json, 0)); 2073 XPUSHs (decode_json (jsonstr, &json, 0));
1916} 2074}
1917 2075
1918

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines