limit $offset scope
[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 $lookbehind = -$context;
27         my $lookahead  =  $context;
28
29         my @links = ('');
30         for (my $offset = $pagesize + .5; $offset < @$self; $offset += $pagesize) {
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         }
79
80         use List::MoreUtils 'uniq';
81         @links = uniq @links;
82         for my $i (0 .. $#links - 1) {
83                 my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
84                 $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
85                 next if $link eq $links[$i] and $i;
86                 $links[$i] .= '-'.$link;
87         }
88         $links[-1] .= '-';
89
90         return \@links;
91 }
92
93 sub rangematch {
94         my ($link) = @_;
95         my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
96                 or return qr/^\Q$link/i;
97         $s1 =~ s/\.$//;
98         my @allow;
99
100         if (length $s1) {
101                 if (length $s2) {
102                         $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
103                 }
104
105                 my $prefix = '';
106                 my $char;
107                 for my $i (0 .. length($s1) - 1) {
108                         my $lasti = $i == length($s1) - 1;
109                         $char = substr $s1, $i, 1;
110                         my $next = $char;
111                         # do not include prefix character in final range
112                         $next = chr( ord($char) + 1 ) unless $lasti;
113
114                         my $last = 'z';
115                         next if $next gt $last;
116                         if (length $s2 > $i) {
117                                 if ($s2 =~ /^\Q$prefix/) {
118                                         $last = substr $s2, $i, 1;
119                                         next if $char eq $last;
120                                         $last = chr( ord($last) - (length $s2 > 1) );
121                                         next if $next gt $last;
122                                 }
123                         }
124
125                         if ($char eq '.') {
126                                 if ($last eq 'z') {
127 #                                       push @allow, $prefix if $i and $lasti;
128 #                                       next;
129                                 }
130 #                               if ($last eq 'z') {
131 #                                       push @allow, $prefix if $i and $lasti;
132 #                                       next;
133 #                               }
134                                 $next = 'a';
135                         }
136
137                         push @allow, $prefix."[$next-$last]";
138                 }
139                 continue {
140                         $prefix .= $char eq '.' ? '[^a-z]' : $char;
141                 }
142         }
143
144         if (length $s2) {
145                 my $prefix = '';
146                 my $char;
147                 for my $i (0 .. length($s2) - 1) {
148                         $char = substr $s2, $i, 1;
149                         my $last = 'z';
150                         if (length $s1 > $i) {
151                                 my $c1 = substr $s1, $i, 1;
152                                 if ($s1 =~ /^\Q$prefix/) {
153                                         next if $c1 le $char;
154                                 }
155                         }
156
157                         if ($char eq '.') {
158                                 next if $i < length($s2) - 1;
159                         }
160
161                         push @allow, $prefix.'(?!['.($char eq '.' ? 'a' : $char)."-$last])"
162                                 if $i or $s1 eq '';
163                 }
164                 continue {
165                         $prefix .= $char eq '.' ? '[^a-z]' : $char;
166                 }
167
168                 push @allow, $prefix
169                         if $s2 =~ /^\Q$prefix/ and $s1 le $s2
170                         and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
171         }
172
173         my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
174         return qr/^$match/i;
175 }
176
177 1;
178
179 __END__
180
181 =head1 NAME
182
183 List::Index - Find and apply prefix ranges to paginate keywords
184
185 =head1 SYNOPSIS
186
187         use List::Index;
188         my $index = List::Index->new(\@values);
189         my @pages = $index->ranges({pagesize => 50});
190         say "<a href='?q=$_'>$_</a>" for @pages;
191
192         use List::Index 'rangematch';
193         my $limit = rangematch('b-bmq');  # ge 'b' && le 'bmq'
194         @request = grep { $limit } @values;
195
196 =head1 DESCRIPTION
197
198 TODO
199
200 =head1 SEE ALSO
201
202 L<List::Maker|List::Maker> for complex ranges of numeric lists.
203
204 =head1 AUTHOR
205
206 Mischa POSLAWSKY <perl@shiar.org>
207
208 =head1 LICENSE
209
210 Copyright. All rights reserved.
211