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;
28 while ($offset < @$self) {
29 my $link = substr $self->[$offset], 0, $length;
31 if ($offset > $context - 1) {
32 # take a value slightly before the current offset
33 my $before = $self->[$offset - $context - 1];
34 # see how much of it matches the current link
36 for my $match (split //, $before) {
37 scalar $link =~ /\G\Q$match/g or last;
40 # truncate link upto where the earlier value starts to differ
41 substr($link, $trim) = '' unless $trim > length $link;
44 if ($offset + $context < $#$self) {
45 # take a value after the current offset
46 my $after = $self->[$offset + $context];
47 # see how much of it matches the current link
49 for my $match (split //, $after) {
50 scalar $link =~ /\G\Q$match/g or last;
53 # use this link if it's shorter
54 if ($trim < length $link) {
55 $link = substr $after, 0, $trim;
64 use List::MoreUtils 'uniq';
66 for my $i (0 .. $#links - 1) {
67 my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
68 $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
69 next if $link eq $links[$i] and $i;
70 $links[$i] .= '-'.$link;
79 my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
80 or return qr/^\Q$link/i;
86 $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
91 for my $i (0 .. length($s1) - 1) {
92 my $lasti = $i == length($s1) - 1;
93 $char = substr $s1, $i, 1;
95 # do not include prefix character in final range
96 $next = chr( ord($char) + 1 ) unless $lasti;
99 next if $next gt $last;
100 if (length $s2 > $i) {
101 if ($s2 =~ /^\Q$prefix/) {
102 $last = substr $s2, $i, 1;
103 next if $char eq $last;
104 $last = chr( ord($last) - (length $s2 > 1) );
105 next if $next gt $last;
111 # push @allow, $prefix if $i and $lasti;
114 # if ($last eq 'z') {
115 # push @allow, $prefix if $i and $lasti;
121 push @allow, $prefix."[$next-$last]";
124 $prefix .= $char eq '.' ? '[^a-z]' : $char;
131 for my $i (0 .. length($s2) - 1) {
132 $char = substr $s2, $i, 1;
134 if (length $s1 > $i) {
135 my $c1 = substr $s1, $i, 1;
136 if ($s1 =~ /^\Q$prefix/) {
137 next if $c1 le $char;
142 next if $i < length($s2) - 1;
145 push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
149 $prefix .= $char eq '.' ? '[^a-z]' : $char;
153 if $s2 =~ /^\Q$prefix/ and $s1 le $s2
154 and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
157 my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
167 List::Index - Find and apply prefix ranges to paginate keywords
172 my $index = List::Index->new(\@values);
173 my @pages = $index->ranges({pagesize => 50});
174 say "<a href='?q=$_'>$_</a>" for @pages;
176 use List::Index 'rangematch';
177 my $limit = rangematch('b-bmq'); # ge 'b' && le 'bmq'
178 @request = grep { $limit } @values;
186 L<List::Maker|List::Maker> for complex ranges of numeric lists.
190 Mischa POSLAWSKY <perl@shiar.org>
194 Copyright. All rights reserved.