From: Jarkko Hietaniemi Date: Mon, 19 Mar 2001 23:17:17 +0000 (+0000) Subject: substr($bytestr, i, n, $charstr) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f7928d6c98a55cfb1aa37088308b1e8ca18c526b;p=p5sagit%2Fp5-mst-13.2.git substr($bytestr, i, n, $charstr) TODO: we are still broken if $bytestr needs UTF-8 upgrading. p4raw-id: //depot/perl@9255 --- diff --git a/Todo-5.6 b/Todo-5.6 index 67b0429..f0c8e67 100644 --- a/Todo-5.6 +++ b/Todo-5.6 @@ -1,6 +1,5 @@ Unicode support finish byte <-> utf8 and localencoding <-> utf8 conversions - make substr($bytestr,0,0,$charstr) do the right conversion add Unicode::Map equivivalent to core add support for I/O disciplines - a way to specify disciplines when opening things: diff --git a/pp.c b/pp.c index 37ada8b..4e4c557 100644 --- a/pp.c +++ b/pp.c @@ -2701,6 +2701,7 @@ PP(pp_substr) char *repl = 0; STRLEN repl_len; int num_args = PL_op->op_private & 7; + bool utfrepllen = FALSE; SvTAINTED_off(TARG); /* decontaminate */ SvUTF8_off(TARG); /* decontaminate */ @@ -2708,6 +2709,7 @@ PP(pp_substr) if (num_args > 3) { sv = POPs; repl = SvPV(sv, repl_len); + utfrepllen = DO_UTF8(sv) && SvCUR(sv); } len = POPi; } @@ -2774,8 +2776,11 @@ PP(pp_substr) sv_setpvn(TARG, tmps, rem); if (utfcurlen) SvUTF8_on(TARG); - if (repl) + if (repl) { sv_insert(sv, pos, rem, repl, repl_len); + if (utfrepllen) + SvUTF8_on(sv); + } else if (lvalue) { /* it's an lvalue! */ if (!SvGMAGICAL(sv)) { if (SvROK(sv)) { diff --git a/t/op/substr.t b/t/op/substr.t index 12bcd00..7ac4194 100755 --- a/t/op/substr.t +++ b/t/op/substr.t @@ -1,6 +1,6 @@ #!./perl -print "1..162\n"; +print "1..168\n"; #P = start of string Q = start of substr R = end of substr S = end of string @@ -548,3 +548,22 @@ ok 162, length($x) == 5 && substr($x, 2, 1) eq "\x{100}" && substr($x, 3, 1) eq "\x{FF}" && substr($x, 4, 1) eq "\x{F3}"; + +substr($x = "ab", 0, 0, "\x{100}\x{200}"); +ok 163, $x eq "\x{100}\x{200}ab"; + +substr($x = "\x{100}\x{200}", 0, 0, "ab"); +ok 164, $x eq "ab\x{100}\x{200}"; + +substr($x = "ab", 1, 0, "\x{100}\x{200}"); +ok 165, $x eq "a\x{100}\x{200}b"; + +substr($x = "\x{100}\x{200}", 1, 0, "ab"); +ok 166, $x eq "\x{100}ab\x{200}"; + +substr($x = "ab", 2, 0, "\x{100}\x{200}"); +ok 167, $x eq "ab\x{100}\x{200}"; + +substr($x = "\x{100}\x{200}", 2, 0, "ab"); +ok 168, $x eq "\x{100}\x{200}ab"; +