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 # $offset += $context + 1;
59 $self->[$offset + ++$penalty] =~ /^\Q$link/
60 while $offset + $penalty < $#$self;
69 use List::MoreUtils 'uniq';
71 for my $i (0 .. $#links - 1) {
72 my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
73 $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
74 next if $link eq $links[$i] and $i;
75 $links[$i] .= '-'.$link;
84 my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
85 or return qr/^\Q$link/i;
91 $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
96 for my $i (0 .. length($s1) - 1) {
97 my $lasti = $i == length($s1) - 1;
98 $char = substr $s1, $i, 1;
100 # do not include prefix character in final range
101 $next = chr( ord($char) + 1 ) unless $lasti;
104 next if $next gt $last;
105 if (length $s2 > $i) {
106 if ($s2 =~ /^\Q$prefix/) {
107 $last = substr $s2, $i, 1;
108 next if $char eq $last;
109 $last = chr( ord($last) - (length $s2 > 1) );
110 next if $next gt $last;
116 # push @allow, $prefix if $i and $lasti;
119 # if ($last eq 'z') {
120 # push @allow, $prefix if $i and $lasti;
126 push @allow, $prefix."[$next-$last]";
129 $prefix .= $char eq '.' ? '[^a-z]' : $char;
136 for my $i (0 .. length($s2) - 1) {
137 $char = substr $s2, $i, 1;
139 if (length $s1 > $i) {
140 my $c1 = substr $s1, $i, 1;
141 if ($s1 =~ /^\Q$prefix/) {
142 next if $c1 le $char;
147 next if $i < length($s2) - 1;
150 push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
154 $prefix .= $char eq '.' ? '[^a-z]' : $char;
158 if $s2 =~ /^\Q$prefix/ and $s1 le $s2
159 and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
162 my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
172 List::Index - Find and apply prefix ranges to paginate keywords
177 my $index = List::Index->new(\@values);
178 my @pages = $index->ranges({pagesize => 50});
179 say "<a href='?q=$_'>$_</a>" for @pages;
181 use List::Index 'rangematch';
182 my $limit = rangematch('b-bmq'); # ge 'b' && le 'bmq'
183 @request = grep { $limit } @values;
191 L<List::Maker|List::Maker> for complex ranges of numeric lists.
195 Mischa POSLAWSKY <perl@shiar.org>
199 Copyright. All rights reserved.