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;
29 while ($offset < @$self) {
30 my $link = substr $self->[$offset], 0, $length;
32 if ($offset > $context - 1 + $penalty) {
33 # take a value slightly before the current offset
34 my $before = $self->[$offset - $context - 1 + $penalty];
35 # see how much of it matches the current link
37 for my $match (split //, $before) {
38 scalar $link =~ /\G\Q$match/g or last;
41 # truncate link upto where the earlier value starts to differ
42 substr($link, $trim) = '' unless $trim > length $link;
46 if ($offset + $context < $#$self) {
47 # take a value after the current offset
48 my $after = $self->[$offset + $context];
49 # see how much of it matches the current link
51 for my $match (split //, $after) {
52 scalar $link =~ /\G\Q$match/g or last;
55 # use this link if it's shorter
56 if ($trim < length $link) {
57 $link = substr $after, 0, $trim;
58 for ($offset .. $#$self) {
59 last if $self->[$offset + $penalty] =~ /^\Q$link/;
70 use List::MoreUtils 'uniq';
72 for my $i (0 .. $#links - 1) {
73 my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
74 $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
75 next if $link eq $links[$i] and $i;
76 $links[$i] .= '-'.$link;
85 my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
86 or return qr/^\Q$link/i;
92 $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
97 for my $i (0 .. length($s1) - 1) {
98 my $lasti = $i == length($s1) - 1;
99 $char = substr $s1, $i, 1;
101 # do not include prefix character in final range
102 $next = chr( ord($char) + 1 ) unless $lasti;
105 next if $next gt $last;
106 if (length $s2 > $i) {
107 if ($s2 =~ /^\Q$prefix/) {
108 $last = substr $s2, $i, 1;
109 next if $char eq $last;
110 $last = chr( ord($last) - (length $s2 > 1) );
111 next if $next gt $last;
117 # push @allow, $prefix if $i and $lasti;
120 # if ($last eq 'z') {
121 # push @allow, $prefix if $i and $lasti;
127 push @allow, $prefix."[$next-$last]";
130 $prefix .= $char eq '.' ? '[^a-z]' : $char;
137 for my $i (0 .. length($s2) - 1) {
138 $char = substr $s2, $i, 1;
140 if (length $s1 > $i) {
141 my $c1 = substr $s1, $i, 1;
142 if ($s1 =~ /^\Q$prefix/) {
143 next if $c1 le $char;
148 next if $i < length($s2) - 1;
151 push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
155 $prefix .= $char eq '.' ? '[^a-z]' : $char;
159 if $s2 =~ /^\Q$prefix/ and $s1 le $s2
160 and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
163 my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
173 List::Index - Find and apply prefix ranges to paginate keywords
178 my $index = List::Index->new(\@values);
179 my @pages = $index->ranges({pagesize => 50});
180 say "<a href='?q=$_'>$_</a>" for @pages;
182 use List::Index 'rangematch';
183 my $limit = rangematch('b-bmq'); # ge 'b' && le 'bmq'
184 @request = grep { $limit } @values;
192 L<List::Maker|List::Maker> for complex ranges of numeric lists.
196 Mischa POSLAWSKY <perl@shiar.org>
200 Copyright. All rights reserved.