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.134 by root, Thu Nov 15 20:13:03 2018 UTC vs.
Revision 1.138 by root, Wed Mar 6 07:21:17 2019 UTC

96static SV *bool_false, *bool_true; 96static SV *bool_false, *bool_true;
97static SV *sv_json; 97static SV *sv_json;
98 98
99enum { 99enum {
100 INCR_M_WS = 0, // initial whitespace skipping, must be 0 100 INCR_M_WS = 0, // initial whitespace skipping, must be 0
101 INCR_M_TFN, // inside true/false/null
102 INCR_M_NUM, // inside number
101 INCR_M_STR, // inside string 103 INCR_M_STR, // inside string
102 INCR_M_BS, // inside backslash 104 INCR_M_BS, // inside backslash
103 INCR_M_C0, // inside comment in initial whitespace sequence 105 INCR_M_C0, // inside comment in initial whitespace sequence
104 INCR_M_C1, // inside comment in other places 106 INCR_M_C1, // inside comment in other places
105 INCR_M_JSON // outside anything, count nesting 107 INCR_M_JSON // outside anything, count nesting
125} JSON; 127} JSON;
126 128
127INLINE void 129INLINE void
128json_init (JSON *json) 130json_init (JSON *json)
129{ 131{
130 Zero (json, 1, JSON); 132 static const JSON init = { F_ALLOW_NONREF, 512 };
131 json->max_depth = 512; 133
134 *json = init;
132} 135}
133 136
134///////////////////////////////////////////////////////////////////////////// 137/////////////////////////////////////////////////////////////////////////////
135// utility functions 138// utility functions
136 139
1501 1504
1502 DEC_DEC_DEPTH; 1505 DEC_DEC_DEPTH;
1503 sv = newRV_noinc ((SV *)hv); 1506 sv = newRV_noinc ((SV *)hv);
1504 1507
1505 // check filter callbacks 1508 // check filter callbacks
1506 if (dec->json.flags & F_HOOK) 1509 if (expect_false (dec->json.flags & F_HOOK))
1507 { 1510 {
1508 if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 1511 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1509 { 1512 {
1510 HE *cb, *he; 1513 HE *cb, *he;
1511 1514
1553 XPUSHs (sv_2mortal (sv)); 1556 XPUSHs (sv_2mortal (sv));
1554 1557
1555 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1558 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1556 1559
1557 if (count == 1) 1560 if (count == 1)
1558 {
1559 sv = newSVsv (POPs); 1561 sv = newSVsv (POPs);
1560 FREETMPS; LEAVE;
1561 return sv;
1562 }
1563 else if (count) 1562 else if (count == 0)
1563 SvREFCNT_inc (sv);
1564 else
1564 croak ("filter_json_object callbacks must not return more than one scalar"); 1565 croak ("filter_json_object callbacks must not return more than one scalar");
1565 1566
1566 SvREFCNT_inc (sv);
1567 FREETMPS; LEAVE; 1567 FREETMPS; LEAVE;
1568 } 1568 }
1569 } 1569 }
1570 1570
1571 return sv; 1571 return sv;
1794 else if (sv) 1794 else if (sv)
1795 { 1795 {
1796 // check for trailing garbage 1796 // check for trailing garbage
1797 decode_ws (&dec); 1797 decode_ws (&dec);
1798 1798
1799 if (*dec.cur) 1799 if (dec.cur != dec.end)
1800 { 1800 {
1801 dec.err = "garbage after JSON object"; 1801 dec.err = "garbage after JSON object";
1802 SvREFCNT_dec (sv); 1802 SvREFCNT_dec (sv);
1803 sv = 0; 1803 sv = 0;
1804 } 1804 }
1842 // the state machine here is a bit convoluted and could be simplified a lot 1842 // the state machine here is a bit convoluted and could be simplified a lot
1843 // but this would make it slower, so... 1843 // but this would make it slower, so...
1844 1844
1845 for (;;) 1845 for (;;)
1846 { 1846 {
1847 //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
1848 switch (self->incr_mode) 1847 switch (self->incr_mode)
1849 { 1848 {
1849 // reached end of a scalar, see if we are inside a nested structure or not
1850 end_of_scalar:
1851 self->incr_mode = INCR_M_JSON;
1852
1853 if (self->incr_nest) // end of a scalar inside array, object or tag
1854 goto incr_m_json;
1855 else // end of scalar outside structure, json text ends here
1856 goto interrupt;
1857
1850 // only used for initial whitespace skipping 1858 // only used for initial whitespace skipping
1851 case INCR_M_WS: 1859 case INCR_M_WS:
1852 for (;;) 1860 for (;;)
1853 { 1861 {
1854 if (*p > 0x20) 1862 if (*p > 0x20)
1896 ++p; 1904 ++p;
1897 } 1905 }
1898 1906
1899 break; 1907 break;
1900 1908
1909 // inside true/false/null
1910 case INCR_M_TFN:
1911 incr_m_tfn:
1912 for (;;)
1913 switch (*p++)
1914 {
1915 case 'r': case 'u': case 'e': // tRUE, falsE, nUll
1916 case 'a': case 'l': case 's': // fALSe, nuLL
1917 // allowed
1918 break;
1919
1920 default:
1921 --p;
1922 goto end_of_scalar;
1923 }
1924
1925 // inside a number
1926 case INCR_M_NUM:
1927 incr_m_num:
1928 for (;;)
1929 switch (*p++)
1930 {
1931 case 'e': case 'E': case '.': case '+':
1932 case '-':
1933 case '0': case '1': case '2': case '3': case '4':
1934 case '5': case '6': case '7': case '8': case '9':
1935 // allowed
1936 break;
1937
1938 default:
1939 --p;
1940 goto end_of_scalar;
1941 }
1942
1901 // inside a string 1943 // inside a string
1902 case INCR_M_STR: 1944 case INCR_M_STR:
1903 incr_m_str: 1945 incr_m_str:
1904 for (;;) 1946 for (;;)
1905 { 1947 {
1906 if (*p == '"') 1948 if (*p == '"')
1907 { 1949 {
1908 ++p; 1950 ++p;
1909 self->incr_mode = INCR_M_JSON;
1910
1911 if (!self->incr_nest)
1912 goto interrupt;
1913
1914 goto incr_m_json; 1951 goto end_of_scalar;
1915 } 1952 }
1916 else if (*p == '\\') 1953 else if (*p == '\\')
1917 { 1954 {
1918 ++p; // "virtually" consumes character after \ 1955 ++p; // "virtually" consumes character after \
1919 1956
1948 { 1985 {
1949 --p; // do not eat the whitespace, let the next round do it 1986 --p; // do not eat the whitespace, let the next round do it
1950 goto interrupt; 1987 goto interrupt;
1951 } 1988 }
1952 break; 1989 break;
1990
1991 // the following three blocks handle scalars. this makes the parser
1992 // more strict than required inside arrays or objects, and could
1993 // be moved to a special case on the toplevel (except strings)
1994 case 't':
1995 case 'f':
1996 case 'n':
1997 self->incr_mode = INCR_M_TFN;
1998 goto incr_m_tfn;
1999
2000 case '-':
2001 case '0': case '1': case '2': case '3': case '4':
2002 case '5': case '6': case '7': case '8': case '9':
2003 self->incr_mode = INCR_M_NUM;
2004 goto incr_m_num;
1953 2005
1954 case '"': 2006 case '"':
1955 self->incr_mode = INCR_M_STR; 2007 self->incr_mode = INCR_M_STR;
1956 goto incr_m_str; 2008 goto incr_m_str;
1957 2009

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines