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