f58e43e572be60f55bf05f972ea70ea97d5d7883
[perl/list-index.git] / lib / List / Index.pm
1 package List::Index;
2
3 use 5.010;
4 use strict;
5 use warnings;
6
7 use Exporter 'import';
8
9 our $VERSION = '1.02';
10 our @EXPORT_OK = qw(rangematch);
11
12 sub new {
13         my ($class, $options) = @_;
14         $options ||= {};
15         bless $options, $class;
16 }
17
18 sub ranges {
19         my $self = shift;
20         my @rows = sort map { s/[^a-z]/./g; $_ } @{ shift() };
21         my $options = shift || {};
22         $options->{$_} //= $self->{$_} for keys %$self;
23
24         my $pagesize = $options->{pagesize} || 50;
25         my $context  = $options->{context } // 1 + ($pagesize >> 4);
26         my $length   = $options->{length  } || 4;
27         my $pages    = $options->{pages   } || 1 + int $#rows / $pagesize;
28
29         $pagesize = $pages >= $#rows ? 1 : @rows / $pages;
30         my $shrunk = 0;
31         my $enlarged = 0;
32
33         my @links = ('');
34         for (my $offset = $pagesize + .5; $offset < @rows; $offset += $pagesize) {
35                 my $link = substr $rows[$offset], 0, $length;
36                 if ($context) {
37                         my $lookbehind = -$context + $shrunk;
38                         my $lookahead  =  $context - $enlarged;
39                         $shrunk = $enlarged = 0;
40
41                         # take a value slightly before the current offset
42                         if ((my $before = $offset + $lookbehind) > 0) {
43                                 # see how much of it matches the current link
44                                 my $trim = 1;
45                                 for my $match (split //, $rows[$before - 1]) {
46                                         scalar $link =~ /\G\Q$match/g or last;
47                                         $trim++;
48                                 }
49                                 # truncate link upto where the earlier value starts to differ
50                                 if ($trim < length $link) {
51                                         substr($link, $trim) = '';
52                                         for (reverse $before .. $offset - 1) {
53                                                 $rows[$_] =~ /^\Q$link/ or last;
54                                                 $enlarged++;
55                                         }
56                                 }
57                         }
58
59                         # take a value after the current offset
60                         if ((my $after = $offset + $lookahead) < $#rows) {
61                                 # see how much of it matches the current link
62                                 my $trim = 1;
63                                 pos $link = 0;
64                                 for my $match (split //, $rows[$after]) {
65                                         scalar $link =~ /\G\Q$match/g or last;
66                                         $trim++;
67                                 }
68                                 # use this link if it's shorter
69                                 if ($trim < length $link) {
70                                         $enlarged = 0;
71                                         for ($offset + 1 .. $after) {
72                                                 my $prefix = substr $rows[$_], 0, $trim;
73                                                 # advance lookbehind offset on the next page
74                                                 $shrunk++;
75                                                 next if $link =~ /^\Q$prefix/;
76                                                 $link = $prefix;
77                                                 last;
78                                         }
79                                 }
80                         }
81                 }
82
83                 push @links, $link unless $links[-1] eq $link;
84         }
85
86         # add range end to each link
87         for my $i (0 .. $#links - 1) {
88                 # end at start of next value with the last character decremented
89                 my $next = $links[$i + 1];
90                 $next =~ s{(.)$}{ $1 le 'a' ? '.' : chr( ord($1) - 1 ) }e;
91                 # amend range if it's ahead
92                 $links[$i] .= '-'.$next unless $next eq $links[$i];
93         }
94         # final value takes the rest
95         $links[-1] .= '-';
96
97         return \@links;
98 }
99
100 sub rangematch {
101         my ($link) = @_;
102         my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
103                 or return qr/^\Q$link/i;
104         $s1 =~ s/\.$//;
105         my @allow;
106
107         if (length $s1) {
108                 if (length $s2) {
109                         $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
110                 }
111
112                 my $prefix = '';
113                 my $char;
114                 for my $i (0 .. length($s1) - 1) {
115                         my $lasti = $i == length($s1) - 1;
116                         $char = substr $s1, $i, 1;
117                         my $next = $char;
118                         # do not include prefix character in final range
119                         $next = chr( ord($char) + 1 ) unless $lasti;
120
121                         my $last = 'z';
122                         next if $next gt $last;
123                         if (length $s2 > $i) {
124                                 if ($s2 =~ /^\Q$prefix/) {
125                                         $last = substr $s2, $i, 1;
126                                         next if $char eq $last;
127                                         $last = chr( ord($last) - (length $s2 > 1) );
128                                         next if $next gt $last;
129                                 }
130                         }
131
132                         if ($char eq '.') {
133                                 if ($last eq 'z') {
134 #                                       push @allow, $prefix if $i and $lasti;
135 #                                       next;
136                                 }
137 #                               if ($last eq 'z') {
138 #                                       push @allow, $prefix if $i and $lasti;
139 #                                       next;
140 #                               }
141                                 $next = 'a';
142                         }
143
144                         push @allow, $prefix."[$next-$last]";
145                 }
146                 continue {
147                         $prefix .= $char eq '.' ? '[^a-z]' : $char;
148                 }
149         }
150
151         if (length $s2) {
152                 my $prefix = '';
153                 my $char;
154                 for my $i (0 .. length($s2) - 1) {
155                         $char = substr $s2, $i, 1;
156                         my $last = 'z';
157                         if (length $s1 > $i) {
158                                 my $c1 = substr $s1, $i, 1;
159                                 if ($s1 =~ /^\Q$prefix/) {
160                                         next if $c1 le $char;
161                                 }
162                         }
163
164                         if ($char eq '.') {
165                                 next if $i < length($s2) - 1;
166                         }
167
168                         push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
169                                 if $i or $s1 eq '';
170                 }
171                 continue {
172                         $prefix .= $char eq '.' ? '[^a-z]' : $char;
173                 }
174
175                 push @allow, $prefix
176                         if $s2 =~ /^\Q$prefix/ and $s1 le $s2
177                         and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
178         }
179
180         my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
181         return qr/^$match/i;
182 }
183
184 1;
185
186 __END__
187
188 =head1 NAME
189
190 List::Index - Find and apply prefix ranges to paginate keywords
191
192 =head1 SYNOPSIS
193
194         use List::Index;
195         my $index = List::Index->new({ pagesize => 50 });
196         my @pages = $index->ranges(\@values);
197         say "<a href='?q=$_'>$_</a>" for @pages;
198
199         use List::Index 'rangematch';
200         my $limit = rangematch('b-bmq');  # ge 'b' && le 'bmq'
201         @request = grep { $limit } @values;
202
203 =head1 DESCRIPTION
204
205 TODO
206
207 =head1 SEE ALSO
208
209 L<List::Maker|List::Maker> for complex ranges of numeric lists.
210
211 =head1 AUTHOR
212
213 Mischa POSLAWSKY <perl@shiar.org>
214
215 =head1 LICENSE
216
217 Copyright. All rights reserved.
218