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