From: Nicholas Clark Date: Tue, 7 Feb 2006 12:19:17 +0000 (+0000) Subject: Fix bug 38454 (rindex corrects for $[ on bytes rather than UTF-8) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a2b7337b02f836aab62591af0d79675c5d157499;p=p5sagit%2Fp5-mst-13.2.git Fix bug 38454 (rindex corrects for $[ on bytes rather than UTF-8) p4raw-id: //depot/perl@27116 --- diff --git a/pp.c b/pp.c index e9f159b..ee32dba 100644 --- a/pp.c +++ b/pp.c @@ -3239,9 +3239,13 @@ PP(pp_rindex) if (MAXARG < 3) offset = blen; else { + /* arybase is in characters, like offset, so combine prior to the + UTF-8 to bytes calculation. */ + offset -= arybase; if (offset > 0 && big_utf8) sv_pos_u2b(big, &offset, 0); - offset = offset - arybase + llen; + /* llen is in bytes. */ + offset += llen; } if (offset < 0) offset = 0; diff --git a/t/op/index.t b/t/op/index.t index 100439d..88c0372 100755 --- a/t/op/index.t +++ b/t/op/index.t @@ -7,7 +7,7 @@ BEGIN { use strict; require './test.pl'; -plan( tests => 58 ); +plan( tests => 66 ); my $foo = 'Now is the time for all good men to come to the aid of their country.'; @@ -121,3 +121,15 @@ is(rindex($a, "foo", ), 0); is (index($text, $search_octets), -1); is (rindex($text, $search_octets), -1); } + +foreach my $utf8 ('', ', utf-8') { + foreach my $arraybase (0, 1, -1, -2) { + my $expect_pos = 2 + $arraybase; + + my $prog = "\$[ = $arraybase; \$big = \"N\\xabN\\xab\"; "; + $prog .= '$big .= chr 256; chop $big; ' if $utf8; + $prog .= 'print rindex $big, "N", 2 + $['; + + fresh_perl_is($prog, $expect_pos, {}, "\$[ = $arraybase$utf8"); + } +}