--- JSON-XS/XS.xs 2007/03/23 15:10:55 1.6 +++ JSON-XS/XS.xs 2007/03/23 15:57:18 1.7 @@ -56,6 +56,15 @@ return &SvUVX (SvRV (sv)); } +static void +shrink (SV *sv) +{ + sv_utf8_downgrade (sv, 1); +#ifdef SvPV_shrink_to_cur + SvPV_shrink_to_cur (sv); +#endif +} + ///////////////////////////////////////////////////////////////////////////// static void @@ -169,7 +178,7 @@ } else { - need (enc, 10); // never more than 11 bytes needed + need (enc, len += 10); // never more than 11 bytes needed enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); ++str; } @@ -427,10 +436,9 @@ SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); -#ifdef SvPV_shrink_to_cur if (enc.flags & F_SHRINK) - SvPV_shrink_to_cur (enc.sv); -#endif + shrink (enc.sv); + return enc.sv; } @@ -578,7 +586,7 @@ STRLEN clen; UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); if (clen == (STRLEN)-1) - ERR ("malformed UTF-8 character in string, cannot convert to JSON"); + ERR ("malformed UTF-8 character in JSON string"); APPEND_GROW (clen); do @@ -605,10 +613,8 @@ if (utf8) SvUTF8_on (sv); -#ifdef SvPV_shrink_to_cur if (dec->flags & F_SHRINK) - SvPV_shrink_to_cur (sv); -#endif + shrink (sv); return sv; @@ -840,7 +846,7 @@ break; default: - ERR ("malformed json string"); + ERR ("malformed json string, neither array, object, number, string or atom"); break; } @@ -870,7 +876,9 @@ if (!sv) { - IV offset = utf8_distance (dec.cur, SvPVX (string)); + 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 COP cop;