cleanup context variables
[perl/list-index.git] / lib / List / Index.pm
index daeb707f9835c58e8160edf05440f59acd600690..6309d2e0d3413926b6063098444e17f471f796f6 100644 (file)
@@ -27,14 +27,17 @@ sub ranges {
        my $pages    = $options->{pages   } || 1 + int $#rows / $pagesize;
 
        $pagesize = $pages >= $#rows ? 1 : @rows / $pages;
-       my $lookbehind = -$context;
-       my $lookahead  =  $context;
+       my $shrunk = 0;
+       my $enlarged = 0;
 
        my @links = ('');
        for (my $offset = $pagesize + .5; $offset < @rows; $offset += $pagesize) {
                my $link = substr $rows[$offset], 0, $length;
                if ($context) {
-                       my $penalty = 0;
+                       my $lookbehind = -$context + $shrunk;
+                       my $lookahead  =  $context - $enlarged;
+                       $shrunk = $enlarged = 0;
+
                        # take a value slightly before the current offset
                        if ((my $before = $offset + $lookbehind) > 0) {
                                # see how much of it matches the current link
@@ -46,15 +49,13 @@ sub ranges {
                                # truncate link upto where the earlier value starts to differ
                                if ($trim < length $link) {
                                        substr($link, $trim) = '';
-                                       for (reverse $before .. $offset) {
-                                               $rows[$offset - $penalty] =~ /^\Q$link/ or last;
-                                               $penalty++;
+                                       for (reverse $before .. $offset - 1) {
+                                               $rows[$_] =~ /^\Q$link/ or last;
+                                               $enlarged++;
                                        }
                                }
                        }
 
-                       $lookbehind = -$context;
-
                        # take a value after the current offset
                        if ((my $after = $offset + $lookahead) < $#rows) {
                                # see how much of it matches the current link
@@ -67,15 +68,13 @@ sub ranges {
                                if ($trim < length $link) {
                                        $link = substr $rows[$after], 0, $trim;
                                        # advance lookbehind offset on the next page
-                                       $penalty = 0;
-                                       for ($offset .. $after) {
+                                       $enlarged = 0;
+                                       for ($offset + 1 .. $after) {
+                                               $shrunk++;
                                                last if $rows[$_] =~ /^\Q$link/;
-                                               $lookbehind++;
                                        }
                                }
                        }
-
-                       $lookahead = $context - $penalty;
                }
 
                push @links, $link unless $links[-1] eq $link;