X-Git-Url: http://git.shiar.net/perl/list-index.git/blobdiff_plain/cbd8812884e40f965709d50f5e5b0a7bc1fd97b3..e5fa8cd7b5cc84943c5f7c32c4f34974613a235b:/lib/List/Index.pm diff --git a/lib/List/Index.pm b/lib/List/Index.pm index 5b46313..339bd7d 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -24,13 +24,14 @@ sub ranges { $pagesize = @$self / $pages; my $offset = $pagesize + .5; + my $penalty = 0; my @links = (''); while ($offset < @$self) { my $link = substr $self->[$offset], 0, $length; if ($context) { - { + if ($offset > $context - 1 + $penalty) { # take a value slightly before the current offset - my $before = $offset > $context ? $self->[$offset - $context] : '.'; + my $before = $self->[$offset - $context - 1 + $penalty]; # see how much of it matches the current link my $trim = 1; for my $match (split //, $before) { @@ -41,6 +42,7 @@ sub ranges { substr($link, $trim) = '' unless $trim > length $link; } + $penalty = 0; if ($offset + $context < $#$self) { # take a value after the current offset my $after = $self->[$offset + $context]; @@ -53,6 +55,10 @@ sub ranges { # use this link if it's shorter if ($trim < length $link) { $link = substr $after, 0, $trim; + for ($offset .. $#$self) { + last if $self->[$offset + $penalty] =~ /^\Q$link/; + $penalty++; + } } } }