XXX: after context
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 12 Nov 2009 17:54:19 +0000 (18:54 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Thu, 12 Nov 2009 17:54:19 +0000 (18:54 +0100)
lib/List/Index.pm
t/10-ranges.t

index 574613e8b1c38e2e6b3eaf4c751fd42d53c6930e..47ef0da75030c8cda582a1b1bf93d4b9a0db493d 100644 (file)
@@ -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];
index 2348fcae55182821df7c986a23ec8e399373ebb9..66edf7607ccc1b0922c376fb8d6b2fe8fb9ae817 100644 (file)
@@ -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-)