+
+ $lookbehind = -$context;
+
+ # take a value after the current offset
+ if ((my $after = $offset + $lookahead) < $#$self) {
+ # see how much of it matches the current link
+ my $trim = 1;
+ for my $match (split //, $self->[$after]) {
+ scalar $link =~ /\G\Q$match/g or last;
+ $trim++;
+ }
+ # use this link if it's shorter
+ if ($trim < length $link) {
+ $link = substr $self->[$after], 0, $trim;
+ # advance lookbehind offset on the next page
+ $penalty = 0;
+ for ($offset .. $after) {
+ last if $self->[$_] =~ /^\Q$link/;
+ $lookbehind++;
+ }
+ }
+ }
+
+ $lookahead = $context - $penalty;