| 1 | # Number::Range::Regex::Util | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright 2012 Brian Szymanski. All rights reserved. This module is | ||||||||||||||||||||||||||||
| 4 | # free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 5 | # terms as Perl itself. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Number::Range::Regex::Util; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use vars qw ( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 15 | eval { require warnings; }; #it's ok if we can't load warnings | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 15 | require Exporter; | |||||||||||||||||||||||||||
| 14 | 15 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 15 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 16 | 15 | @EXPORT = qw ( option_mangler has_regex_overloading | |||||||||||||||||||||||||||
| 17 | multi_union empty_set | ||||||||||||||||||||||||||||
| 18 | base_chr base_ord base_digits base_next base_prev | ||||||||||||||||||||||||||||
| 19 | _calculate_digit_range ); | ||||||||||||||||||||||||||||
| 20 | 15 | @EXPORT_OK = qw ( _order_by_min ) ; | |||||||||||||||||||||||||||
| 21 | 15 | %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 15 | $VERSION = '0.32'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 15 | require overload; | |||||||||||||||||||||||||||
| 26 | 11 | ▶ | sub has_regex_overloading { | ||||||||||||||||||||||||||
|
Subroutine
has_regex_overloading: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | # http://www.gossamer-threads.com/lists/perl/porters/244314 | ||||||||||||||||||||||||||||
| 28 | # http://search.cpan.org/~jesse/perl-5.12.0/pod/perl5120delta.pod#qr_overload$ | ||||||||||||||||||||||||||||
| 29 | # 1.08, 1.09 are too low. 1.10: works | ||||||||||||||||||||||||||||
| 30 | # http://search.cpan.org/~jesse/perl-5.11.1/lib/overload.pm | ||||||||||||||||||||||||||||
| 31 | 11 | ▶ | return defined $overload::VERSION && $overload::VERSION > '1.09'; | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $overload::VERSION && $overload::VERSION > '1.09'
MC/DC0%0/2
defined $overload::VERSION && $overload::VERSION > '1.09'
defined $overload::VERSION
$overload::VERSION > '1.09'
Truth table33%1/3
A && B
A= defined $overload::VERSIONB= $overload::VERSION > '1.09'
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 585 | ▶ | sub empty_set { | ||||||||||||||||||||||||||
|
Subroutine
empty_set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 585 | shift; | |||||||||||||||||||||||||||
| 36 | 585 | return Number::Range::Regex::CompoundRange->new( @_ ); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 568 | ▶ | sub multi_union { | ||||||||||||||||||||||||||
|
Subroutine
multi_union: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 568 | ▶ | my $opts = option_mangler( ref $_[-1] eq 'HASH' ? pop : undef ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 568 | my $warn_overlap = delete $opts->{warn_overlap}; | |||||||||||||||||||||||||||
| 42 | 568 | my @ranges = @_; | |||||||||||||||||||||||||||
| 43 | 568 | my $self = empty_set( $opts ); | |||||||||||||||||||||||||||
| 44 | 568 | $self = $self->union( $_, { warn_overlap => $warn_overlap } ) for @ranges; | |||||||||||||||||||||||||||
| 45 | # $self->{opts} = $opts; | ||||||||||||||||||||||||||||
| 46 | 568 | return $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # local options can override defaults | ||||||||||||||||||||||||||||
| 50 | 13061 | ▶ | sub option_mangler { | ||||||||||||||||||||||||||
|
Subroutine
option_mangler: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 13061 | my (@passed_opts) = grep defined, @_; | |||||||||||||||||||||||||||
| 52 | # next line is redundant but an optimization | ||||||||||||||||||||||||||||
| 53 | 13061 | ▶ | return $Number::Range::Regex::Range::default_opts unless @passed_opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 10085 | unshift @passed_opts, $Number::Range::Regex::Range::default_opts; | |||||||||||||||||||||||||||
| 55 | 10085 | my $opts; | |||||||||||||||||||||||||||
| 56 | 10085 | foreach my $opts_ref ( @passed_opts ) { | |||||||||||||||||||||||||||
| 57 | 26029 | ▶ | die "too many arguments from ".join(":", caller())." $opts_ref" unless ref $opts_ref eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | # make a copy of options hashref, add overrides | ||||||||||||||||||||||||||||
| 59 | 26028 | ▶ | while (my ($key, $val) = each %$opts_ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 300820 | $opts->{$key} = $val; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 10084 | return $opts; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 901 | ▶ | sub _order_by_min { | ||||||||||||||||||||||||||
|
Subroutine
_order_by_min: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | 901 | my ($a, $b) = @_; | |||||||||||||||||||||||||||
| 68 | 901 | ▶ | return $a->{min} < $b->{min} ? ($a, $b) : ($b, $a); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2665 | ▶ | sub base_digits { | ||||||||||||||||||||||||||
|
Subroutine
base_digits: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 2665 | my ($base) = @_; | |||||||||||||||||||||||||||
| 73 | 2665 | return join '', map { $Number::Range::Regex::Range::STANDARD_DIGIT_ORDER[$_] } (0..$base-1); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 317 | ▶ | sub base_next { | ||||||||||||||||||||||||||
|
Subroutine
base_next: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 317 | my ($c, $base_digits) = @_; | |||||||||||||||||||||||||||
| 78 | 317 | my $ord = base_ord($c, $base_digits); | |||||||||||||||||||||||||||
| 79 | 317 | ▶ | return if $ord+1 == length $base_digits; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 244 | return base_chr($ord+1, $base_digits); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 317 | ▶ | sub base_prev { | ||||||||||||||||||||||||||
|
Subroutine
base_prev: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 317 | my ($c, $base_digits) = @_; | |||||||||||||||||||||||||||
| 85 | 317 | my $ord = base_ord($c, $base_digits); | |||||||||||||||||||||||||||
| 86 | 317 | ▶ | return if $ord == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 240 | return base_chr($ord-1, $base_digits); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | #TODO: memoize base_ord, base_chr for performance? | ||||||||||||||||||||||||||||
| 91 | 6534 | ▶ | sub base_ord { | ||||||||||||||||||||||||||
|
Subroutine
base_ord: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 6534 | my ($c, $base_digits) = @_; | |||||||||||||||||||||||||||
| 93 | 6534 | ▶ | return -1 if $c eq -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 6534 | ▶ | return 1+length $base_digits if length $c > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 6534 | my $ord = index $base_digits, $c; | |||||||||||||||||||||||||||
| 96 | 6534 | ▶ | die "$c not found in $base_digits" if $ord == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 6534 | return $ord; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 25043 | ▶ | sub base_chr { | ||||||||||||||||||||||||||
|
Subroutine
base_chr: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 25043 | my ($n, $base_digits) = @_; | |||||||||||||||||||||||||||
| 102 | 25043 | my $chr = substr($base_digits, $n, 1); | |||||||||||||||||||||||||||
| 103 | 25043 | ▶ | die "offset out of range: $n > ".length($base_digits) if !length $chr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 25043 | return $chr; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | #TODO: should _calculate_digit_range() be in Util? | ||||||||||||||||||||||||||||
| 108 | # calculate the tersest possible representation of a digit range | ||||||||||||||||||||||||||||
| 109 | # '1' -> 1 | ||||||||||||||||||||||||||||
| 110 | # '12' -> [12] | ||||||||||||||||||||||||||||
| 111 | # '123' -> [1-3] #preferred stylistically to [123] | ||||||||||||||||||||||||||||
| 112 | # '1234' -> [1-4] | ||||||||||||||||||||||||||||
| 113 | # '0123456789' -> \d | ||||||||||||||||||||||||||||
| 114 | # '123456789abc' -> [1-9a-c] | ||||||||||||||||||||||||||||
| 115 | 3100 | ▶ | sub _calculate_digit_range { | ||||||||||||||||||||||||||
|
Subroutine
_calculate_digit_range: called CC=17 SCAR=28.3
| |||||||||||||||||||||||||||||
| 116 | 3100 | my ($digit_min, $digit_max, $base_digits) = @_; | |||||||||||||||||||||||||||
| 117 | 3100 | ▶ | return unless defined $digit_min && defined $digit_max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $digit_min and defined $digit_max
MC/DC50%1/2
defined $digit_min and defined $digit_max
defined $digit_min
defined $digit_max
Truth table66%2/3
A and B
A= defined $digit_minB= defined $digit_max
| |||||||||||||||||||||||||||||
| 118 | 2950 | my $ord_min = base_ord( $digit_min, $base_digits ); | |||||||||||||||||||||||||||
| 119 | 2950 | my $ord_max = base_ord( $digit_max, $base_digits ); | |||||||||||||||||||||||||||
| 120 | 2950 | ▶ | return if $ord_min > $ord_max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2830 | ▶ | return $digit_min if $ord_min == $ord_max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 2724 | my @range_chars; | |||||||||||||||||||||||||||
| 123 | 2724 | ▶ | for(my $n=$ord_min; $n <= $ord_max; ++$n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 24559 | push @range_chars, base_chr( $n, $base_digits ); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 2724 | my $last = $range_chars[0]; | |||||||||||||||||||||||||||
| 127 | 2724 | my $n = 1; | |||||||||||||||||||||||||||
| 128 | 2724 | ▶ | while($n < @range_chars) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 21835 | my $this = $range_chars[$n]; | |||||||||||||||||||||||||||
| 130 | 21835 | ▶ | if(1 == ord($this)-ord($last)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 21832 | $range_chars[$n-1] .= $this; | |||||||||||||||||||||||||||
| 132 | 21832 | splice @range_chars, $n, 1; | |||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||
| 134 | 3 | $n++; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 21835 | $last = $this; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 2724 | foreach my $n (0..$#range_chars) { | |||||||||||||||||||||||||||
| 139 | 2727 | my $str = $range_chars[$n]; | |||||||||||||||||||||||||||
| 140 | 2727 | my $len = length $str; | |||||||||||||||||||||||||||
| 141 | 2727 | ▶ | die "internal error" if $len == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 2727 | ▶ | next if $len == 1; # 'a' is as terse as possible | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 2726 | ▶ | next if $len == 2; # 'bc' is also as terse as possible | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | # collapse e.g. 234567 into 2-7 | ||||||||||||||||||||||||||||
| 145 | 2570 | my $first = substr($str, 0, 1); | |||||||||||||||||||||||||||
| 146 | 2570 | my $last = substr($str, -1, 1); | |||||||||||||||||||||||||||
| 147 | 2570 | ▶ | $range_chars[$n] = ($first eq '0' && $last eq '9') ? '\d' : "$first-$last"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first eq 0 and $last eq 9
MC/DC100%2/2
$first eq 0 and $last eq 9
$first eq 0
$last eq 9
Truth table100%3/3
A and B
A= $first eq 0B= $last eq 9
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 2724 | ▶ | if(1==@range_chars) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 2721 | my $ret = $range_chars[0]; | |||||||||||||||||||||||||||
| 151 | # we don't need brackets if all we have is \d or a single digit | ||||||||||||||||||||||||||||
| 152 | 2721 | ▶ | return $ret if $ret eq '\d' || length($ret)==1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ret eq "\\d" or length $ret == 1
MC/DC50%1/2
$ret eq "\\d" or length $ret == 1
$ret eq "\\d"
length $ret == 1
Truth table66%2/3
A or B
A= $ret eq "\\d"B= length $ret == 1
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 504 | return join '', '[', @range_chars, ']'; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 15 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||