From: Vincent Pit Date: Sat, 16 May 2009 12:27:47 +0000 (+0200) Subject: Fix shifting more bits than the length of the window X-Git-Tag: v0.06~11 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=c36fbc2566b9aeb0642e7a1b2fb8fc843faff3e9;p=perl%2Fmodules%2FScalar-Vec-Util.git Fix shifting more bits than the length of the window --- diff --git a/lib/Scalar/Vec/Util.pm b/lib/Scalar/Vec/Util.pm index 12d049c..b5a8080 100644 --- a/lib/Scalar/Vec/Util.pm +++ b/lib/Scalar/Vec/Util.pm @@ -132,14 +132,17 @@ sub vshift { $left = 0; $bits = -$bits; } - $bits = $length if $bits > $length; - $length -= $bits; - if ($left) { - vcopy($_[0], $start, $_[0], $start + $bits, $length); - vfill($_[0], $start, $bits, $insert) if defined $insert; + if ($bits < $length) { + $length -= $bits; + if ($left) { + vcopy($_[0], $start, $_[0], $start + $bits, $length); + vfill($_[0], $start, $bits, $insert) if defined $insert; + } else { + vcopy($_[0], $start + $bits, $_[0], $start, $length); + vfill($_[0], $start + $length, $bits, $insert) if defined $insert; + } } else { - vcopy($_[0], $start + $bits, $_[0], $start, $length); - vfill($_[0], $start + $length, $bits, $insert) if defined $insert; + vfill($_[0], $start, $length, $insert) if defined $insert; } } diff --git a/t/40-vshift.t b/t/40-vshift.t index 39e14d5..5fe1b69 100644 --- a/t/40-vshift.t +++ b/t/40-vshift.t @@ -27,6 +27,22 @@ sub pat { } } +sub expect { + (undef, my $s, my $l, my $b, my $left, my $insert) = @_; + myfill($_[0], 0, $s, 0); + if ($b < $l) { + if ($left) { + myfill($_[0], $s, $b, defined $insert ? $insert : 1); + myfill($_[0], $s + $b, $l - $b, 1); + } else { + myfill($_[0], $s, $l - $b, 1); + myfill($_[0], $s + $l - $b, $b, defined $insert ? $insert : 1); + } + } else { + myfill($_[0], $s, $l, defined $insert ? $insert : 1); + } +} + my ($v, $v0, $c) = ('', '') x 2; sub try { @@ -35,22 +51,16 @@ sub try { for my $s (@s) { for my $l (0 .. $n - 1) { last if $s + $l > $n; + rst $v0; pat $v0, $s, $l, 0; my @b = (0); my $l2 = int($l/2); push @b, $l2 if $l2 != $l; - push @b, $l if $l; + push @b, $l + 1; for my $b (@b) { $v = $v0; - $c = ''; - myfill($c, 0, $s, 0); - if ($left) { - myfill($c, $s, $b, defined $insert ? $insert : 1); - myfill($c, $s + $b, $l - $b, 1); - } else { - myfill($c, $s, $l - $b, 1); - myfill($c, $s + $l - $b, $b, defined $insert ? $insert : 1); - } + rst $c; + expect $c, $s, $l, $b, $left, $insert; $b = -$b unless $left; vshift $v, $s, $l => $b, $insert; my $i = defined $insert ? $insert : 'undef';