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