fix lookahead (reset \G, first match)
[perl/list-index.git] / lib / List / Index.pm
index 6309d2e0d3413926b6063098444e17f471f796f6..f58e43e572be60f55bf05f972ea70ea97d5d7883 100644 (file)
@@ -60,18 +60,21 @@ sub ranges {
                        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) {
-                                       $link = substr $rows[$after], 0, $trim;
-                                       # advance lookbehind offset on the next page
                                        $enlarged = 0;
                                        for ($offset + 1 .. $after) {
+                                               my $prefix = substr $rows[$_], 0, $trim;
+                                               # advance lookbehind offset on the next page
                                                $shrunk++;
-                                               last if $rows[$_] =~ /^\Q$link/;
+                                               next if $link =~ /^\Q$prefix/;
+                                               $link = $prefix;
+                                               last;
                                        }
                                }
                        }