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.76 by root, Thu Mar 20 00:56:37 2008 UTC vs.
Revision 1.77 by root, Tue Mar 25 06:37:38 2008 UTC

78#endif 78#endif
79 79
80static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
81static SV *json_true, *json_false; 81static SV *json_true, *json_false;
82 82
83enum {
84 INCR_M_WS = 0, // initial whitespace skipping, must be 0
85 INCR_M_STR, // inside string
86 INCR_M_BS, // inside backslash
87 INCR_M_JSON // outside anything, count nesting
88};
89
90#define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON)
91
83typedef struct { 92typedef struct {
84 U32 flags; 93 U32 flags;
85 SV *cb_object; 94 SV *cb_object;
86 HV *cb_sk_object; 95 HV *cb_sk_object;
96
97 // for the incremental parser
98 SV *incr_text; // the source text so far
99 STRLEN incr_pos; // the current offset into the text
100 int incr_nest; // {[]}-nesting level
101 int incr_mode;
87} JSON; 102} JSON;
88 103
89///////////////////////////////////////////////////////////////////////////// 104/////////////////////////////////////////////////////////////////////////////
90// utility functions 105// utility functions
91 106
1171 char *p = dec->cur; 1186 char *p = dec->cur;
1172 char *e = p + 24; // only try up to 24 bytes 1187 char *e = p + 24; // only try up to 24 bytes
1173 1188
1174 for (;;) 1189 for (;;)
1175 { 1190 {
1176 // the >= 0x80 is true on most architectures 1191 // the >= 0x80 is false on most architectures
1177 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') 1192 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
1178 { 1193 {
1179 // slow path, back up and use decode_str 1194 // slow path, back up and use decode_str
1180 SV *key = decode_str (dec); 1195 SV *key = decode_str (dec);
1181 if (!key) 1196 if (!key)
1373fail: 1388fail:
1374 return 0; 1389 return 0;
1375} 1390}
1376 1391
1377static SV * 1392static SV *
1378decode_json (SV *string, JSON *json, UV *offset_return) 1393decode_json (SV *string, JSON *json, STRLEN *offset_return)
1379{ 1394{
1380 dec_t dec; 1395 dec_t dec;
1381 UV offset; 1396 STRLEN offset;
1382 SV *sv; 1397 SV *sv;
1383 1398
1384 SvGETMAGIC (string); 1399 SvGETMAGIC (string);
1385 SvUPGRADE (string, SVt_PV); 1400 SvUPGRADE (string, SVt_PV);
1386 1401
1456 1471
1457 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1472 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1458 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1473 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1459 1474
1460 return sv; 1475 return sv;
1476}
1477
1478/////////////////////////////////////////////////////////////////////////////
1479// incremental parser
1480
1481static void
1482incr_parse (JSON *self)
1483{
1484 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1485
1486 for (;;)
1487 {
1488 //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
1489 switch (self->incr_mode)
1490 {
1491 // only used for intiial whitespace skipping
1492 case INCR_M_WS:
1493 for (;;)
1494 {
1495 if (*p > 0x20)
1496 {
1497 self->incr_mode = INCR_M_JSON;
1498 goto incr_m_json;
1499 }
1500 else if (!*p)
1501 goto interrupt;
1502
1503 ++p;
1504 }
1505
1506 // skip a single char inside a string (for \\-processing)
1507 case INCR_M_BS:
1508 if (!*p)
1509 goto interrupt;
1510
1511 ++p;
1512 self->incr_mode = INCR_M_STR;
1513 goto incr_m_str;
1514
1515 // inside a string
1516 case INCR_M_STR:
1517 incr_m_str:
1518 for (;;)
1519 {
1520 if (*p == '"')
1521 {
1522 ++p;
1523 self->incr_mode = INCR_M_JSON;
1524
1525 if (!self->incr_nest)
1526 goto interrupt;
1527
1528 goto incr_m_json;
1529 }
1530 else if (*p == '\\')
1531 {
1532 ++p; // "virtually" consumes character after \
1533
1534 if (!*p) // if at end of string we have to switch modes
1535 {
1536 self->incr_mode = INCR_M_BS;
1537 goto interrupt;
1538 }
1539 }
1540 else if (!*p)
1541 goto interrupt;
1542
1543 ++p;
1544 }
1545
1546 // after initial ws, outside string
1547 case INCR_M_JSON:
1548 incr_m_json:
1549 for (;;)
1550 {
1551 switch (*p++)
1552 {
1553 case 0:
1554 --p;
1555 goto interrupt;
1556
1557 case 0x09:
1558 case 0x0a:
1559 case 0x0d:
1560 case 0x20:
1561 if (!self->incr_nest)
1562 {
1563 --p; // do not eat the whitespace, let the next round do it
1564 goto interrupt;
1565 }
1566 break;
1567
1568 case '"':
1569 self->incr_mode = INCR_M_STR;
1570 goto incr_m_str;
1571
1572 case '[':
1573 case '{':
1574 ++self->incr_nest;
1575 break;
1576
1577 case ']':
1578 case '}':
1579 if (!--self->incr_nest)
1580 goto interrupt;
1581 }
1582 }
1583 }
1584
1585 modechange:
1586 ;
1587 }
1588
1589interrupt:
1590 self->incr_pos = p - SvPVX (self->incr_text);
1591 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1461} 1592}
1462 1593
1463///////////////////////////////////////////////////////////////////////////// 1594/////////////////////////////////////////////////////////////////////////////
1464// XS interface functions 1595// XS interface functions
1465 1596
1628 XPUSHs (decode_json (jsonstr, self, 0)); 1759 XPUSHs (decode_json (jsonstr, self, 0));
1629 1760
1630void decode_prefix (JSON *self, SV *jsonstr) 1761void decode_prefix (JSON *self, SV *jsonstr)
1631 PPCODE: 1762 PPCODE:
1632{ 1763{
1633 UV offset; 1764 STRLEN offset;
1634 EXTEND (SP, 2); 1765 EXTEND (SP, 2);
1635 PUSHs (decode_json (jsonstr, self, &offset)); 1766 PUSHs (decode_json (jsonstr, self, &offset));
1636 PUSHs (sv_2mortal (newSVuv (offset))); 1767 PUSHs (sv_2mortal (newSVuv (offset)));
1637} 1768}
1769
1770void incr_parse (JSON *self, SV *jsonstr = 0)
1771 PPCODE:
1772{
1773 if (!self->incr_text)
1774 self->incr_text = newSVpvn ("", 0);
1775
1776 // append data, if any
1777 if (jsonstr)
1778 {
1779 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text))
1780 {
1781 /* utf-8-ness differs, need to upgrade */
1782 sv_utf8_upgrade (self->incr_text);
1783
1784 if (self->incr_pos)
1785 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1786 - (U8 *)SvPVX (self->incr_text);
1787 }
1788
1789 {
1790 STRLEN len;
1791 const char *str = SvPV (jsonstr, len);
1792 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1);
1793 Move (str, SvEND (self->incr_text), len, char);
1794 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1795 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1796 }
1797 }
1798
1799 if (GIMME_V != G_VOID)
1800 do
1801 {
1802 STRLEN offset;
1803
1804 incr_parse (self);
1805
1806 if (!INCR_DONE (self))
1807 break;
1808
1809 XPUSHs (decode_json (self->incr_text, self, &offset));
1810
1811 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1812 self->incr_pos -= offset;
1813 self->incr_nest = 0;
1814 self->incr_mode = 0;
1815 }
1816 while (GIMME_V == G_ARRAY);
1817}
1818
1819SV *incr_text (JSON *self)
1820 ATTRS: lvalue
1821 CODE:
1822{
1823 if (self->incr_pos)
1824 croak ("incr_text can only be called after a successful incr_parse call in scalar context %d", self->incr_pos);//D
1825
1826 RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef;
1827}
1828 OUTPUT:
1829 RETVAL
1638 1830
1639void DESTROY (JSON *self) 1831void DESTROY (JSON *self)
1640 CODE: 1832 CODE:
1641 SvREFCNT_dec (self->cb_sk_object); 1833 SvREFCNT_dec (self->cb_sk_object);
1642 SvREFCNT_dec (self->cb_object); 1834 SvREFCNT_dec (self->cb_object);
1835 SvREFCNT_dec (self->incr_text);
1643 1836
1644PROTOTYPES: ENABLE 1837PROTOTYPES: ENABLE
1645 1838
1646void encode_json (SV *scalar) 1839void encode_json (SV *scalar)
1647 ALIAS: 1840 ALIAS:
1661{ 1854{
1662 JSON json = { F_DEFAULT | ix }; 1855 JSON json = { F_DEFAULT | ix };
1663 XPUSHs (decode_json (jsonstr, &json, 0)); 1856 XPUSHs (decode_json (jsonstr, &json, 0));
1664} 1857}
1665 1858
1859

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines