89fdc75c83fa3c7b8709734f6b0403aeee087048
[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.00';
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 '.' ? 'z' : 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                 my $prefix = '';
63                 my $c1;
64                 for my $i (0 .. length($s1) - 1) {
65                         $c1 = substr $s1, $i, 1;
66                         my $c2 = length $s2 <= $i ? undef : substr $s2, $i, 1;
67                         my $next = $i + 1 >= length($s1) ? $c1 : chr( ord($c1) + 1 );
68                         my $last = defined $c2 && $i == 0 ? chr( ord($c2) - (length $s2 > 1) ) : 'z';
69                         $next le $last or next if defined $c2;
70                         push @allow, $prefix."[$next-$last]";
71                 }
72                 continue {
73                         $prefix .= $c1;
74                 }
75         }
76
77         if (length $s2) {
78                 my $prefix = '';
79                 for my $i (0 .. length($s2) - 1) {
80                         my $c1 = length $s1 <= $i ? undef : substr $s1, $i, 1;
81                         my $c2 = substr $s2, $i, 1;
82                         my $last = 'z';
83                         push @allow, "$prefix(?![$c2-$last])"
84                                 if $i or $s1 eq '';
85                         $prefix .= $c2;
86                 }
87                 push @allow, $prefix
88                         unless length $s1 > length $s2 or length $s1 != 0 && length $s2 == 1; #TODO
89         }
90
91         my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow);
92         return qr/^$match/i;
93 }
94
95 1;
96
97 __END__
98
99 =head1 NAME
100
101 List::Index - Paginate alphabetic entries by finding minimal prefixes
102
103 =head1 SYNOPSIS
104
105         use List::Index;
106         my $index = List::Index->new(\@values);
107         my @pages = $index->ranges({pagesize => 50});
108         printf '<a href="?start=%s&amp;end=%s">%1$s</a> ', @$_ for @pages;
109
110 =head1 DESCRIPTION
111
112 TODO
113
114 =head1 SEE ALSO
115
116 L<List::Maker|List::Maker> for complex ranges of numeric lists.
117
118 =head1 AUTHOR
119
120 Mischa POSLAWSKY <perl@shiar.org>
121
122 =head1 LICENSE
123
124 Copyright. All rights reserved.
125