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 = @$self / $pages;
26 my $offset = $pagesize + .5;
30 while ($offset < @$self) {
31 my $link = substr $self->[$offset], 0, $length;
34 # take a value slightly before the current offset
35 if ((my $before = $offset - $context + $shrunk) > 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;
53 # take a value after the current offset
54 if ((my $after = $offset + $context - $enlarged) < $#$self) {
55 # see how much of it matches the current link
57 for my $match (split //, $self->[$after]) {
58 scalar $link =~ /\G\Q$match/g or last;
61 # use this link if it's shorter
62 if ($trim < length $link) {
63 $link = substr $self->[$after], 0, $trim;
64 # advance lookbehind offset on the next page
66 for ($offset .. $#$self) {
67 last if $self->[$offset + $shrunk] =~ /^\Q$link/;
79 use List::MoreUtils 'uniq';
81 for my $i (0 .. $#links - 1) {
82 my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
83 $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
84 next if $link eq $links[$i] and $i;
85 $links[$i] .= '-'.$link;
94 my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
95 or return qr/^\Q$link/i;
101 $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
106 for my $i (0 .. length($s1) - 1) {
107 my $lasti = $i == length($s1) - 1;
108 $char = substr $s1, $i, 1;
110 # do not include prefix character in final range
111 $next = chr( ord($char) + 1 ) unless $lasti;
114 next if $next gt $last;
115 if (length $s2 > $i) {
116 if ($s2 =~ /^\Q$prefix/) {
117 $last = substr $s2, $i, 1;
118 next if $char eq $last;
119 $last = chr( ord($last) - (length $s2 > 1) );
120 next if $next gt $last;
126 # push @allow, $prefix if $i and $lasti;
129 # if ($last eq 'z') {
130 # push @allow, $prefix if $i and $lasti;
136 push @allow, $prefix."[$next-$last]";
139 $prefix .= $char eq '.' ? '[^a-z]' : $char;
146 for my $i (0 .. length($s2) - 1) {
147 $char = substr $s2, $i, 1;
149 if (length $s1 > $i) {
150 my $c1 = substr $s1, $i, 1;
151 if ($s1 =~ /^\Q$prefix/) {
152 next if $c1 le $char;
157 next if $i < length($s2) - 1;
160 push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
164 $prefix .= $char eq '.' ? '[^a-z]' : $char;
168 if $s2 =~ /^\Q$prefix/ and $s1 le $s2
169 and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
172 my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
182 List::Index - Find and apply prefix ranges to paginate keywords
187 my $index = List::Index->new(\@values);
188 my @pages = $index->ranges({pagesize => 50});
189 say "<a href='?q=$_'>$_</a>" for @pages;
191 use List::Index 'rangematch';
192 my $limit = rangematch('b-bmq'); # ge 'b' && le 'bmq'
193 @request = grep { $limit } @values;
201 L<List::Maker|List::Maker> for complex ranges of numeric lists.
205 Mischa POSLAWSKY <perl@shiar.org>
209 Copyright. All rights reserved.