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