10 our @EXPORT_OK = qw(rangematch);
13 my ($class, $options) = @_;
15 bless $options, $class;
20 my @rows = sort map { s/[^a-z]/./g; $_ } @{ shift() };
21 my $options = shift || {};
22 $options->{$_} //= $self->{$_} for keys %$self;
24 my $pagesize = $options->{pagesize} || 50;
25 my $context = $options->{context } // 1 + ($pagesize >> 4);
26 my $length = $options->{length } || 4;
27 my $pages = $options->{pages } || 1 + int $#rows / $pagesize;
29 $pagesize = $pages >= $#rows ? 1 : @rows / $pages;
34 for (my $offset = $pagesize + .5; $offset < @rows; $offset += $pagesize) {
35 my $link = substr $rows[$offset], 0, $length;
37 my $lookbehind = -$context + $shrunk;
38 my $lookahead = $context - $enlarged;
39 $shrunk = $enlarged = 0;
41 # take a value slightly before the current offset
42 if ((my $before = $offset + $lookbehind) > 0) {
43 # see how much of it matches the current link
45 for my $match (split //, $rows[$before - 1]) {
46 scalar $link =~ /\G\Q$match/g or last;
49 # truncate link upto where the earlier value starts to differ
50 if ($trim < length $link) {
51 substr($link, $trim) = '';
52 for (reverse $before .. $offset - 1) {
53 $rows[$_] =~ /^\Q$link/ or last;
59 # take a value after the current offset
60 if ((my $after = $offset + $lookahead) < $#rows) {
61 # see how much of it matches the current link
63 for my $match (split //, $rows[$after]) {
64 scalar $link =~ /\G\Q$match/g or last;
67 # use this link if it's shorter
68 if ($trim < length $link) {
69 $link = substr $rows[$after], 0, $trim;
70 # advance lookbehind offset on the next page
72 for ($offset + 1 .. $after) {
74 last if $rows[$_] =~ /^\Q$link/;
80 push @links, $link unless $links[-1] eq $link;
83 # add range end to each link
84 for my $i (0 .. $#links - 1) {
85 # end at start of next value with the last character decremented
86 my $next = $links[$i + 1];
87 $next =~ s{(.)$}{ $1 le 'a' ? '.' : chr( ord($1) - 1 ) }e;
88 # amend range if it's ahead
89 $links[$i] .= '-'.$next unless $next eq $links[$i];
91 # final value takes the rest
99 my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
100 or return qr/^\Q$link/i;
106 $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
111 for my $i (0 .. length($s1) - 1) {
112 my $lasti = $i == length($s1) - 1;
113 $char = substr $s1, $i, 1;
115 # do not include prefix character in final range
116 $next = chr( ord($char) + 1 ) unless $lasti;
119 next if $next gt $last;
120 if (length $s2 > $i) {
121 if ($s2 =~ /^\Q$prefix/) {
122 $last = substr $s2, $i, 1;
123 next if $char eq $last;
124 $last = chr( ord($last) - (length $s2 > 1) );
125 next if $next gt $last;
131 # push @allow, $prefix if $i and $lasti;
134 # if ($last eq 'z') {
135 # push @allow, $prefix if $i and $lasti;
141 push @allow, $prefix."[$next-$last]";
144 $prefix .= $char eq '.' ? '[^a-z]' : $char;
151 for my $i (0 .. length($s2) - 1) {
152 $char = substr $s2, $i, 1;
154 if (length $s1 > $i) {
155 my $c1 = substr $s1, $i, 1;
156 if ($s1 =~ /^\Q$prefix/) {
157 next if $c1 le $char;
162 next if $i < length($s2) - 1;
165 push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
169 $prefix .= $char eq '.' ? '[^a-z]' : $char;
173 if $s2 =~ /^\Q$prefix/ and $s1 le $s2
174 and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
177 my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
187 List::Index - Find and apply prefix ranges to paginate keywords
192 my $index = List::Index->new({ pagesize => 50 });
193 my @pages = $index->ranges(\@values);
194 say "<a href='?q=$_'>$_</a>" for @pages;
196 use List::Index 'rangematch';
197 my $limit = rangematch('b-bmq'); # ge 'b' && le 'bmq'
198 @request = grep { $limit } @values;
206 L<List::Maker|List::Maker> for complex ranges of numeric lists.
210 Mischa POSLAWSKY <perl@shiar.org>
214 Copyright. All rights reserved.