| 1 | package Net::CIDR::Set; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Manipulate sets of IP addresses | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use v5.14; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Carp qw( croak confess ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Net::CIDR::Set::IPv4; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Net::CIDR::Set::IPv6; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use overload '""' => 'as_string'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 9 | our $VERSION = '0.23'; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||||||
| 20 | 9 | my %type_map = ( | |||||||||||||||||||||||||||||||
| 21 | ipv4 => 'Net::CIDR::Set::IPv4', | ||||||||||||||||||||||||||||||||
| 22 | ipv6 => 'Net::CIDR::Set::IPv6', | ||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 114 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 26 | 114 | my $self = shift; | |||||||||||||||||||||||||||||||
| 27 | 114 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||
| 28 | 114 | my $set = bless { ranges => [] }, $class; | |||||||||||||||||||||||||||||||
| 29 | 114 | ▶ | my $opt = 'HASH' eq ref $_[0] ? shift : {}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 114 | ▶ | if ( defined( my $type = delete $opt->{type} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $coder_class = $type_map{$type} || $type; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$type_map{$type} || $type
MC/DC0%0/2
$type_map{$type} || $type
$type_map{$type}
$type
Truth table0%0/3
A || B
A= $type_map{$type}B= $type
| |||||||||||||||||||||||||||||||||
| 32 | 0 | $set->{coder} = $coder_class->new; | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | elsif ( ref $self ) { | ||||||||||||||||||||||||||||||||
| 35 | 16 | $set->{coder} = $self->{coder}; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | 114 | my @unk = keys %$opt; | |||||||||||||||||||||||||||||||
| 38 | 114 | ▶ | croak "Unknown options: ", _and( sort @unk ) if @unk; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 114 | ▶ | $set->add( @_ ) if @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 111 | return $set; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # Return the index of the first element >= the supplied value. If the | ||||||||||||||||||||||||||||||||
| 45 | # supplied value is larger than any element in the list the returned | ||||||||||||||||||||||||||||||||
| 46 | # value will be equal to the size of the list. | ||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | sub _find_pos { | ||||||||||||||||||||||||||||||
|
Subroutine
_find_pos: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 48 | 330 | my $self = shift; | |||||||||||||||||||||||||||||||
| 49 | 330 | my $val = shift; | |||||||||||||||||||||||||||||||
| 50 | 330 | ▶ | my $low = shift || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 330 | my $high = scalar( @{ $self->{ranges} } ); | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 330 | ▶ | while ( $low < $high ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 192 | my $mid = int( ( $low + $high ) / 2 ); | |||||||||||||||||||||||||||||||
| 56 | 192 | my $cmp = $val cmp $self->{ranges}[$mid]; | |||||||||||||||||||||||||||||||
| 57 | 192 | ▶ | if ( $cmp < 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 58 | 43 | $high = $mid; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | elsif ( $cmp > 0 ) { | ||||||||||||||||||||||||||||||||
| 61 | 97 | $low = $mid + 1; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||||||
| 64 | 52 | return $mid; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 278 | return $low; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 681 | ▶ | sub _inc { | ||||||||||||||||||||||||||||||
|
Subroutine
_inc: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 72 | 681 | my @b = reverse unpack 'C*', shift; | |||||||||||||||||||||||||||||||
| 73 | 681 | for ( @b ) { | |||||||||||||||||||||||||||||||
| 74 | 2999 | ▶ | last unless ++$_ == 256; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 2319 | $_ = 0; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 681 | return pack 'C*', reverse @b; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 158 | ▶ | sub _dec { | ||||||||||||||||||||||||||||||
|
Subroutine
_dec: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 81 | 158 | my @b = reverse unpack 'C*', shift; | |||||||||||||||||||||||||||||||
| 82 | 158 | for ( @b ) { | |||||||||||||||||||||||||||||||
| 83 | 605 | ▶ | last unless $_-- == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 448 | $_ = 255; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | 158 | return pack 'C*', reverse @b; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 97 | ▶ | sub _guess_coder { | ||||||||||||||||||||||||||||||
|
Subroutine
_guess_coder: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 90 | 97 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 91 | 97 | for my $class ( qw( Net::CIDR::Set::IPv4 Net::CIDR::Set::IPv6 ) ) { | |||||||||||||||||||||||||||||||
| 92 | 139 | my $coder = $class->new; | |||||||||||||||||||||||||||||||
| 93 | 139 | ( eval { $coder->encode( $ip ) } ); | |||||||||||||||||||||||||||||||
| 94 | 139 | ▶ | return $coder unless $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | 9 | croak "Can't decode $ip as an IPv4 or IPv6 address"; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 160 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 100 | 160 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 101 | 160 | ▶ | my $cdr = $self->{coder} ||= $self->_guess_coder( $ip ); | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"coder"} ||= $self->_guess_coder($ip)
MC/DC100%2/2
$self->{"coder"} ||= $self->_guess_coder($ip)
$self->{"coder"}
$self->_guess_coder($ip)
Truth table100%3/3
A ||= B
A= $self->{"coder"}B= $self->_guess_coder($ip)
| |||||||||||||||||||||||||||||||||
| 102 | 151 | return $cdr->encode( $ip ); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||||||
| 106 | 9 | for my $dele ( qw( _decode _nbits ) ) { | |||||||||||||||||||||||||||||||
| 107 | 9 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 108 | 18 | my $meth = $dele =~ s/^_//r; | |||||||||||||||||||||||||||||||
| 109 | 18 | ▶ | *{$dele} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 110 | 107 | my $self = shift; | |||||||||||||||||||||||||||||||
| 111 | 107 | ▶ | my $cdr = $self->{coder} || croak "Don't know how to $meth yet"; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'coder'} || croak("Don't know how to $meth yet")
MC/DC0%0/1
$self->{'coder'} || croak("Don't know how to $meth yet")
$self->{'coder'}
Truth table50%1/2
A
A= $self->{'coder'}
| |||||||||||||||||||||||||||||||||
| 112 | 107 | return $cdr->$meth( @_ ); | |||||||||||||||||||||||||||||||
| 113 | 18 | }; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 2 | ▶ | sub _conjunction { | ||||||||||||||||||||||||||||||
|
Subroutine
_conjunction: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 118 | 2 | my ( $conj, @list ) = @_; | |||||||||||||||||||||||||||||||
| 119 | 2 | my $last = pop @list; | |||||||||||||||||||||||||||||||
| 120 | 2 | return join " $conj ", join( ', ', @list ), $last; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | sub _and { _conjunction( 'and', @_ ) } | ||||||||||||||||||||||||||||||
|
Subroutine
_and: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | sub _check_and_coerce { | ||||||||||||||||||||||||||||||
|
Subroutine
_check_and_coerce: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||
| 126 | 9 | my ( $self, @others ) = @_; | |||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | my %class = map { | ||||||||||||||||||||||||||||||||
| 129 | 20 | ▶ | eval { ( defined $_ && $_->nbits || '' ) => $_ } | ||||||||||||||||||||||||||||||
|
Condition80%4/5
defined $_ && $_->nbits || ''
defined $_ and $_->nbits
MC/DC50%1/2
defined $_ && $_->nbits || ''
defined $_
$_->nbits
Truth table66%2/3
A and B
A= defined $_B= $_->nbits
| |||||||||||||||||||||||||||||||||
| 130 | 9 | } map { $_->{coder} } grep { defined } $self, @others; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 9 | my @found = sort grep $_, keys %class; | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | croak "Can't mix ", _and( @found ), " bit addresses" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | if @found > 1; | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 9 | ▶ | $self->{coder} ||= $class{ $found[0] }; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"coder"} ||= $class{$found[0]}
MC/DC0%0/2
$self->{"coder"} ||= $class{$found[0]}
$self->{"coder"}
$class{$found[0]}
Truth table66%2/3
A ||= B
A= $self->{"coder"}B= $class{$found[0]}
| |||||||||||||||||||||||||||||||||
| 138 | 9 | return $self; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 15 | ▶ | sub invert { | ||||||||||||||||||||||||||||||
|
Subroutine
invert: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 143 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 15 | my @pad = ( 0 ) x ( $self->_nbits / 8 ); | |||||||||||||||||||||||||||||||
| 146 | 15 | my ( $min, $max ) = map { pack 'C*', $_, @pad } 0, 1; | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 15 | ▶ | if ( $self->is_empty ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | 0 | $self->{ranges} = [ $min, $max ]; | |||||||||||||||||||||||||||||||
| 150 | 0 | return; | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 15 | ▶ | if ( $self->{ranges}[0] eq $min ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | 8 | shift @{ $self->{ranges} }; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||||||
| 157 | 7 | unshift @{ $self->{ranges} }, $min; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 15 | ▶ | if ( $self->{ranges}[-1] eq $max ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | 8 | pop @{ $self->{ranges} }; | |||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | else { | ||||||||||||||||||||||||||||||||
| 164 | 7 | push @{ $self->{ranges} }, $max; | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 16 | ▶ | sub copy { | ||||||||||||||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 170 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||
| 171 | 16 | my $copy = $self->new; | |||||||||||||||||||||||||||||||
| 172 | 16 | @{ $copy->{ranges} } = @{ $self->{ranges} }; | |||||||||||||||||||||||||||||||
| 173 | 16 | return $copy; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 165 | ▶ | sub _add_range { | ||||||||||||||||||||||||||||||
|
Subroutine
_add_range: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 177 | 165 | my ( $self, $from, $to ) = @_; | |||||||||||||||||||||||||||||||
| 178 | 165 | my $fpos = $self->_find_pos( $from ); | |||||||||||||||||||||||||||||||
| 179 | 165 | my $tpos = $self->_find_pos( _inc( $to ), $fpos ); | |||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 165 | ▶ | $from = $self->{ranges}[ --$fpos ] if ( $fpos & 1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 182 | 165 | ▶ | $to = $self->{ranges}[ $tpos++ ] if ( $tpos & 1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 165 | splice @{ $self->{ranges} }, $fpos, $tpos - $fpos, ( $from, $to ); | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 100 | ▶ | sub add { | ||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 189 | 100 | my ( $self, @addr ) = @_; | |||||||||||||||||||||||||||||||
| 190 | 100 | for my $ip ( map { split /\s*,\s*/ } @addr ) { | |||||||||||||||||||||||||||||||
| 191 | 160 | ▶ | my ( $lo, $hi ) = $self->_encode( $ip ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 192 | or croak "Can't decode $ip"; | ||||||||||||||||||||||||||||||||
| 193 | 149 | $self->_add_range( $lo, $hi ); | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 199 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 1 | $self->invert; | |||||||||||||||||||||||||||||||
| 202 | 1 | $self->add( @_ ); | |||||||||||||||||||||||||||||||
| 203 | 1 | $self->invert; | |||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 9 | ▶ | sub merge { | ||||||||||||||||||||||||||||||
|
Subroutine
merge: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 208 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||
| 209 | 9 | $self->_check_and_coerce( @_ ); | |||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | # TODO: This isn't very efficient - and merge gets called from all | ||||||||||||||||||||||||||||||||
| 212 | # sorts of other places. | ||||||||||||||||||||||||||||||||
| 213 | 9 | for my $other ( @_ ) { | |||||||||||||||||||||||||||||||
| 214 | 11 | my $iter = $other->_iterate_runs; | |||||||||||||||||||||||||||||||
| 215 | 11 | ▶ | while ( my ( $from, $to ) = $iter->() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 216 | 16 | $self->_add_range( $from, $to ); | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 9 | *contains = *contains_all; | |||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 1 | ▶ | sub contains_all { | ||||||||||||||||||||||||||||||
|
Subroutine
contains_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 225 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 226 | 1 | my $class = ref $self; | |||||||||||||||||||||||||||||||
| 227 | 1 | return $class->new( @_ )->subset( $self ); | |||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | sub contains_any { | ||||||||||||||||||||||||||||||
|
Subroutine
contains_any: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 232 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 233 | 0 | my $class = ref $self; | |||||||||||||||||||||||||||||||
| 234 | 0 | return !$class->new( @_ )->intersection( $self )->is_empty; | |||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | 62 | ▶ | sub _iterate_runs { | ||||||||||||||||||||||||||||||
|
Subroutine
_iterate_runs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 238 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | 62 | my $pos = 0; | |||||||||||||||||||||||||||||||
| 241 | 62 | my $limit = scalar( @{ $self->{ranges} } ); | |||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 147 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 244 | 147 | ▶ | return if $pos >= $limit; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 245 | 85 | my @r = @{ $self->{ranges} }[ $pos, $pos + 1 ]; | |||||||||||||||||||||||||||||||
| 246 | 85 | $pos += 2; | |||||||||||||||||||||||||||||||
| 247 | 85 | return @r; | |||||||||||||||||||||||||||||||
| 248 | 62 | }; | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub compliment { | ||||||||||||||||||||||||||||||
|
Subroutine
compliment: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 252 | 0 | croak "That's very kind of you - but I expect you meant complement"; | |||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 10 | ▶ | sub complement { | ||||||||||||||||||||||||||||||
|
Subroutine
complement: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 257 | 10 | my $new = shift->copy; | |||||||||||||||||||||||||||||||
| 258 | # TODO: What if it's empty? | ||||||||||||||||||||||||||||||||
| 259 | 10 | $new->invert; | |||||||||||||||||||||||||||||||
| 260 | 10 | return $new; | |||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 6 | ▶ | sub union { | ||||||||||||||||||||||||||||||
|
Subroutine
union: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 265 | 6 | my $new = shift->copy; | |||||||||||||||||||||||||||||||
| 266 | 6 | $new->merge( @_ ); | |||||||||||||||||||||||||||||||
| 267 | 6 | return $new; | |||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | 3 | ▶ | sub intersection { | ||||||||||||||||||||||||||||||
|
Subroutine
intersection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 272 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 273 | 3 | my $class = ref $self; | |||||||||||||||||||||||||||||||
| 274 | 3 | my $new = $class->new; | |||||||||||||||||||||||||||||||
| 275 | 3 | $new->merge( map { $_->complement } $self, @_ ); | |||||||||||||||||||||||||||||||
| 276 | 3 | $new->invert; | |||||||||||||||||||||||||||||||
| 277 | 3 | return $new; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | sub xor { | ||||||||||||||||||||||||||||||
|
Subroutine
xor: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 282 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 283 | 0 | return $self->union( @_ ) | |||||||||||||||||||||||||||||||
| 284 | ->intersection( $self->intersection( @_ )->complement ); | ||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | sub diff { | ||||||||||||||||||||||||||||||
|
Subroutine
diff: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 289 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 290 | 0 | my $other = shift; | |||||||||||||||||||||||||||||||
| 291 | 0 | return $self->intersection( $other->union( @_ )->complement ); | |||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 16 | ▶ | sub is_empty { | ||||||||||||||||||||||||||||||
|
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 296 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||
| 297 | 16 | return @{ $self->{ranges} } == 0; | |||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub superset { | ||||||||||||||||||||||||||||||
|
Subroutine
superset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 302 | 0 | my $other = pop; | |||||||||||||||||||||||||||||||
| 303 | 0 | return $other->subset( reverse( @_ ) ); | |||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | sub subset { | ||||||||||||||||||||||||||||||
|
Subroutine
subset: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 308 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | my $other = shift || croak "I need two sets to compare"; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || croak('I need two sets to compare')
MC/DC0%0/1
shift() || croak('I need two sets to compare')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 310 | 0 | return $self->equals( $self->intersection( $other ) ); | |||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||
| 314 | 36 | ▶ | sub equals { | ||||||||||||||||||||||||||||||
|
Subroutine
equals: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 315 | 36 | ▶ | return unless @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | # Array of array refs | ||||||||||||||||||||||||||||||||
| 318 | 36 | my @edges = map { $_->{ranges} } @_; | |||||||||||||||||||||||||||||||
| 319 | 36 | my $medge = scalar( @edges ) - 1; | |||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | 36 | POS: for ( my $pos = 0;; $pos++ ) { | |||||||||||||||||||||||||||||||
| 322 | 114 | my $v = $edges[0]->[$pos]; | |||||||||||||||||||||||||||||||
| 323 | 114 | ▶ | if ( defined( $v ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 324 | 78 | for ( @edges[ 1 .. $medge ] ) { | |||||||||||||||||||||||||||||||
| 325 | 78 | my $vv = $_->[$pos]; | |||||||||||||||||||||||||||||||
| 326 | 78 | ▶ | return unless defined( $vv ) && $vv eq $v; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $vv and $vv eq $v
MC/DC0%0/2
defined $vv and $vv eq $v
defined $vv
$vv eq $v
Truth table33%1/3
A and B
A= defined $vvB= $vv eq $v
| |||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | else { | ||||||||||||||||||||||||||||||||
| 330 | 36 | for ( @edges[ 1 .. $medge ] ) { | |||||||||||||||||||||||||||||||
| 331 | 36 | ▶ | return if defined $_->[$pos]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 114 | ▶ | last POS unless defined( $v ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | 36 | return 1; | |||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 1 | ▶ | sub iterate_addresses { | ||||||||||||||||||||||||||||||
|
Subroutine
iterate_addresses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 343 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||
| 344 | 1 | my $iter = $self->_iterate_runs; | |||||||||||||||||||||||||||||||
| 345 | 1 | my @r = (); | |||||||||||||||||||||||||||||||
| 346 | 2 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 347 | 2 | while ( 1 ) { | |||||||||||||||||||||||||||||||
| 348 | 3 | ▶ | @r = $iter->() or return unless @r; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
@r || ((@r = &$iter()) || (return))
@r = &$iter() or return
MC/DC0%0/2
@r || ((@r = &$iter()) || (return))
@r
@r = &$iter()
Truth table66%2/3
A || B
A= @rB= @r = &$iter()
| |||||||||||||||||||||||||||||||||
| 349 | 2 | ▶ | return $self->_decode( ( my $last, $r[0] ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 350 | = ( $r[0], _inc( $r[0] ) ), @args ) | ||||||||||||||||||||||||||||||||
| 351 | unless $r[0] eq $r[1]; | ||||||||||||||||||||||||||||||||
| 352 | 1 | @r = (); | |||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||
| 354 | 1 | }; | |||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 8 | ▶ | sub iterate_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
iterate_cidr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 359 | 8 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||
| 360 | 8 | my $iter = $self->_iterate_runs; | |||||||||||||||||||||||||||||||
| 361 | 8 | my $size = $self->_nbits; | |||||||||||||||||||||||||||||||
| 362 | 8 | my @r = (); | |||||||||||||||||||||||||||||||
| 363 | 39 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 364 | 39 | while ( 1 ) { | |||||||||||||||||||||||||||||||
| 365 | 55 | ▶ | @r = $iter->() or return unless @r; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
@r || ((@r = &$iter()) || (return))
@r = &$iter() or return
MC/DC0%0/2
@r || ((@r = &$iter()) || (return))
@r
@r = &$iter()
Truth table66%2/3
A || B
A= @rB= @r = &$iter()
| |||||||||||||||||||||||||||||||||
| 366 | 47 | ▶ | unless ( $r[0] eq $r[1] ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 367 | 31 | ( my $bits = unpack 'B*', $r[0] ) =~ /(0*)$/; | |||||||||||||||||||||||||||||||
| 368 | 31 | my $pad = length $1; | |||||||||||||||||||||||||||||||
| 369 | 31 | ▶ | $pad = $size if $pad > $size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 370 | 31 | while ( 1 ) { | |||||||||||||||||||||||||||||||
| 371 | 205 | my $next = _inc( $r[0] | pack 'B*', | |||||||||||||||||||||||||||||||
| 372 | ( '0' x ( length( $bits ) - $pad ) ) . ( '1' x $pad ) ); | ||||||||||||||||||||||||||||||||
| 373 | 205 | ▶ | return $self->_decode( ( my $last, $r[0] ) = ( $r[0], $next ), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 374 | @args ) | ||||||||||||||||||||||||||||||||
| 375 | if $next le $r[1]; | ||||||||||||||||||||||||||||||||
| 376 | 174 | $pad--; | |||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||
| 379 | 16 | @r = (); | |||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | 8 | }; | |||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | 42 | ▶ | sub iterate_ranges { | ||||||||||||||||||||||||||||||
|
Subroutine
iterate_ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 386 | 42 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||
| 387 | 42 | my $iter = $self->_iterate_runs; | |||||||||||||||||||||||||||||||
| 388 | 94 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 389 | 94 | ▶ | return unless my @r = $iter->(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 390 | 52 | return $self->_decode( @r, @args ); | |||||||||||||||||||||||||||||||
| 391 | 42 | }; | |||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | 51 | ▶ | sub as_array { | ||||||||||||||||||||||||||||||
|
Subroutine
as_array: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 396 | 51 | my ( $self, $iter ) = @_; | |||||||||||||||||||||||||||||||
| 397 | 51 | my @addr = (); | |||||||||||||||||||||||||||||||
| 398 | 51 | ▶ | while ( my $addr = $iter->() ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 399 | 84 | push @addr, $addr; | |||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||
| 401 | 51 | return @addr; | |||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||
| 405 | 1 | ▶ | sub as_address_array { | ||||||||||||||||||||||||||||||
|
Subroutine
as_address_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 406 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 407 | 1 | return $self->as_array( $self->iterate_addresses( @_ ) ); | |||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | 8 | ▶ | sub as_cidr_array { | ||||||||||||||||||||||||||||||
|
Subroutine
as_cidr_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 412 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||
| 413 | 8 | return $self->as_array( $self->iterate_cidr( @_ ) ); | |||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | 42 | ▶ | sub as_range_array { | ||||||||||||||||||||||||||||||
|
Subroutine
as_range_array: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 418 | 42 | my $self = shift; | |||||||||||||||||||||||||||||||
| 419 | 42 | return $self->as_array( $self->iterate_ranges( @_ ) ); | |||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | 36 | ▶ | sub as_string { join ', ', shift->as_range_array( @_ ) } | ||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | 9 | 1; | |||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | __END__ | ||||||||||||||||||||||||||||||||