Common/net.pm

filter: Statementstmt 100% 65 / 65 Branchbran 86.3% 19 / 22 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 2 totaltotal 88.8% 104 / 117 CC 15 SCAR 13.6
CC
15
Cov
93%
__ANON__
7.0
__ANON__
5.1
__ANON__
4.4
CRAP 3.9
« Common/balanced.pm ↑ Summary Common/_support.pm »
1 package Regexp::Common::net;
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 Regexp::Common qw /pattern clean no_defaults/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 71 our $VERSION = '2024080801';
12  
13  
14 71 my %IPunit = (
15 dec => q{(?k:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})},
16 oct => q{(?k:[0-3]?[0-7]{1,2})},
17 hex => q{(?k:[0-9a-fA-F]{1,2})},
18 bin => q{(?k:[0-1]{1,8})},
19 strict => q{(?k:2(?:5[0-5]?|[0-4][0-9]?|[6-9]?)|1[0-9]{0,2}|[3-9][0-9]?|0)},
20 );
21 71 my %MACunit = (
22 %IPunit,
23 hex => q{(?k:[0-9a-fA-F]{1,2})},
24 );
25  
26 71 my %IPv6unit = (
27 hex => q {(?k:[0-9a-f]{1,4})},
28 HEX => q {(?k:[0-9A-F]{1,4})},
29 HeX => q {(?k:[0-9a-fA-F]{1,4})},
30 );
31  
32 6 ▶sub dec {$_};
Subroutine dec: called CC=1 SCAR=0.0
Pod undocumented
33 6 ▶sub bin {oct "0b$_"}
Subroutine bin: called CC=1 SCAR=0.0
Pod undocumented
34  
35 71 my $IPdefsep = '[.]';
36 71 my $MACdefsep = ':';
37 71 my $IPv6defsep = ':';
38  
39 71 pattern name => [qw (net IPv4)],
40 create => "(?k:$IPunit{dec}$IPdefsep$IPunit{dec}$IPdefsep" .
41 "$IPunit{dec}$IPdefsep$IPunit{dec})",
42 ;
43  
44 pattern name => [qw (net MAC)],
45 create => "(?k:" . join ($MACdefsep => ($MACunit{hex}) x 6) . ")",
46 1 ▶ subs => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
47 1 ▶ $_ [1] = join ":" => map {sprintf "%02x" => hex}
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /$_[0]/ 1 0 1
48 split /$MACdefsep/ => $_ [1]
49 if $_ [1] =~ /$_[0]/
50 },
51 71 ;
52  
53 71 foreach my $type (qw /dec oct hex bin strict/) {
54 pattern name => [qw (net IPv4), $type, "-sep=$IPdefsep"],
55 2240 ▶ create => sub {my $sep = $_ [1] -> {-sep};
Subroutine __ANON__: called CC=1 SCAR=0.0
56 2240 "(?k:$IPunit{$type}$sep$IPunit{$type}$sep" .
57 "$IPunit{$type}$sep$IPunit{$type})"
58 },
59 355 ;
60  
61 pattern name => [qw (net MAC), $type, "-sep=$MACdefsep"],
62 13 ▶ create => sub {my $sep = $_ [1] -> {-sep};
Subroutine __ANON__: called CC=1 SCAR=0.0
63 13 "(?k:" . join ($sep => ($MACunit{$type}) x 6) . ")",
64 },
65 7 ▶ subs => sub {
Subroutine __ANON__: called CC=4 SCAR=14.9
66 7 ▶ return if $] < 5.006 and $type eq 'bin';
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.006" and $type eq "bin" 0 7 7
Condition33%1/3
$] < "5.006" and $type eq "bin"
!ll&&!rl&&r
700
MC/DC0%0/2
$] < "5.006" and $type eq "bin"
$] < "5.006" $type eq "bin"
Truth table33%1/3
A and B
A= $] < "5.006"B= $type eq "bin"
ABresult
0X0
100
111
67 36 $_ [1] = join ":" => map {sprintf "%02x" => eval $type}
68 $2, $3, $4, $5, $6, $7
69 if $_ [1] =~ $RE {net} {MAC} {$type}
70 {-sep => $_ [0] -> {flags} {-sep}}
71 7 ▶ {-keep};
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /$Regexp::Common::net::RE{"net"}{"MAC"}{$type}{join $;, "-sep", $_[0]{"flags"}{"-sep"}}{"-keep"}/ 6 1 7
72 },
73 355 ;
74  
75 }
76  
77  
78 71 my %cache6;
79 pattern name => [qw (net IPv6), "-sep=$IPv6defsep", "-style=HeX"],
80 49 ▶ create => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
81 49 my $style = $_ [1] {-style};
82 49 my $sep = $_ [1] {-sep};
83  
84 49 ▶ return $cache6 {$style, $sep} if $cache6 {$style, $sep};
Branch: 1 branch
BranchTrueFalseTotal
if $cache6{join $;, $style, $sep} 44 5 49
85  
86 5 my @re;
87  
88 5 ▶ die "Impossible style '$style'\n" unless exists $IPv6unit {$style};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $IPv6unit{$style} 0 5 5
89  
90 #
91 # Nothing missing
92 #
93 5 push @re => join $sep => ($IPv6unit {$style}) x 8;
94  
95 #
96 # For "double colon" representations, at least 2 units must
97 # be omitted, leaving us with at most 6 units. 0 units is also
98 # possible. Note we can have at most one double colon.
99 #
100 5 ▶ for (my $l = 0; $l <= 6; $l ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= 6) 35 5 40
101 #
102 # We prefer to do longest match, so larger $r gets priority
103 #
104 35 ▶ for (my $r = 6 - $l; $r >= 0; $r --) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r >= 0) 140 35 175
105 #
106 # $l is the number of blocks left of the double colon,
107 # $r is the number of blocks left of the double colon,
108 # $m is the number of omitted blocks
109 #
110 140 my $m = 8 - $l - $r;
111 140 ▶ my $patl = $l ? ($IPv6unit {$style} . $sep) x $l : $sep;
Branch: 1 branch
BranchTrueFalseTotal
$l ? : 105 35 140
112 140 ▶ my $patr = $r ? ($sep . $IPv6unit {$style}) x $r : $sep;
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 105 35 140
113 140 my $patm = "(?k:)" x $m;
114 140 my $pat = $patl . $patm . $patr;
115 140 push @re => "(?:$pat)";
116 }
117 }
118 5 local $" = "|";
119 5 $cache6 {$style, $sep} = qq /(?k:(?|@re))/;
120 },
121 71 ;
122  
123  
124 71 my $letter = "[A-Za-z]";
125 71 my $let_dig = "[A-Za-z0-9]";
126 71 my $let_dig_hyp = "[-A-Za-z0-9]";
127  
128 # Domain names, from RFC 1035.
129 pattern name => [qw (net domain -nospace= -rfc1101=)],
130 8 ▶ create => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
131 my $rfc1101 = exists $_ [1] {-rfc1101} &&
132 8 ▶ !defined $_ [1] {-rfc1101};
Condition66%2/3
exists $_[1]{'-rfc1101'} && !defined($_[1]{'-rfc1101'})
!ll&&!rl&&r
062
MC/DC50%1/2
exists $_[1]{'-rfc1101'} && !defined($_[1]{'-rfc1101'})
exists $_[1]{'-rfc1101'} !defined($_[1]{'-rfc1101'})
Truth table66%2/3
A && not(B)
A= exists $_[1]{'-rfc1101'}B= !defined($_[1]{'-rfc1101'})
ABresult
0X0
100
111
133  
134 8 ▶ my $lead = $rfc1101 ? "(?!$RE{net}{IPv4}(?:[.]|\$))$let_dig"
Branch: 1 branch
BranchTrueFalseTotal
$rfc1101 ? : 2 6 8
135 : $letter;
136  
137 8 ▶ if (exists $_ [1] {-nospace} && !defined $_ [1] {-nospace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[1]{'-nospace'} and not defined $_[1]{'-nospace'}) { } 2 6 8
Condition66%2/3
exists $_[1]{"-nospace"} and not defined $_[1]{"-nospace"}
!ll&&!rl&&r
062
MC/DC50%1/2
exists $_[1]{"-nospace"} and not defined $_[1]{"-nospace"}
exists $_[1]{"-nospace"} not defined $_[1]{"-nospace"}
Truth table66%2/3
A and not(B)
A= exists $_[1]{"-nospace"}B= not defined $_[1]{"-nospace"}
ABresult
0X0
100
111
138 2 return "(?k:$lead(?:(?:$let_dig_hyp){0,61}$let_dig)?" .
139 "(?:\\.$lead(?:(?:$let_dig_hyp){0,61}$let_dig)?)*)"
140 }
141 else {
142 6 return "(?k: |(?:$lead(?:(?:$let_dig_hyp){0,61}$let_dig)?" .
143 "(?:\\.$lead(?:(?:$let_dig_hyp){0,61}$let_dig)?)*))"
144 }
145 },
146 71 ;
147  
148  
149  
150 71 1;
151  
152 __END__
« Common/balanced.pm ↑ Summary Common/_support.pm »