| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$] < "5.006" and $type eq "bin"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 5 | my @re; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 5 | ▶ | die "Impossible style '$style'\n" unless exists $IPv6unit {$style}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 112 | 140 | ▶ | my $patr = $r ? ($sep . $IPv6unit {$style}) x $r : $sep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'})
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'})
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 8 | ▶ | my $lead = $rfc1101 ? "(?!$RE{net}{IPv4}(?:[.]|\$))$let_dig" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | : $letter; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 8 | ▶ | if (exists $_ [1] {-nospace} && !defined $_ [1] {-nospace}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $_[1]{"-nospace"} and not defined $_[1]{"-nospace"}
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"}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||