more legible code to add range ends
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 15 Nov 2009 03:43:41 +0000 (04:43 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sun, 15 Nov 2009 03:46:51 +0000 (04:46 +0100)
lib/List/Index.pm

index e56d7713be5f5e113c7152b8a19f49e904d79b61..719aca74ed00acf60feddb2437bcce314410277f 100644 (file)
@@ -77,12 +77,15 @@ sub ranges {
                push @links, $link unless $links[-1] eq $link;
        }
 
+       # add range end to each link
        for my $i (0 .. $#links - 1) {
-               my ($link, $lastchar) = $links[$i + 1] =~ /(.*)(.)/;
-               $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 );
-               next if $link eq $links[$i] and $i;
-               $links[$i] .= '-'.$link;
+               # end at start of next value with the last character decremented
+               my $next = $links[$i + 1];
+               $next =~ s{(.)$}{ $1 le 'a' ? '.' : chr( ord($1) - 1 ) }e;
+               # amend range if it's ahead
+               $links[$i] .= '-'.$next unless $next eq $links[$i];
        }
+       # final value takes the rest
        $links[-1] .= '-';
 
        return \@links;