… | |
… | |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
43 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
44 | |
44 | |
45 | #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 |
46 | |
46 | |
|
|
47 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
|
|
48 | |
47 | #define SB do { |
49 | #define SB do { |
48 | #define SE } while (0) |
50 | #define SE } while (0) |
49 | |
51 | |
50 | #if __GNUC__ >= 3 |
52 | #if __GNUC__ >= 3 |
51 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
53 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
… | |
… | |
258 | } |
260 | } |
259 | |
261 | |
260 | // this relies greatly on the quality of the pow () |
262 | // this relies greatly on the quality of the pow () |
261 | // implementation of the platform, but a good |
263 | // implementation of the platform, but a good |
262 | // implementation is hard to beat. |
264 | // implementation is hard to beat. |
|
|
265 | // (IEEE 754 conformant ones are required to be exact) |
263 | if (postdp) *expo -= eaccum; |
266 | if (postdp) *expo -= eaccum; |
264 | *accum += uaccum * Perl_pow (10., *expo); |
267 | *accum += uaccum * Perl_pow (10., *expo); |
265 | *expo += eaccum; |
268 | *expo += eaccum; |
266 | } |
269 | } |
267 | |
270 | |
… | |
… | |
1401 | dSP; |
1404 | dSP; |
1402 | int count; |
1405 | int count; |
1403 | |
1406 | |
1404 | ENTER; SAVETMPS; PUSHMARK (SP); |
1407 | ENTER; SAVETMPS; PUSHMARK (SP); |
1405 | XPUSHs (HeVAL (he)); |
1408 | XPUSHs (HeVAL (he)); |
|
|
1409 | sv_2mortal (sv); |
1406 | |
1410 | |
1407 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1411 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1408 | |
1412 | |
1409 | if (count == 1) |
1413 | if (count == 1) |
1410 | { |
1414 | { |
1411 | sv = newSVsv (POPs); |
1415 | sv = newSVsv (POPs); |
1412 | FREETMPS; LEAVE; |
1416 | FREETMPS; LEAVE; |
1413 | return sv; |
1417 | return sv; |
1414 | } |
1418 | } |
1415 | |
1419 | |
|
|
1420 | SvREFCNT_inc (sv); |
1416 | FREETMPS; LEAVE; |
1421 | FREETMPS; LEAVE; |
1417 | } |
1422 | } |
1418 | } |
1423 | } |
1419 | |
1424 | |
1420 | if (dec->json.cb_object) |
1425 | if (dec->json.cb_object) |
… | |
… | |
1516 | { |
1521 | { |
1517 | dec_t dec; |
1522 | dec_t dec; |
1518 | SV *sv; |
1523 | SV *sv; |
1519 | |
1524 | |
1520 | /* work around bugs in 5.10 where manipulating magic values |
1525 | /* work around bugs in 5.10 where manipulating magic values |
1521 | * will perl ignore the magic in subsequent accesses |
1526 | * will perl ignore the magic in subsequent accesses. |
|
|
1527 | * also make a copy of non-PV values, to get them into a clean |
|
|
1528 | * state (SvPV should do that, but it's buggy, see below). |
1522 | */ |
1529 | */ |
1523 | /*SvGETMAGIC (string);*/ |
1530 | /*SvGETMAGIC (string);*/ |
1524 | if (SvMAGICAL (string)) |
1531 | if (SvMAGICAL (string) || !SvPOK (string)) |
1525 | string = sv_2mortal (newSVsv (string)); |
1532 | string = sv_2mortal (newSVsv (string)); |
1526 | |
1533 | |
1527 | SvUPGRADE (string, SVt_PV); |
1534 | SvUPGRADE (string, SVt_PV); |
1528 | |
1535 | |
1529 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1536 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1546 | if (offset > json->max_size && json->max_size) |
1553 | if (offset > json->max_size && json->max_size) |
1547 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1554 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1548 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1555 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1549 | } |
1556 | } |
1550 | |
1557 | |
1551 | if (json->flags & F_UTF8) |
1558 | if (DECODE_WANTS_OCTETS (json)) |
1552 | sv_utf8_downgrade (string, 0); |
1559 | sv_utf8_downgrade (string, 0); |
1553 | else |
1560 | else |
1554 | sv_utf8_upgrade (string); |
1561 | sv_utf8_upgrade (string); |
1555 | |
1562 | |
1556 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1563 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1927 | PPCODE: |
1934 | PPCODE: |
1928 | { |
1935 | { |
1929 | if (!self->incr_text) |
1936 | if (!self->incr_text) |
1930 | self->incr_text = newSVpvn ("", 0); |
1937 | self->incr_text = newSVpvn ("", 0); |
1931 | |
1938 | |
|
|
1939 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
1940 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
1941 | if (DECODE_WANTS_OCTETS (self)) |
|
|
1942 | { |
|
|
1943 | if (self->incr_pos) |
|
|
1944 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
1945 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
1946 | |
|
|
1947 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
1948 | } |
|
|
1949 | else |
|
|
1950 | { |
|
|
1951 | sv_utf8_upgrade (self->incr_text); |
|
|
1952 | |
|
|
1953 | if (self->incr_pos) |
|
|
1954 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1955 | - (U8 *)SvPVX (self->incr_text); |
|
|
1956 | } |
|
|
1957 | |
1932 | // append data, if any |
1958 | // append data, if any |
1933 | if (jsonstr) |
1959 | if (jsonstr) |
1934 | { |
1960 | { |
|
|
1961 | /* make sure both strings have same encoding */ |
|
|
1962 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1935 | if (SvUTF8 (jsonstr)) |
1963 | if (SvUTF8 (jsonstr)) |
|
|
1964 | sv_utf8_downgrade (jsonstr, 0); |
1936 | { |
1965 | else |
1937 | if (!SvUTF8 (self->incr_text)) |
|
|
1938 | { |
|
|
1939 | /* utf-8-ness differs, need to upgrade */ |
|
|
1940 | sv_utf8_upgrade (self->incr_text); |
|
|
1941 | |
|
|
1942 | if (self->incr_pos) |
|
|
1943 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1944 | - (U8 *)SvPVX (self->incr_text); |
|
|
1945 | } |
|
|
1946 | } |
|
|
1947 | else if (SvUTF8 (self->incr_text)) |
|
|
1948 | sv_utf8_upgrade (jsonstr); |
1966 | sv_utf8_upgrade (jsonstr); |
1949 | |
1967 | |
|
|
1968 | /* and then just blindly append */ |
1950 | { |
1969 | { |
1951 | STRLEN len; |
1970 | STRLEN len; |
1952 | const char *str = SvPV (jsonstr, len); |
1971 | const char *str = SvPV (jsonstr, len); |
1953 | STRLEN cur = SvCUR (self->incr_text); |
1972 | STRLEN cur = SvCUR (self->incr_text); |
1954 | |
1973 | |
… | |
… | |
1973 | if (self->incr_pos > self->max_size && self->max_size) |
1992 | if (self->incr_pos > self->max_size && self->max_size) |
1974 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1993 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1975 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1994 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1976 | |
1995 | |
1977 | if (!INCR_DONE (self)) |
1996 | if (!INCR_DONE (self)) |
|
|
1997 | { |
|
|
1998 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
1999 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2000 | { |
|
|
2001 | self->incr_pos = 0; |
|
|
2002 | SvCUR_set (self->incr_text, 0); |
|
|
2003 | } |
|
|
2004 | |
1978 | break; |
2005 | break; |
|
|
2006 | } |
1979 | } |
2007 | } |
1980 | |
2008 | |
1981 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2009 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1982 | |
2010 | |
1983 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2011 | self->incr_pos -= offset - SvPVX (self->incr_text); |