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