fix rangematch('zz-')
[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         my @allow;
82
83         if (length $s1) {
84                 if (length $s2) {
85                         $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
86                 }
87
88                 my $prefix = '';
89                 my $char;
90                 for my $i (0 .. length($s1) - 1) {
91                         $char = substr $s1, $i, 1;
92                         my $next = $char;
93                         $next = chr( ord($char) + 1 ) if length $s1 > $i + 1;
94                         my $last = 'z';
95                         next if $next gt $last;
96                         if (length $s2 > $i) {
97                                 if ($s2 =~ /^\Q$prefix/) {
98                                         $last = substr $s2, $i, 1;
99                                         next if $char eq $last;
100                                         $last = chr( ord($last) - (length $s2 > 1) );
101                                         next if $next gt $last;
102                                 }
103                         }
104                         push @allow, $prefix."[$next-$last]";
105                 }
106                 continue {
107                         $prefix .= $char;
108                 }
109         }
110
111         if (length $s2) {
112                 my $prefix = '';
113                 my $char;
114                 for my $i (0 .. length($s2) - 1) {
115                         $char = substr $s2, $i, 1;
116                         my $last = 'z';
117                         if (length $s1 > $i) {
118                                 my $c1 = substr $s1, $i, 1;
119                                 if ($s1 =~ /^\Q$prefix/) {
120                                         next if $c1 le $char;
121                                 }
122                         }
123                         push @allow, $prefix."(?![$char-$last])"
124                                 if $i or $s1 eq '';
125                 }
126                 continue {
127                         $prefix .= $char;
128                 }
129
130                 push @allow, $prefix
131                         if $s2 =~ /^\Q$prefix/ and $s1 le $s2
132                         and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
133         }
134
135         my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
136         return qr/^$match/i;
137 }
138
139 1;
140
141 __END__
142
143 =head1 NAME
144
145 List::Index - Paginate alphabetic entries by finding minimal prefixes
146
147 =head1 SYNOPSIS
148
149         use List::Index;
150         my $index = List::Index->new(\@values);
151         my @pages = $index->ranges({pagesize => 50});
152         printf '<a href="?q=%s-%s">%1$s</a> ', @$_ for @pages;
153
154         use List::Index 'rangematch';
155         my $limit = rangematch('b-bmq');  # matches prefix like 'baa'..'bmq'
156         @results = grep { $limit } @results;
157
158 =head1 DESCRIPTION
159
160 TODO
161
162 =head1 SEE ALSO
163
164 L<List::Maker|List::Maker> for complex ranges of numeric lists.
165
166 =head1 AUTHOR
167
168 Mischa POSLAWSKY <perl@shiar.org>
169
170 =head1 LICENSE
171
172 Copyright. All rights reserved.
173