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