10 our @EXPORT_OK = qw(rangematch);
13 my ($class, $values) = @_;
14 bless [sort map { s/[^a-z]/./g; $_ } @$values], $class;
19 my $options = shift || {};
20 my $pagesize = $options->{pagesize} || 50;
21 my $context = $options->{context } // 1 + ($pagesize >> 4);
22 my $length = $options->{length } || 4;
23 my $pages = $options->{pages } || 1 + int $#$self / $pagesize;
25 $pagesize = $pages >= $#$self ? 1 : @$self / $pages;
26 my $lookbehind = -$context;
27 my $lookahead = $context;
30 for (my $offset = $pagesize + .5; $offset < @$self; $offset += $pagesize) {
31 my $link = substr $self->[$offset], 0, $length;
34 # take a value slightly before the current offset
35 if ((my $before = $offset + $lookbehind) > 0) {
36 # see how much of it matches the current link
38 for my $match (split //, $self->[$before - 1]) {
39 scalar $link =~ /\G\Q$match/g or last;
42 # truncate link upto where the earlier value starts to differ
43 if ($trim < length $link) {
44 substr($link, $trim) = '';
45 for (reverse $before .. $offset) {
46 $self->[$offset - $penalty] =~ /^\Q$link/ or last;
52 $lookbehind = -$context;
54 # take a value after the current offset
55 if ((my $after = $offset + $lookahead) < $#$self) {
56 # see how much of it matches the current link
58 for my $match (split //, $self->[$after]) {
59 scalar $link =~ /\G\Q$match/g or last;
62 # use this link if it's shorter
63 if ($trim < length $link) {
64 $link = substr $self->[$after], 0, $trim;
65 # advance lookbehind offset on the next page
67 for ($offset .. $after) {
68 last if $self->[$_] =~ /^\Q$link/;
74 $lookahead = $context - $penalty;
77 push @links, $link unless $links[-1] eq $link;
80 for my $i (0 .. $#links - 1) {
81 my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
82 $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
83 next if $link eq $links[$i] and $i;
84 $links[$i] .= '-'.$link;
93 my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
94 or return qr/^\Q$link/i;
100 $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
105 for my $i (0 .. length($s1) - 1) {
106 my $lasti = $i == length($s1) - 1;
107 $char = substr $s1, $i, 1;
109 # do not include prefix character in final range
110 $next = chr( ord($char) + 1 ) unless $lasti;
113 next if $next gt $last;
114 if (length $s2 > $i) {
115 if ($s2 =~ /^\Q$prefix/) {
116 $last = substr $s2, $i, 1;
117 next if $char eq $last;
118 $last = chr( ord($last) - (length $s2 > 1) );
119 next if $next gt $last;
125 # push @allow, $prefix if $i and $lasti;
128 # if ($last eq 'z') {
129 # push @allow, $prefix if $i and $lasti;
135 push @allow, $prefix."[$next-$last]";
138 $prefix .= $char eq '.' ? '[^a-z]' : $char;
145 for my $i (0 .. length($s2) - 1) {
146 $char = substr $s2, $i, 1;
148 if (length $s1 > $i) {
149 my $c1 = substr $s1, $i, 1;
150 if ($s1 =~ /^\Q$prefix/) {
151 next if $c1 le $char;
156 next if $i < length($s2) - 1;
159 push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
163 $prefix .= $char eq '.' ? '[^a-z]' : $char;
167 if $s2 =~ /^\Q$prefix/ and $s1 le $s2
168 and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
171 my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
181 List::Index - Find and apply prefix ranges to paginate keywords
186 my $index = List::Index->new(\@values);
187 my @pages = $index->ranges({pagesize => 50});
188 say "<a href='?q=$_'>$_</a>" for @pages;
190 use List::Index 'rangematch';
191 my $limit = rangematch('b-bmq'); # ge 'b' && le 'bmq'
192 @request = grep { $limit } @values;
200 L<List::Maker|List::Maker> for complex ranges of numeric lists.
204 Mischa POSLAWSKY <perl@shiar.org>
208 Copyright. All rights reserved.