X-Git-Url: http://git.shiar.nl/perl/list-index.git/blobdiff_plain/3df9395c1a2f641564d8fca7fc843d066d1411d4..e269a677a2adb49fc79e6c2267551a716a55c559:/lib/List/Index.pm diff --git a/lib/List/Index.pm b/lib/List/Index.pm index c799221..89fdc75 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -4,7 +4,10 @@ use 5.010; use strict; use warnings; +use Exporter 'import'; + our $VERSION = '1.00'; +our @EXPORT_OK = qw(rangematch); sub new { my ($class, $values) = @_; @@ -49,6 +52,46 @@ sub ranges { return \@links; } +sub rangematch { + my ($link) = @_; + my ($s1, $s2) = $link =~ /([^-]*) - ([^-]*)/x + or return qr/^\Q$link/i; + my @allow; + + if (length $s1) { + my $prefix = ''; + my $c1; + for my $i (0 .. length($s1) - 1) { + $c1 = substr $s1, $i, 1; + my $c2 = length $s2 <= $i ? undef : substr $s2, $i, 1; + my $next = $i + 1 >= length($s1) ? $c1 : chr( ord($c1) + 1 ); + my $last = defined $c2 && $i == 0 ? chr( ord($c2) - (length $s2 > 1) ) : 'z'; + $next le $last or next if defined $c2; + push @allow, $prefix."[$next-$last]"; + } + continue { + $prefix .= $c1; + } + } + + if (length $s2) { + my $prefix = ''; + for my $i (0 .. length($s2) - 1) { + my $c1 = length $s1 <= $i ? undef : substr $s1, $i, 1; + my $c2 = substr $s2, $i, 1; + my $last = 'z'; + push @allow, "$prefix(?![$c2-$last])" + if $i or $s1 eq ''; + $prefix .= $c2; + } + push @allow, $prefix + unless length $s1 > length $s2 or length $s1 != 0 && length $s2 == 1; #TODO + } + + my $match = sprintf @allow <= 1 ? '%s' : '(?:%s)', join('|', @allow); + return qr/^$match/i; +} + 1; __END__