XXX: catch invalid rangematch() input (return undef, tests)
[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 = 0;
27         my @links;
28         while ($offset < @$self) {
29                 my $link = substr $self->[$offset], 0, $length;
30                 if ($context) {
31                         my $trim = 1;
32                         my $before = $offset > $context ? $self->[$offset - $context] : '';
33                         for my $match (split //, $before) {
34                                 scalar $link =~ /\G\Q$match/g or last;
35                                 $trim++;
36                         }
37                         substr($link, $trim) = '' unless $trim > length $link;
38                 }
39
40                 push @links, [$link];
41                 $offset += $pagesize;
42         }
43
44         for my $i (0 .. $#links - 1) {
45                 my ($link, $lastchar) = $links[$i + 1]->[0] =~ /(.*)(.)/;
46                 $link .= $lastchar eq '.' ? 'a' : chr( ord($lastchar) - 1 )
47                         unless $lastchar eq 'a';
48                 $links[$i]->[1] = $link;
49         }
50         $links[-1]->[1] = '';
51
52         return \@links;
53 }
54
55 sub rangematch {
56         my ($link) = @_;
57         my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x
58                 or return qr/^\Q$link/i;
59         my @allow;
60
61         if (length $s1) {
62                 if (length $s2) {
63                         $s1 le $s2 or $s1 =~ /^\Q$s2/ or return undef;
64                 }
65
66                 my $prefix = '';
67                 my $char;
68                 for my $i (0 .. length($s1) - 1) {
69                         $char = substr $s1, $i, 1;
70                         my $next = $char;
71                         $next = chr( ord($char) + 1 ) if length $s1 > $i + 1;
72                         my $last = 'z';
73                         if (length $s2 > $i) {
74                                 if ($s2 =~ /^\Q$prefix/) {
75                                         $last = substr $s2, $i, 1;
76                                         next if $char eq $last;
77                                         $last = chr( ord($last) - (length $s2 > 1) );
78                                         next if $next gt $last;
79                                 }
80                         }
81                         push @allow, $prefix."[$next-$last]";
82                 }
83                 continue {
84                         $prefix .= $char;
85                 }
86         }
87
88         if (length $s2) {
89                 my $prefix = '';
90                 my $char;
91                 for my $i (0 .. length($s2) - 1) {
92                         $char = substr $s2, $i, 1;
93                         my $last = 'z';
94                         if (length $s1 > $i) {
95                                 my $c1 = substr $s1, $i, 1;
96                                 if ($s1 =~ /^\Q$prefix/) {
97                                         next if $c1 le $char;
98                                 }
99                         }
100                         push @allow, $prefix."(?![$char-$last])"
101                                 if $i or $s1 eq '';
102                 }
103                 continue {
104                         $prefix .= $char;
105                 }
106
107                 push @allow, $prefix
108                         if $s2 =~ /^\Q$prefix/ and $s1 le $s2
109                         and not (length $s2 == 1 && length $s1 >= length $s2 && $s1 ne $s2);
110         }
111
112         my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
113         return qr/^$match/i;
114 }
115
116 1;
117
118 __END__
119
120 =head1 NAME
121
122 List::Index - Paginate alphabetic entries by finding minimal prefixes
123
124 =head1 SYNOPSIS
125
126         use List::Index;
127         my $index = List::Index->new(\@values);
128         my @pages = $index->ranges({pagesize => 50});
129         printf '<a href="?q=%s-%s">%1$s</a> ', @$_ for @pages;
130
131         use List::Index 'rangematch';
132         my $limit = rangematch('b-bmq');  # matches prefix like 'baa'..'bmq'
133         @results = grep { $limit } @results;
134
135 =head1 DESCRIPTION
136
137 TODO
138
139 =head1 SEE ALSO
140
141 L<List::Maker|List::Maker> for complex ranges of numeric lists.
142
143 =head1 AUTHOR
144
145 Mischa POSLAWSKY <perl@shiar.org>
146
147 =head1 LICENSE
148
149 Copyright. All rights reserved.
150