Common/number.pm

filter: Statementstmt 100% 70 / 70 Branchbran 83.3% 25 / 30 Conditioncond 61.9% 13 / 21 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 4 totaltotal 83.6% 128 / 153 CC 25 SCAR 18.9
CC
25
Cov
89%
real_creator
10.0
int_creator
8.8
decimal_creator
8.4
CRAP 6.6
« Common.pm ↑ Summary Common/delimited.pm »
1 package Regexp::Common::number;
2  
3 71 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 71 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 71 ▶use Regexp::Common qw /pattern clean no_defaults/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 71 our $VERSION = '2024080801';
13  
14  
15 4 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
16 4 require Carp;
17 4 goto &Carp::croak;
18 }
19  
20 71 my $digits = join ("", 0 .. 9, "A" .. "Z");
21  
22 13908 ▶sub int_creator {
Subroutine int_creator: called CC=8 SCAR=21.7
Pod undocumented
23 13908 my $flags = $_ [1];
24 my ($sep, $group, $base, $places, $sign) =
25 13908 @{$flags} {qw /-sep -group -base -places -sign/};
26  
27 # Deal with the bases.
28 13908 ▶ _croak "Base must be between 1 and 36" unless $base >= 1 &&
Branch: 1 branch
BranchTrueFalseTotal
unless $base >= 1 and $base <= 36 0 13908 13908
Condition33%1/3
$base >= 1 and $base <= 36
!ll&&!rl&&r
0013908
MC/DC0%0/2
$base >= 1 and $base <= 36
$base >= 1 $base <= 36
Truth table33%1/3
A and B
A= $base >= 1B= $base <= 36
ABresult
0X0
100
111
29 $base <= 36;
30 13908 my $chars = substr $digits, 0, $base;
31  
32 13908 ▶ $sep = ',' if exists $flags -> {-sep} && !defined $flags -> {-sep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $flags->{"-sep"} and not defined $flags->{"-sep"} 0 13908 13908
Condition33%1/3
exists $flags->{"-sep"} and not defined $flags->{"-sep"}
!ll&&!rl&&r
0139080
MC/DC0%0/2
exists $flags->{"-sep"} and not defined $flags->{"-sep"}
exists $flags->{"-sep"} not defined $flags->{"-sep"}
Truth table33%1/3
A and not(B)
A= exists $flags->{"-sep"}B= not defined $flags->{"-sep"}
ABresult
0X0
100
111
33  
34 13908 my $max = $group;
35 13908 ▶ $max = $2 if $group =~ /^\s*(\d+)\s*,\s*(\d+)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $group =~ /^\s*(\d+)\s*,\s*(\d+)\s*$/ 134 13774 13908
36  
37 13908 ▶ my $quant = $places ? "{$places}" : "+";
Branch: 1 branch
BranchTrueFalseTotal
$places ? : 8660 5248 13908
38  
39 13908 ▶ return $sep ? qq {(?k:(?k:$sign)(?k:[$chars]{1,$max}} .
Branch: 1 branch
BranchTrueFalseTotal
$sep ? : 678 13230 13908
40 qq {(?:$sep} . qq {[$chars]{$group})*))}
41 : qq {(?k:(?k:$sign)(?k:[$chars]$quant))}
42 }
43  
44 45 ▶sub real_creator {
Subroutine real_creator: called CC=10 SCAR=23.1
Pod undocumented
45 my ($base, $places, $radix, $sep, $group, $expon, $sign) =
46 45 @{$_[1]}{-base, -places, -radix, -sep, -group, -expon, -sign};
47 45 ▶ _croak "Base must be between 1 and 36"
Branch: 1 branch
BranchTrueFalseTotal
unless $base >= 1 and $base <= 36 2 43 45
Condition100%3/3
$base >= 1 and $base <= 36
!ll&&!rl&&r
1143
MC/DC100%2/2
$base >= 1 and $base <= 36
$base >= 1 $base <= 36
Truth table100%3/3
A and B
A= $base >= 1B= $base <= 36
ABresult
0X0
100
111
48 unless $base >= 1 && $base <= 36;
49 $sep = ',' if exists $_[1]->{-sep}
50 43 ▶ && !defined $_[1]->{-sep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[1]{"-sep"} and not defined $_[1]{"-sep"} 13 30 43
Condition66%2/3
exists $_[1]{"-sep"} and not defined $_[1]{"-sep"}
!ll&&!rl&&r
03013
MC/DC50%1/2
exists $_[1]{"-sep"} and not defined $_[1]{"-sep"}
exists $_[1]{"-sep"} not defined $_[1]{"-sep"}
Truth table66%2/3
A and not(B)
A= exists $_[1]{"-sep"}B= not defined $_[1]{"-sep"}
ABresult
0X0
100
111
51 43 ▶ if ($base > 14 && $expon =~ /^[Ee]$/) {$expon = 'G'}
Branch: 1 branch
BranchTrueFalseTotal
if ($base > 14 and $expon =~ /^[Ee]$/) 14 29 43
Condition66%2/3
$base > 14 and $expon =~ /^[Ee]$/
!ll&&!rl&&r
29014
MC/DC50%1/2
$base > 14 and $expon =~ /^[Ee]$/
$base > 14 $expon =~ /^[Ee]$/
Truth table66%2/3
A and B
A= $base > 14B= $expon =~ /^[Ee]$/
ABresult
0X0
100
111
52 43 ▶ foreach ($radix, $sep, $expon) {$_ = "[$_]" if 1 == length}
Branch: 1 branch
BranchTrueFalseTotal
if 1 == length $_ 57 72 129
53 43 my $chars = substr $digits, 0, $base;
54 43 ▶ return $sep
Branch: 1 branch
BranchTrueFalseTotal
$sep ? : 16 27 43
55 ? qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
56 qq {(?k:[$chars]{1,$group}(?:(?:$sep)[$chars]{$group})*)} .
57 qq {(?:(?k:$radix)(?k:[$chars]{$places}))?)} .
58 qq {(?:(?k:$expon)(?k:(?k:$sign)(?k:[$chars]+))|))}
59 : qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
60 qq {(?k:[$chars]*)(?:(?k:$radix)(?k:[$chars]{$places}))?)} .
61 qq {(?:(?k:$expon)(?k:(?k:$sign)(?k:[$chars]+))|))};
62 }
63 3721 ▶sub decimal_creator {
Subroutine decimal_creator: called CC=8 SCAR=21.3
Pod undocumented
64 my ($base, $places, $radix, $sep, $group, $sign) =
65 3721 @{$_[1]}{-base, -places, -radix, -sep, -group, -sign};
66 3721 ▶ _croak "Base must be between 1 and 36"
Branch: 1 branch
BranchTrueFalseTotal
unless $base >= 1 and $base <= 36 2 3719 3721
Condition100%3/3
$base >= 1 and $base <= 36
!ll&&!rl&&r
113719
MC/DC100%2/2
$base >= 1 and $base <= 36
$base >= 1 $base <= 36
Truth table100%3/3
A and B
A= $base >= 1B= $base <= 36
ABresult
0X0
100
111
67 unless $base >= 1 && $base <= 36;
68 $sep = ',' if exists $_[1]->{-sep}
69 3719 ▶ && !defined $_[1]->{-sep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[1]{"-sep"} and not defined $_[1]{"-sep"} 0 3719 3719
Condition33%1/3
exists $_[1]{"-sep"} and not defined $_[1]{"-sep"}
!ll&&!rl&&r
037190
MC/DC0%0/2
exists $_[1]{"-sep"} and not defined $_[1]{"-sep"}
exists $_[1]{"-sep"} not defined $_[1]{"-sep"}
Truth table33%1/3
A and not(B)
A= exists $_[1]{"-sep"}B= not defined $_[1]{"-sep"}
ABresult
0X0
100
111
70 3719 ▶ foreach ($radix, $sep) {$_ = "[$_]" if 1 == length}
Branch: 1 branch
BranchTrueFalseTotal
if 1 == length $_ 945 6493 7438
71 3719 my $chars = substr $digits, 0, $base;
72 3719 ▶ return $sep
Branch: 1 branch
BranchTrueFalseTotal
$sep ? : 0 3719 3719
73 ? qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
74 qq {(?k:[$chars]{1,$group}(?:(?:$sep)[$chars]{$group})*)} .
75 qq {(?:(?k:$radix)(?k:[$chars]{$places}))?))}
76 : qq {(?k:(?i)(?k:$sign)(?k:(?=$radix?[$chars])} .
77 qq {(?k:[$chars]*)(?:(?k:$radix)(?k:[$chars]{$places}))?))}
78 }
79  
80  
81 71 pattern name => [qw (num int -sep= -base=10 -group=3 -sign=[-+]?)],
82 create => \&int_creator,
83 ;
84  
85 71 pattern name => [qw (num real -base=10), '-places=0,',
86 qw (-radix=[.] -sep= -group=3 -expon=E -sign=[-+]?)],
87 create => \&real_creator,
88 ;
89  
90 71 pattern name => [qw (num decimal -base=10), '-places=0,',
91 qw (-radix=[.] -sep= -group=3 -sign=[-+]?)],
92 create => \&decimal_creator,
93 ;
94  
95 284 ▶sub real_synonym {
Subroutine real_synonym: called CC=1 SCAR=0.0
Pod undocumented
96 284 my ($name, $base) = @_;
97 pattern name => ['num', $name, '-places=0,', '-radix=[.]',
98 '-sep=', '-group=3', '-expon=E', '-sign=[-+]?'],
99 17 ▶ create => sub {my %flags = (%{$_[1]}, -base => $base);
Subroutine __ANON__: called CC=1 SCAR=0.0
100 17 real_creator (undef, \%flags);
101 }
102 284 ;
103 }
104  
105  
106 71 real_synonym (hex => 16);
107 71 real_synonym (dec => 10);
108 71 real_synonym (oct => 8);
109 71 real_synonym (bin => 2);
110  
111  
112 # 2147483647 == 2^31 - 1
113 # 9223372036854775807 == 2^63 - 1
114 pattern name => [qw (num square)],
115 326 ▶ create => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
116 71 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 326 my $sixty_four_bits = $Config {use64bitint};
118 #
119 # CPAN testers claim it fails on 5.8.8 and darwin 9.0.
120 #
121 326 ▶ my $num = $sixty_four_bits
Branch: 1 branch
BranchTrueFalseTotal
$sixty_four_bits ? : 326 0 326
122 ? '0*(?:(?:9(?:[0-1][0-9]{17}' .
123 '|2(?:[0-1][0-9]{16}' .
124 '|2(?:[0-2][0-9]{15}' .
125 '|3(?:[0-2][0-9]{14}' .
126 '|3(?:[0-6][0-9]{13}' .
127 '|7(?:[0-1][0-9]{12}' .
128 '|20(?:[0-2][0-9]{10}' .
129 '|3(?:[0-5][0-9]{9}' .
130 '|6(?:[0-7][0-9]{8}' .
131 '|8(?:[0-4][0-9]{7}' .
132 '|5(?:[0-3][0-9]{6}' .
133 '|4(?:[0-6][0-9]{5}' .
134 '|7(?:[0-6][0-9]{4}' .
135 '|7(?:[0-4][0-9]{3}' .
136 '|5(?:[0-7][0-9]{2}' .
137 '|80(?:[0-6])))))))))))))))))|[1-8]?[0-9]{0,18})'
138 : '0*(?:2(?:[0-0][0-9]{8}' .
139 '|1(?:[0-3][0-9]{7}' .
140 '|4(?:[0-6][0-9]{6}' .
141 '|7(?:[0-3][0-9]{5}' .
142 '|4(?:[0-7][0-9]{4}' .
143 '|8(?:[0-2][0-9]{3}' .
144 '|3(?:[0-5][0-9]{2}' .
145 '|6(?:[0-3][0-9]{1}' .
146 '|4[0-7])))))))))|1?[0-9]{1,9}';
147 qr {($num)(?(?{length $^N && sqrt ($^N) == int sqrt ($^N)})|(?!))}
148 326 },
149 71 ;
150  
151 71 pattern name => [qw (num roman)],
152 create => '(?xi)(?=[MDCLXVI])
153 (?k:M{0,4}
154 (?:C[DM]|D?C{0,4})?
155 (?:X[LC]|L?X{0,4})?
156 (?:I[VX]|V?I{0,4})?)'
157 ;
158  
159 71 1;
160  
161 __END__
« Common.pm ↑ Summary Common/delimited.pm »