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