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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines