- 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++;
+ if ((my $before = $offset + $lookbehind) > 0) {
+ # see how much of it matches the current link
+ my $trim = 1;
+ for my $match (split //, $rows[$before - 1]) {
+ scalar $link =~ /\G\Q$match/g or last;
+ $trim++;
+ }
+ # truncate link upto where the earlier value starts to differ
+ if ($trim < length $link) {
+ substr($link, $trim) = '';
+ for (reverse $before .. $offset - 1) {
+ $rows[$_] =~ /^\Q$link/ or last;
+ $enlarged++;
+ }
+ }
+ }
+
+ # take a value after the current offset
+ if ((my $after = $offset + $lookahead) < $#rows) {
+ # see how much of it matches the current link
+ my $trim = 1;
+ pos $link = 0;
+ for my $match (split //, $rows[$after]) {
+ scalar $link =~ /\G\Q$match/g or last;
+ $trim++;
+ }
+ # use this link if it's shorter
+ if ($trim < length $link) {
+ $enlarged = 0;
+ for ($offset + 1 .. $after) {
+ my $prefix = substr $rows[$_], 0, $trim;
+ # advance lookbehind offset on the next page
+ $shrunk++;
+ next if $link =~ /^\Q$prefix/;
+ $link = $prefix;
+ last;
+ }
+ }