X-Git-Url: http://git.shiar.net/perl/list-index.git/blobdiff_plain/20b084d596ff3d71a76dbc55a9f458911b4b8415..ab9917512865cf7070b364a1b0f07f32a3b49e3f:/lib/List/Index.pm diff --git a/lib/List/Index.pm b/lib/List/Index.pm index 574613e..47ef0da 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -28,16 +28,33 @@ sub ranges { while ($offset < @$self) { my $link = substr $self->[$offset], 0, $length; if ($context) { - # take a value slightly before the current offset - my $before = $offset > $context ? $self->[$offset - $context] : '.'; - # see how much of it matches the current link - my $trim = 1; - for my $match (split //, $before) { - scalar $link =~ /\G\Q$match/g or last; - $trim++; + { + # take a value slightly before the current offset + my $before = $offset > $context ? $self->[$offset - $context] : '.'; + # see how much of it matches the current link + my $trim = 1; + for my $match (split //, $before) { + scalar $link =~ /\G\Q$match/g or last; + $trim++; + } + # truncate link upto where the earlier value starts to differ + substr($link, $trim) = '' unless $trim > length $link; + } + + if ($offset + $context < $#$self) { + # take a value after the current offset + my $after = $self->[$offset + $context]; + # see how much of it matches the current link + my $trim = 1; + for my $match (split //, $after) { + scalar $link =~ /\G\Q$match/g or last; + $trim++; + } + # use this link if it's shorter + if ($trim < length $link) { + $link = substr $after, 0, $trim; + } } - # truncate link upto where the earlier value starts to differ - substr($link, $trim) = '' unless $trim > length $link; } push @links, [$link];