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