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