diff options
author | Nicholas Clark <nick@ccl4.org> | 2009-10-20 09:48:38 +0100 |
---|---|---|
committer | Nicholas Clark <nick@ccl4.org> | 2009-10-20 09:48:38 +0100 |
commit | 81a923f452ade5c263d536ab9509f4be1fcd4e75 (patch) | |
tree | f055f2b8f7eec60789b8c001620c915e2bdeb5fa /toke.c | |
parent | 3e2f783e67807d5cec489704325d99148992cf37 (diff) | |
download | perl-81a923f452ade5c263d536ab9509f4be1fcd4e75.tar.gz |
Pull out filter setup code from S_swallow_bom() into S_add_utf16_textfilter()
Diffstat (limited to 'toke.c')
-rw-r--r-- | toke.c | 56 |
1 files changed, 25 insertions, 31 deletions
@@ -12703,21 +12703,8 @@ S_swallow_bom(pTHX_ U8 *s) #ifndef PERL_NO_UTF16_FILTER if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF16-LE script encoding (BOM)\n"); s += 2; - utf16le: if (PL_bufend > (char*)s) { - U8 *news; - I32 newlen; - - IoLINES(filter_add(S_utf16_textfilter, NULL)) = 1; - Newx(news, (PL_bufend - (char*)s) * 3 / 2 + 1, U8); - utf16_to_utf8_reversed(s, news, - PL_bufend - (char*)s - 1, - &newlen); - sv_setpvn(PL_linestr, (const char*)news, newlen); - Safefree(news); - SvUTF8_on(PL_linestr); - s = (U8*)SvPVX(PL_linestr); - PL_bufend = SvPVX(PL_linestr) + newlen; + s = add_utf16_textfilter(s, TRUE); } #else Perl_croak(aTHX_ "Unsupported script encoding UTF16-LE"); @@ -12729,21 +12716,8 @@ S_swallow_bom(pTHX_ U8 *s) #ifndef PERL_NO_UTF16_FILTER if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (BOM)\n"); s += 2; - utf16be: if (PL_bufend > (char *)s) { - U8 *news; - I32 newlen; - - filter_add(S_utf16_textfilter, NULL); - Newx(news, (PL_bufend - (char*)s) * 3 / 2 + 1, U8); - utf16_to_utf8(s, news, - PL_bufend - (char*)s, - &newlen); - sv_setpvn(PL_linestr, (const char*)news, newlen); - Safefree(news); - SvUTF8_on(PL_linestr); - s = (U8*)SvPVX(PL_linestr); - PL_bufend = SvPVX(PL_linestr) + newlen; + s = add_utf16_textfilter(s, FALSE); } #else Perl_croak(aTHX_ "Unsupported script encoding UTF16-BE"); @@ -12769,7 +12743,7 @@ S_swallow_bom(pTHX_ U8 *s) * 00 xx 00 xx * are a good indicator of UTF-16BE. */ if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16BE script encoding (no BOM)\n"); - goto utf16be; + s = add_utf16_textfilter(s, FALSE); } } #ifdef EBCDIC @@ -12787,7 +12761,7 @@ S_swallow_bom(pTHX_ U8 *s) * xx 00 xx 00 * are a good indicator of UTF-16LE. */ if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-16LE script encoding (no BOM)\n"); - goto utf16le; + s = add_utf16_textfilter(s, TRUE); } } return (char*)s; @@ -12801,7 +12775,7 @@ S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen) dVAR; const STRLEN old = SvCUR(sv); const I32 count = FILTER_READ(idx+1, sv, maxlen); - const int reverse = IoLINES(sv); + const bool reverse = IoLINES(sv); DEBUG_P(PerlIO_printf(Perl_debug_log, "utf16%s_textfilter(%p): %d %d (%d)\n", reverse ? "rev" : "", @@ -12828,6 +12802,26 @@ S_utf16_textfilter(pTHX_ int idx, SV *sv, int maxlen) DEBUG_P({sv_dump(sv);}); return SvCUR(sv); } + +static U8 * +S_add_utf16_textfilter(pTHX_ U8 *const s, bool reversed) +{ + U8 *news; + I32 newlen; + + IoLINES(filter_add(S_utf16_textfilter, NULL)) = reversed; + Newx(news, (PL_bufend - (char*)s) * 3 / 2 + 1, U8); + if (reversed) { + utf16_to_utf8_reversed(s, news, PL_bufend - (char*)s - 1, &newlen); + } else { + utf16_to_utf8(s, news, PL_bufend - (char*)s, &newlen); + } + sv_setpvn(PL_linestr, (const char*)news, newlen); + Safefree(news); + SvUTF8_on(PL_linestr); + PL_bufend = SvPVX(PL_linestr) + newlen; + return (U8*)SvPVX(PL_linestr); +} #endif /* |