From: Mischa POSLAWSKY Date: Thu, 12 Nov 2009 17:54:19 +0000 (+0100) Subject: XXX: after context X-Git-Url: http://git.shiar.nl/perl/list-index.git/commitdiff_plain/ab9917512865cf7070b364a1b0f07f32a3b49e3f XXX: after context --- 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]; diff --git a/t/10-ranges.t b/t/10-ranges.t index 2348fca..66edf76 100644 --- a/t/10-ranges.t +++ b/t/10-ranges.t @@ -25,11 +25,11 @@ is_deeply($index->ranges({pagesize => @uniform / 2.1}), [map { [split /-/, $_, 2 { ok(my $index = List::Index->new(['aa'..'zz', 1..193]), 'non-alphabetic values (uniform)'); is_deeply($index->ranges, [map { [split /-/, $_, 2] } qw( - -. ..-. .. ...-. - a-bv bw-dr ds-fn fo-hk hl-jg jh-k l-m n-ov ow-qr qs-sn so-uk ul-wg wh-x y- + -. . . . + a-b c-dr ds-fn fo-hk hl-jg jh-k l-m n-o p-qr qs-sn so-uk ul-wg wh-x y- )], 'default ranges'); is_deeply($index->ranges({pagesize => 300}), [map { [split /-/, $_, 2] } qw( - -c d-ov ow- + -c d-o p- )], 'large pagesize'); } @@ -38,13 +38,13 @@ ok(my $index = List::Index->new([qw( baa1 baa2 baa3 baaa bbc cbc daaa ea eaaa zed )]), 'variable length values'); is_deeply($index->ranges({pagesize => 2, context => 0}), [ - map { [split /-/, $_, 2] } qw(-baa baa.-bbb bbc-daa. daaa-eaa. eaaa-) + map { [split /-/, $_, 2] } qw(-baa. baa.-bbb bbc-daa. daaa-eaa. eaaa-) ], 'no context'); is_deeply($index->ranges({pagesize => 2}), [ - map { [split /-/, $_, 2] } qw(-baa baa.-ba bb-c d-ea. eaa-) + map { [split /-/, $_, 2] } qw(-a b c d-ea. eaa-) ], 'default context'); # context should be 1 is_deeply($index->ranges({pagesize => 2, context => 2}), [ - map { [split /-/, $_, 2] } qw(-a b-ba bb-c d e-) + map { [split /-/, $_, 2] } qw(-a b-c d-c d e-) ], 'overlap'); # first item equals second due to large context is_deeply($index->ranges({pagesize => 2, length => 1}), [ map { [split /-/, $_, 2] } qw(-a b-a b-c d e-)