--- JSON-XS/XS.xs 2007/03/25 21:52:47 1.19 +++ JSON-XS/XS.xs 2007/06/06 17:49:01 1.37 @@ -5,15 +5,21 @@ #include "assert.h" #include "string.h" #include "stdlib.h" +#include "stdio.h" + +#if defined(__BORLANDC__) || defined(_MSC_VER) +# define snprintf _snprintf // C compilers have this in stdio.h +#endif #define F_ASCII 0x00000001UL -#define F_UTF8 0x00000002UL -#define F_INDENT 0x00000004UL -#define F_CANONICAL 0x00000008UL -#define F_SPACE_BEFORE 0x00000010UL -#define F_SPACE_AFTER 0x00000020UL -#define F_ALLOW_NONREF 0x00000080UL -#define F_SHRINK 0x00000100UL +#define F_LATIN1 0x00000002UL +#define F_UTF8 0x00000004UL +#define F_INDENT 0x00000008UL +#define F_CANONICAL 0x00000010UL +#define F_SPACE_BEFORE 0x00000020UL +#define F_SPACE_AFTER 0x00000040UL +#define F_ALLOW_NONREF 0x00000100UL +#define F_SHRINK 0x00000200UL #define F_MAXDEPTH 0xf8000000UL #define S_MAXDEPTH 27 @@ -23,17 +29,27 @@ // F_BLESSED? <=> { $__class__$ => } #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER -#define F_DEFAULT (13UL << S_MAXDEPTH) +#define F_DEFAULT (9UL << S_MAXDEPTH) #define INIT_SIZE 32 // initial scalar size to be allocated #define INDENT_STEP 3 // spaces per indentation level -#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character #define SHORT_STRING_LEN 512 // special-case strings of up to this size #define SB do { #define SE } while (0) +#if __GNUC__ >= 3 +# define expect(expr,value) __builtin_expect ((expr),(value)) +# define inline inline +#else +# define expect(expr,value) (expr) +# define inline static +#endif + +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + static HV *json_stash; // JSON::XS:: ///////////////////////////////////////////////////////////////////////////// @@ -67,10 +83,10 @@ // we special-case "safe" characters from U+80 .. U+7FF, // but use the very good perl function to parse anything else. // note that we never call this function for a ascii codepoints -static UV +inline UV decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) { - if (s[0] > 0xdf || s[0] < 0xc2) + if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) { @@ -78,7 +94,10 @@ return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); } else - return (UV)-1; + { + *clen = (STRLEN)-1; + return (UV)-1; + } } ///////////////////////////////////////////////////////////////////////////// @@ -95,19 +114,19 @@ U32 maxdepth; // max. indentation/recursion level } enc_t; -static void +inline void need (enc_t *enc, STRLEN len) { - if (enc->cur + len >= enc->end) + if (expect_false (enc->cur + len >= enc->end)) { STRLEN cur = enc->cur - SvPVX (enc->sv); SvGROW (enc->sv, cur + len + 1); enc->cur = SvPVX (enc->sv) + cur; - enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); + enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; } } -static void +inline void encode_ch (enc_t *enc, char ch) { need (enc, 1); @@ -125,15 +144,15 @@ { unsigned char ch = *(unsigned char *)str; - if (ch >= 0x20 && ch < 0x80) // most common case + if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case { - if (ch == '"') // but with slow exceptions + if (expect_false (ch == '"')) // but with slow exceptions { need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = '"'; } - else if (ch == '\\') + else if (expect_false (ch == '\\')) { need (enc, len += 1); *enc->cur++ = '\\'; @@ -175,7 +194,7 @@ if (uch > 0x10FFFFUL) croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); - if (uch < 0x80 || enc->flags & F_ASCII) + if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) { if (uch > 0xFFFFUL) { @@ -199,6 +218,11 @@ str += clen; } + else if (enc->flags & F_LATIN1) + { + *enc->cur++ = uch; + str += clen; + } else if (is_utf8) { need (enc, len += clen); @@ -210,7 +234,7 @@ } else { - need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed + need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); ++str; } @@ -222,7 +246,7 @@ } } -static void +inline void encode_indent (enc_t *enc) { if (enc->flags & F_INDENT) @@ -235,14 +259,14 @@ } } -static void +inline void encode_space (enc_t *enc) { need (enc, 1); encode_ch (enc, ' '); } -static void +inline void encode_nl (enc_t *enc) { if (enc->flags & F_INDENT) @@ -252,7 +276,7 @@ } } -static void +inline void encode_comma (enc_t *enc) { encode_ch (enc, ','); @@ -270,6 +294,9 @@ { int i, len = av_len (av); + if (enc->indent >= enc->maxdepth) + croak ("data structure too deep (hit recursion limit)"); + encode_ch (enc, '['); encode_nl (enc); ++enc->indent; @@ -345,6 +372,9 @@ { int count, i; + if (enc->indent >= enc->maxdepth) + croak ("data structure too deep (hit recursion limit)"); + encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; if ((count = hv_iterinit (hv))) @@ -355,8 +385,13 @@ // that randomises hash orderings if (enc->flags & F_CANONICAL) { - HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode int fast = 1; + HE *he; +#if defined(__BORLANDC__) || defined(_MSC_VER) + HE **hes = _alloca (count * sizeof (HE)); +#else + HE *hes [count]; // if your compiler dies here, you need to enable C99 mode +#endif i = 0; while ((he = hv_iternext (hv))) @@ -401,7 +436,6 @@ } else { - SV *sv; HE *he = hv_iternext (hv); for (;;) @@ -422,6 +456,34 @@ --enc->indent; encode_indent (enc); encode_ch (enc, '}'); } +// encode objects, arrays and special \0=false and \1=true values. +static void +encode_rv (enc_t *enc, SV *sv) +{ + svtype svt; + + SvGETMAGIC (sv); + svt = SvTYPE (sv); + + if (svt == SVt_PVHV) + encode_hv (enc, (HV *)sv); + else if (svt == SVt_PVAV) + encode_av (enc, (AV *)sv); + else if (svt < SVt_PVAV) + { + if (SvNIOK (sv) && SvIV (sv) == 0) + encode_str (enc, "false", 5, 0); + else if (SvNIOK (sv) && SvIV (sv) == 1) + encode_str (enc, "true", 4, 0); + else + croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); + } + else + croak ("encountered %s, but JSON can only represent references to arrays or hashes", + SvPV_nolen (sv_2mortal (newRV_inc (sv)))); +} + static void encode_sv (enc_t *enc, SV *sv) { @@ -443,29 +505,41 @@ } else if (SvIOKp (sv)) { - need (enc, 64); - enc->cur += - SvIsUV(sv) - ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) - : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); - } - else if (SvROK (sv)) - { - SV *rv = SvRV (sv); - - if (enc->indent >= enc->maxdepth) - croak ("data structure too deep (hit recursion limit)"); - - switch (SvTYPE (rv)) + // we assume we can always read an IV as a UV + if (SvUV (sv) & ~(UV)0x7fff) { - case SVt_PVAV: encode_av (enc, (AV *)rv); break; - case SVt_PVHV: encode_hv (enc, (HV *)rv); break; - - default: - croak ("encountered %s, but JSON can only represent references to arrays or hashes", - SvPV_nolen (sv)); + need (enc, sizeof (UV) * 3); + enc->cur += + SvIsUV(sv) + ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) + : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); + } + else + { + // optimise the "small number case" + // code will likely be branchless and use only a single multiplication + I32 i = SvIV (sv); + U32 u; + + need (enc, 6); + + *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; + u = i < 0 ? -i : i; + + // convert to 4.28 fixed-point representation + u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits + + char digit, nz = 0; + + digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; + digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; + digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; + digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; + digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; } } + else if (SvROK (sv)) + encode_rv (enc, SvRV (sv)); else if (!SvOK (sv)) encode_str (enc, "null", 4, 0); else @@ -476,10 +550,11 @@ static SV * encode_json (SV *scalar, U32 flags) { + enc_t enc; + if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); - enc_t enc; enc.flags = flags; enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); enc.cur = SvPVX (enc.sv); @@ -490,10 +565,11 @@ SvPOK_only (enc.sv); encode_sv (&enc, scalar); - if (!(flags & (F_ASCII | F_UTF8))) - SvUTF8_on (enc.sv); - SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); + *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings + + if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) + SvUTF8_on (enc.sv); if (enc.flags & F_SHRINK) shrink (enc.sv); @@ -515,7 +591,7 @@ U32 maxdepth; // recursion depth limit } dec_t; -static void +inline void decode_ws (dec_t *dec) { for (;;) @@ -551,10 +627,10 @@ signed char d1, d2, d3, d4; unsigned char *cur = (unsigned char *)dec->cur; - d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); - d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); - d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); - d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); + d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); + d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); + d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); + d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); dec->cur += 4; @@ -575,19 +651,19 @@ do { - char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; + char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; char *cur = buf; do { unsigned char ch = *(unsigned char *)dec->cur++; - if (ch == '"') + if (expect_false (ch == '"')) { --dec->cur; break; } - else if (ch == '\\') + else if (expect_false (ch == '\\')) { switch (*dec->cur) { @@ -647,43 +723,49 @@ ERR ("illegal backslash escape sequence in string"); } } - else if (ch >= 0x20 && ch <= 0x7f) + else if (expect_true (ch >= 0x20 && ch <= 0x7f)) *cur++ = ch; else if (ch >= 0x80) { + STRLEN clen; + UV uch; + --dec->cur; - STRLEN clen; - UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); + uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); if (clen == (STRLEN)-1) ERR ("malformed UTF-8 character in JSON string"); do - { - *cur++ = *dec->cur++; - } + *cur++ = *dec->cur++; while (--clen); utf8 = 1; } - else if (!ch) - ERR ("unexpected end of string while parsing json string"); else - ERR ("invalid character encountered"); + { + --dec->cur; + if (!ch) + ERR ("unexpected end of string while parsing JSON string"); + else + ERR ("invalid character encountered while parsing JSON string"); + } } while (cur < buf + SHORT_STRING_LEN); - STRLEN len = cur - buf; + { + STRLEN len = cur - buf; - if (sv) - { - SvGROW (sv, SvCUR (sv) + len + 1); - memcpy (SvPVX (sv) + SvCUR (sv), buf, len); - SvCUR_set (sv, SvCUR (sv) + len); - } - else - sv = newSVpvn (buf, len); + if (sv) + { + SvGROW (sv, SvCUR (sv) + len + 1); + memcpy (SvPVX (sv) + SvCUR (sv), buf, len); + SvCUR_set (sv, SvCUR (sv) + len); + } + else + sv = newSVpvn (buf, len); + } } while (*dec->cur != '"'); @@ -770,16 +852,36 @@ if (!is_nv) { - UV uv; - int numtype = grok_number (start, dec->cur - start, &uv); - if (numtype & IS_NUMBER_IN_UV) - if (numtype & IS_NUMBER_NEG) + // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so + if (*start == '-') + switch (dec->cur - start) { - if (uv < (UV)IV_MIN) - return newSViv (-(IV)uv); + case 2: return newSViv (-( start [1] - '0' )); + case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); + case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); + case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); } - else - return newSVuv (uv); + else + switch (dec->cur - start) + { + case 1: return newSViv ( start [0] - '0' ); + case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); + case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); + case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); + } + + { + UV uv; + int numtype = grok_number (start, dec->cur - start, &uv); + if (numtype & IS_NUMBER_IN_UV) + if (numtype & IS_NUMBER_NEG) + { + if (uv < (UV)IV_MIN) + return newSViv (-(IV)uv); + } + else + return newSVuv (uv); + } } return newSVnv (Atof (start)); @@ -937,7 +1039,7 @@ break; default: - ERR ("malformed json string, neither array, object, number, string or atom"); + ERR ("malformed JSON string, neither array, object, number, string or atom"); break; } @@ -946,10 +1048,15 @@ } static SV * -decode_json (SV *string, U32 flags) +decode_json (SV *string, U32 flags, UV *offset_return) { + dec_t dec; + UV offset; SV *sv; + SvGETMAGIC (string); + SvUPGRADE (string, SVt_PV); + if (flags & F_UTF8) sv_utf8_downgrade (string, 0); else @@ -957,7 +1064,6 @@ SvGROW (string, SvCUR (string) + 1); // should basically be a NOP - dec_t dec; dec.flags = flags; dec.cur = SvPVX (string); dec.end = SvEND (string); @@ -965,14 +1071,34 @@ dec.depth = 0; dec.maxdepth = DEC_DEPTH (dec.flags); - *SvEND (string) = 0; // this should basically be a nop, too + *dec.end = 0; // this should basically be a nop, too, but make sure it's there sv = decode_sv (&dec); + if (!(offset_return || !sv)) + { + // check for trailing garbage + decode_ws (&dec); + + if (*dec.cur) + { + dec.err = "garbage after JSON object"; + SvREFCNT_dec (sv); + sv = 0; + } + } + + if (offset_return || !sv) + { + offset = dec.flags & F_UTF8 + ? dec.cur - SvPVX (string) + : utf8_distance (dec.cur, SvPVX (string)); + + if (offset_return) + *offset_return = offset; + } + if (!sv) { - IV offset = dec.flags & F_UTF8 - ? dec.cur - SvPVX (string) - : utf8_distance (dec.cur, SvPVX (string)); SV *uni = sv_newmortal (); // horrible hack to silence warning inside pv_uni_display @@ -984,7 +1110,7 @@ pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); LEAVE; - croak ("%s, at character offset %d (%s)", + croak ("%s, at character offset %d [\"%s\"]", dec.err, (int)offset, dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); @@ -1030,6 +1156,7 @@ SV *ascii (SV *self, int enable = 1) ALIAS: ascii = F_ASCII + latin1 = F_LATIN1 utf8 = F_UTF8 indent = F_INDENT canonical = F_CANONICAL @@ -1051,7 +1178,7 @@ OUTPUT: RETVAL -SV *max_depth (SV *self, int max_depth = 0x80000000UL) +SV *max_depth (SV *self, UV max_depth = 0x80000000UL) CODE: { UV *uv = SvJSON (self); @@ -1075,7 +1202,16 @@ void decode (SV *self, SV *jsonstr) PPCODE: - XPUSHs (decode_json (jsonstr, *SvJSON (self))); + XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); + +void decode_prefix (SV *self, SV *jsonstr) + PPCODE: +{ + UV offset; + EXTEND (SP, 2); + PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); + PUSHs (sv_2mortal (newSVuv (offset))); +} PROTOTYPES: ENABLE @@ -1089,5 +1225,5 @@ ALIAS: jsonToObj = 0 PPCODE: - XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); + XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));