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.135 by root, Thu Nov 15 20:49:12 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
1502 1504
1503 DEC_DEC_DEPTH; 1505 DEC_DEC_DEPTH;
1504 sv = newRV_noinc ((SV *)hv); 1506 sv = newRV_noinc ((SV *)hv);
1505 1507
1506 // check filter callbacks 1508 // check filter callbacks
1507 if (dec->json.flags & F_HOOK) 1509 if (expect_false (dec->json.flags & F_HOOK))
1508 { 1510 {
1509 if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 1511 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1510 { 1512 {
1511 HE *cb, *he; 1513 HE *cb, *he;
1512 1514
1554 XPUSHs (sv_2mortal (sv)); 1556 XPUSHs (sv_2mortal (sv));
1555 1557
1556 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1558 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1557 1559
1558 if (count == 1) 1560 if (count == 1)
1559 {
1560 sv = newSVsv (POPs); 1561 sv = newSVsv (POPs);
1561 FREETMPS; LEAVE;
1562 return sv;
1563 }
1564 else if (count) 1562 else if (count == 0)
1563 SvREFCNT_inc (sv);
1564 else
1565 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");
1566 1566
1567 SvREFCNT_inc (sv);
1568 FREETMPS; LEAVE; 1567 FREETMPS; LEAVE;
1569 } 1568 }
1570 } 1569 }
1571 1570
1572 return sv; 1571 return sv;
1843 // 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
1844 // but this would make it slower, so... 1843 // but this would make it slower, so...
1845 1844
1846 for (;;) 1845 for (;;)
1847 { 1846 {
1848 //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
1849 switch (self->incr_mode) 1847 switch (self->incr_mode)
1850 { 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
1851 // only used for initial whitespace skipping 1858 // only used for initial whitespace skipping
1852 case INCR_M_WS: 1859 case INCR_M_WS:
1853 for (;;) 1860 for (;;)
1854 { 1861 {
1855 if (*p > 0x20) 1862 if (*p > 0x20)
1897 ++p; 1904 ++p;
1898 } 1905 }
1899 1906
1900 break; 1907 break;
1901 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
1902 // inside a string 1943 // inside a string
1903 case INCR_M_STR: 1944 case INCR_M_STR:
1904 incr_m_str: 1945 incr_m_str:
1905 for (;;) 1946 for (;;)
1906 { 1947 {
1907 if (*p == '"') 1948 if (*p == '"')
1908 { 1949 {
1909 ++p; 1950 ++p;
1910 self->incr_mode = INCR_M_JSON;
1911
1912 if (!self->incr_nest)
1913 goto interrupt;
1914
1915 goto incr_m_json; 1951 goto end_of_scalar;
1916 } 1952 }
1917 else if (*p == '\\') 1953 else if (*p == '\\')
1918 { 1954 {
1919 ++p; // "virtually" consumes character after \ 1955 ++p; // "virtually" consumes character after \
1920 1956
1949 { 1985 {
1950 --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
1951 goto interrupt; 1987 goto interrupt;
1952 } 1988 }
1953 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;
1954 2005
1955 case '"': 2006 case '"':
1956 self->incr_mode = INCR_M_STR; 2007 self->incr_mode = INCR_M_STR;
1957 goto incr_m_str; 2008 goto incr_m_str;
1958 2009

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines