Regex/TrivialRange.pm

filter: Statementstmt 100% 64 / 64 Branchbran 86.6% 26 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 94.2% 98 / 104 CC 16 SCAR 24.4
CC
16
Cov
96%
regex
16.1
BEGIN
1.0
BEGIN
1.0
CRAP 11.5
↑ Summary Regex/SimpleRange.pm »
1 # Number::Range::Regex::TrivialRange
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::TrivialRange;
8  
9 # one range, expressible in the form $header.$range.$trailer, where
10 # header = \d+
11 # range = [\d-\d]
12 # trailer = \\d+
13 # e.g. 12[3-8]\d\d
14  
15 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use vars qw ( @ISA @EXPORT @EXPORT_OK $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 eval { require warnings; }; #it's ok if we can't load warnings
18  
19 14 require Exporter;
20 14 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 @ISA = qw( Exporter Number::Range::Regex::SimpleRange );
22  
23 14 $VERSION = '0.32';
24  
25 14 ▶use Number::Range::Regex::SimpleRange;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶use Number::Range::Regex::Util ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 14 ▶use Number::Range::Regex::Util::inf qw ( neg_inf pos_inf _is_negative );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 968 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 968 my ($class, $min, $max, $passed_opts) = @_;
31  
32 968 my $opts = option_mangler( $passed_opts );
33  
34 968 my $base = $opts->{base};
35 968 my $base_digits = $opts->{base_digits} = base_digits($base);
36 968 my $base_max = $opts->{base_max} = substr($base_digits, -1);
37 968 my $base_digits_regex = $opts->{base_digits_regex} = _calculate_digit_range( 0, $base_max, $base_digits );
38  
39 968 return bless { min => $min, max => $max, opts => $opts }, $class;
40 }
41  
42 5475 ▶sub regex {
Subroutine regex: called CC=16 SCAR=27.8
Pod undocumented
43 5475 my ($self, $passed_opts) = @_;
44  
45 5475 my $opts = option_mangler( $self->{opts}, $passed_opts );
46  
47 5475 ▶ my $zeroes_maybe = $opts->{no_leading_zeroes} ? '' : '0*';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'no_leading_zeroes'} ? : 303 5172 5475
48  
49 5475 ▶ if( _is_negative( $self->{min} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_negative($self->{'min'})) { } 134 5341 5475
50 134 my $pmin = abs $self->{max};
51 # -'-inf' == 'inf' according to perl. that's no good for us
52 #TODO: is the above still true with Util/inf.pm ?
53 134 ▶ my $pmax = ($self->{min} == neg_inf) ? pos_inf : abs $self->{min};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'min'} == neg_inf() ? : 38 96 134
54 134 my $re_part = Number::Range::Regex::TrivialRange->new( $pmin, $pmax )->
55 regex( { no_leading_zeroes => 1, no_sign => 1 } );
56 134 return qr/-$zeroes_maybe$re_part/;
57 } else {
58 5341 ▶ my $sign_maybe = $opts->{no_sign} ? '' : '[+]?';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'no_sign'} ? : 134 5207 5341
59 5341 ▶ if($self->{min} eq $self->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'min'} eq $self->{'max'}) { } 512 4829 5341
60 512 return qr/$sign_maybe$zeroes_maybe$self->{min}/;
61 } else {
62 #note: because of the nature of a trivial range, max must also be positive
63 4829 my $ndigits = length $self->{min};
64 4829 ▶ if($self->{max} == pos_inf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'max'} == pos_inf()) { } 77 4752 4829
65 # for a trivial range extending to +inf, min must be /^10+$/
66 77 my $trailer;
67 77 ▶ if($opts->{no_leading_zeroes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'no_leading_zeroes'}) { } 55 22 77
68 55 ▶ die "internal error" if $ndigits <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ndigits <= 1 0 55 55
69 55 $ndigits--; #change the first '\d' to '[1-9]'
70 55 $trailer = "[1-$opts->{base_max}]";
71 55 $zeroes_maybe = '';
72 } else {
73 22 $trailer = '';
74 }
75 $trailer .= $ndigits == 0 ? '' :
76 $ndigits == 1 ? $opts->{base_digits_regex} :
77 77 ▶ "$opts->{base_digits_regex}\{$ndigits,\}";
Branch: 2 branches
BranchTrueFalseTotal
$ndigits == 0 ? : 0 77 77
$ndigits == 1 ? : 55 22 77
78 77 return qr/$sign_maybe$zeroes_maybe$trailer/;
79 } else {
80 4752 ▶ die "internal error" if $ndigits != length $self->{max};
Branch: 1 branch
BranchTrueFalseTotal
if $ndigits != length $self->{"max"} 0 4752 4752
81 4752 my $nsame = 0;
82 4752 ▶ for(; $nsame<$ndigits; $nsame++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsame < $ndigits) 10576 0 10576
83 10576 ▶ last if substr($self->{min}, $nsame, 1) ne substr($self->{max}, $nsame, 1);
Branch: 1 branch
BranchTrueFalseTotal
if substr($self->{"min"}, $nsame, 1) ne substr($self->{"max"}, $nsame, 1) 4752 5824 10576
84 }
85 4752 my $static_header = substr($self->{min}, 0, $nsame);
86 4752 my $dig_min = substr($self->{min}, $nsame, 1);
87 4752 my $dig_max = substr($self->{max}, $nsame, 1);
88 4752 my $digit_range = "[$dig_min-$dig_max]";
89 4752 my $extra_digits = $ndigits-$nsame-1;
90 my $trailer = $extra_digits == 0 ? '' :
91 $extra_digits == 1 ? $opts->{base_digits_regex} :
92 4752 ▶ "$opts->{base_digits_regex}\{$extra_digits\}";
Branch: 2 branches
BranchTrueFalseTotal
$extra_digits == 0 ? : 2627 2125 4752
$extra_digits == 1 ? : 1468 657 2125
93 4752 return qr/$sign_maybe$zeroes_maybe$static_header$digit_range$trailer/;
94 }
95 }
96 }
97 }
98  
99 # touches/union/intersect/subtract/etc. inherit from SimpleRange.pm
100  
101 14 1;
102  
↑ Summary Regex/SimpleRange.pm »