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];
{
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');
}
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-)