lib/Net/IP/RangeCompare.pm

filter: Statementstmt 98.4% 322 / 327 Branchbran 81.5% 124 / 152 Conditioncond 86.6% 13 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 97.1% 68 / 70 Podpod 90.2% 37 / 41 totaltotal 92.8% 571 / 615 CC 82 SCAR 13.5
CC
82
Cov
93%
parse_new_range
9.5
overlap
9.0
_internal_ip_list_func
8.0
CRAP 3.8
↑ Summary
1  
2 package Net::IP::RangeCompare;
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Scalar::Util qw(looks_like_number blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use vars qw($error $VERSION @ISA @EXPORT_OK %EXPORT_TAGS %HELPER);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Data::Range::Compare qw(HELPER_CB
Subroutine BEGIN: called CC=1 SCAR=0.0
10 key_helper
11 key_start
12 key_end
13 key_generated
14 key_missing
15 key_data
16 add_one
17 sub_one
18 cmp_values
19 sort_largest_range_end_first
20 sort_largest_range_start_first
21 sort_smallest_range_start_first
22 sort_smallest_range_end_first
23 sort_in_consolidate_order
24 sort_in_presentation_order
25 12 );
26 12 ▶use Data::IPV4::Range::Parse qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 ALL_BITS
29 MAX_CIDR
30 MIN_CIDR
31 int_to_ip
32 ip_to_int
33 sort_quad
34 sort_notations
35 broadcast_int
36 base_int
37 size_from_mask
38 hostmask
39 cidr_to_int
40 parse_ipv4_cidr
41 parse_ipv4_range
42 parse_ipv4_ip
43 auto_parse_ipv4_range
44  
45 12 );
46  
47 12 ▶use constant key_start_ip =>key_start;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 12 ▶use constant key_end_ip =>key_end;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 12 %HELPER=HELPER_CB;
51  
52 12 $VERSION=4.025;
53 use overload
54 12 ▶ '""' => \&notation
Subroutine BEGIN: called CC=1 SCAR=0.0
55 12 ,'fallback' => 1;
56  
57  
58 12 require Exporter;
59 12 @ISA=qw(Exporter Data::Range::Compare );
60  
61 12 @EXPORT_OK=qw(
62 hostmask
63 cidr_to_int
64 ip_to_int
65 int_to_ip
66 size_from_mask
67 base_int
68 broadcast_int
69 cmp_int
70 sort_quad
71 sort_notations
72 add_one
73 sub_one
74  
75 sort_ranges
76 sort_largest_first_int_first
77 sort_smallest_last_int_first
78 sort_largest_last_int_first
79 sort_smallest_first_int_first
80  
81 get_overlapping_range
82 get_common_range
83 grep_overlap
84 grep_non_overlap
85  
86 consolidate_ranges
87 range_start_end_fill
88 fill_missing_ranges
89 range_compare
90 compare_row
91 range_compare_force_cidr
92 );
93  
94 12 %EXPORT_TAGS = (
95 ALL=>\@EXPORT_OK
96 ,HELPER=>[qw(
97 hostmask
98 cidr_to_int
99 ip_to_int
100 int_to_ip
101 sort_quad
102 size_from_mask
103 base_int
104 broadcast_int
105 cmp_int
106 sort_notations
107 add_one
108 sub_one
109 )]
110 ,SORT=>[qw(
111 sort_ranges
112 sort_largest_first_int_first
113 sort_smallest_last_int_first
114 sort_largest_last_int_first
115 sort_smallest_first_int_first
116 )]
117 ,OVERLAP=>[qw(
118 get_overlapping_range
119 get_common_range
120 grep_overlap
121 grep_non_overlap
122 )]
123 ,PROCESS=>[qw(
124 consolidate_ranges
125 range_start_end_fill
126 fill_missing_ranges
127 range_compare
128 compare_row
129 range_compare_force_cidr
130 )]
131 );
132  
133 ## compatibilty stuffs
134 12 *sort_ranges=\&sort_in_consolidate_order;
135 12 *sort_largest_first_int_first=\&sort_largest_range_start_first;
136 12 *sort_smallest_last_int_first=\&sort_smallest_range_end_first;
137 12 *sort_largest_last_int_first =\&sort_largest_range_end_first;
138 12 *sort_smallest_first_int_first=\&sort_smallest_range_start_first;
139  
140 228 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
141 228 my ($s,$start,$end,@args)=@_;
142 228 ▶ return undef unless looks_like_number $start;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($start) 1 227 228
143 227 ▶ return undef unless looks_like_number $end;
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($end) 1 226 227
144 226 ▶ return undef if $start>$end;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $end 3 223 226
145 223 $s->SUPER::new(\%HELPER,$start,$end,@args);
146 }
147  
148 88 ▶sub parse_new_range {
Subroutine parse_new_range: called CC=9 SCAR=22.5
Pod documented
149 88 my ($s,@args)=@_;
150 88 my $string;
151 88 ▶ if($#args >0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#args > 0) { } 1 87 88
152 1 $string=join ' - ',@args;
153 } else {
154 87 ▶ return undef unless defined($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args[0] 1 86 87
155 86 $string=$args[0];
156 86 my $class=blessed($string);
157 86 ▶ if(ref($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 47 39 86
158 47 ▶ return $string if $class and $class eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $class and $class eq "Net::IP::RangeCompare" 46 1 47
Condition66%2/3
$class and $class eq "Net::IP::RangeCompare"
!ll&&!rl&&r
1046
MC/DC50%1/2
$class and $class eq "Net::IP::RangeCompare"
$class $class eq "Net::IP::RangeCompare"
Truth table66%2/3
A and B
A= $classB= $class eq "Net::IP::RangeCompare"
ABresult
0X0
100
111
159 1 ▶ if($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 1 1
160 0 $string .='';
161 0 ▶ return undef unless $string=~ m/
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ m[ ^ \d(\.\d{1,3}){0,3} \s*[/-]\s* \d(\.\d{1,3}){0,3} $ ]x 0 0 0
162 ^
163 \d(\.\d{1,3}){0,3}
164 \s*[\/-]\s*
165 \d(\.\d{1,3}){0,3}
166 $
167 /x;
168 } else {
169 1 return undef;
170 }
171 }
172 }
173 40 my ($start,$end)=auto_parse_ipv4_range($string);
174 40 ▶ return undef unless looks_like_number($end);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($end) 1 39 40
175 39 $s->new($start,$end);
176 }
177 12 *new_from_ip=\&parse_new_range;
178 12 *new_from_range=\&parse_new_range;
179 12 *new_from_cidr=\&parse_new_range;
180  
181 ##########################################################
182 #
183 # OO Stubs
184  
185 2076 ▶sub first_int () { $_[0]->[key_start_ip] }
Subroutine first_int: called CC=1 SCAR=0.0
Pod documented
186 1128 ▶sub last_int () { $_[0]->[key_end_ip] }
Subroutine last_int: called CC=1 SCAR=0.0
Pod documented
187 6 ▶sub first_ip () { int_to_ip($_[0]->[key_start_ip]) }
Subroutine first_ip: called CC=1 SCAR=0.0
Pod documented
188 6 ▶sub last_ip () { int_to_ip($_[0]->[key_end_ip]) }
Subroutine last_ip: called CC=1 SCAR=0.0
Pod documented
189 157 ▶sub missing () {$_[0]->[key_missing] }
Subroutine missing: called CC=1 SCAR=0.0
Pod undocumented
190 2 ▶sub generated () {$_[0]->[key_generated] }
Subroutine generated: called CC=1 SCAR=0.0
Pod undocumented
191 0 ▶sub error () { $error }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
192 4 ▶sub size () { 1 + $_[0]->last_int - $_[0]->first_int }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
193  
194 2 ▶sub data () {
Subroutine data: called CC=2 SCAR=6.9
Pod documented
195 2 my ($s)=@_;
196 # always return the data ref if it exists
197 2 ▶ return $s->[key_data] if ref($s->[key_data]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $s->[5] 1 1 2
198 1 $s->[key_data]={};
199 1 $s->[key_data]
200 }
201  
202 266 ▶sub notation {
Subroutine notation: called CC=1 SCAR=0.0
Pod documented
203 266 join ' - '
204 ,int_to_ip($_[0]->first_int)
205 ,int_to_ip($_[0]->last_int)
206 }
207  
208 2 ▶sub get_cidr_notation () {
Subroutine get_cidr_notation: called CC=2 SCAR=6.9
Pod documented
209 2 my ($s)=@_;
210 2 my $n=$s;
211 2 my $return_ref=[];
212 2 my ($range,$cidr);
213 2 ▶ while($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 3 2 5
214 3 ($range,$cidr,$n)=$n->get_first_cidr;
215 3 push @$return_ref,$cidr;
216 }
217 2 join(', ',@$return_ref);
218 }
219  
220 45 ▶sub overlap ($) {
Subroutine overlap: called CC=9 SCAR=22.0
Pod documented
221 45 my ($range_a,$range_b)=@_;
222 45 my $class=blessed $range_a;
223 45 $range_b=$class->parse_new_range($range_b);
224  
225 # return true if range_b's start range is contained by range_a
226 45 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $range_a->cmp_first_int($range_b) != 1 and $range_a->cmp_last_int($range_b) != -1 9 36 45
Condition100%3/3
$range_a->cmp_first_int($range_b) != 1 and $range_a->cmp_last_int($range_b) != -1
!ll&&!rl&&r
6309
MC/DC100%2/2
$range_a->cmp_first_int($range_b) != 1 and $range_a->cmp_last_int($range_b) != -1
$range_a->cmp_first_int($range_b) != 1 $range_a->cmp_last_int($range_b) != -1
Truth table100%3/3
A and B
A= $range_a->cmp_first_int($range_b) != 1B= $range_a->cmp_last_int($range_b) != -1
ABresult
0X0
100
111
227 $range_a->cmp_first_int($range_b)!=1
228 &&
229 $range_a->cmp_last_int($range_b)!=-1;
230  
231 # return true if range_b's end range is contained by range_a
232 36 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if cmp_int($range_a->first_int, $range_b->last_int) != 1 and cmp_int($range_a->last_int, $range_b->last_int) != -1 0 36 36
Condition66%2/3
cmp_int($range_a->first_int, $range_b->last_int) != 1 and cmp_int($range_a->last_int, $range_b->last_int) != -1
!ll&&!rl&&r
4320
MC/DC0%0/2
cmp_int($range_a->first_int, $range_b->last_int) != 1 and cmp_int($range_a->last_int, $range_b->last_int) != -1
cmp_int($range_a->first_int, $range_b->last_int) != 1 cmp_int($range_a->last_int, $range_b->last_int) != -1
Truth table66%2/3
A and B
A= cmp_int($range_a->first_int, $range_b->last_int) != 1B= cmp_int($range_a->last_int, $range_b->last_int) != -1
ABresult
0X0
100
111
233 #$range_a->first_int <=$range_b->last_int
234 cmp_int($range_a->first_int,$range_b->last_int )!=1
235 &&
236 #$range_a->last_int >=$range_b->last_int;
237 cmp_int($range_a->last_int,$range_b->last_int)!=-1;
238  
239 36 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $range_b->cmp_first_int($range_a) != 1 and $range_b->cmp_last_int($range_a) != -1 2 34 36
Condition100%3/3
$range_b->cmp_first_int($range_a) != 1 and $range_b->cmp_last_int($range_a) != -1
!ll&&!rl&&r
3042
MC/DC100%2/2
$range_b->cmp_first_int($range_a) != 1 and $range_b->cmp_last_int($range_a) != -1
$range_b->cmp_first_int($range_a) != 1 $range_b->cmp_last_int($range_a) != -1
Truth table100%3/3
A and B
A= $range_b->cmp_first_int($range_a) != 1B= $range_b->cmp_last_int($range_a) != -1
ABresult
0X0
100
111
240 #$range_b->first_int <=$range_a->first_int
241 $range_b->cmp_first_int($range_a)!=1
242 &&
243 #$range_b->last_int >=$range_a->first_int;
244 $range_b->cmp_last_int($range_a)!=-1;
245  
246 # return true if range_b's end range is contained by range_a
247 34 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if cmp_int($range_b->first_int, $range_a->last_int) != 1 and cmp_int($range_b->last_int, $range_a->last_int) != -1 4 30 34
Condition100%3/3
cmp_int($range_b->first_int, $range_a->last_int) != 1 and cmp_int($range_b->last_int, $range_a->last_int) != -1
!ll&&!rl&&r
2644
MC/DC100%2/2
cmp_int($range_b->first_int, $range_a->last_int) != 1 and cmp_int($range_b->last_int, $range_a->last_int) != -1
cmp_int($range_b->first_int, $range_a->last_int) != 1 cmp_int($range_b->last_int, $range_a->last_int) != -1
Truth table100%3/3
A and B
A= cmp_int($range_b->first_int, $range_a->last_int) != 1B= cmp_int($range_b->last_int, $range_a->last_int) != -1
ABresult
0X0
100
111
248 #$range_b->first_int <=$range_a->last_int
249 cmp_int($range_b->first_int,$range_a->last_int )!=1
250 &&
251 #$range_b->last_int >=$range_a->last_int;
252 cmp_int($range_b->last_int,$range_a->last_int)!=-1;
253  
254 # return undef by default
255 undef
256 30 }
257  
258 26 ▶sub next_first_int () { add_one($_[0]->last_int) }
Subroutine next_first_int: called CC=1 SCAR=0.0
Pod documented
259 1 ▶sub previous_last_int () { sub_one($_[0]->first_int) }
Subroutine previous_last_int: called CC=1 SCAR=0.0
Pod documented
260 32 ▶sub get_first_cidr () {
Subroutine get_first_cidr: called CC=5 SCAR=16.1
Pod documented
261 32 my ($s)=@_;
262 32 my $class=blessed $s;
263 32 my $first_cidr;
264 my $output_cidr;
265 32 ▶ for(my $cidr=MAX_CIDR;$cidr>-1;--$cidr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr > -1) 1017 0 1017
266 1017 $output_cidr=MAX_CIDR - $cidr;
267 1017 my $mask=cidr_to_int($output_cidr);
268  
269 1017 my $hostmask=hostmask($mask);
270 1017 my $size=size_from_mask($mask);
271  
272 1017 ▶ next if $s->mod_first_int($size);
Branch: 1 branch
BranchTrueFalseTotal
if $s->mod_first_int($size) 585 432 1017
273  
274  
275 432 my $last_int=$s->first_int + $hostmask;
276 432 ▶ next if cmp_int($last_int,$s->last_int)==1;
Branch: 1 branch
BranchTrueFalseTotal
if cmp_int($last_int, $s->last_int) == 1 400 32 432
277  
278 32 $first_cidr=$class->new($s->first_int,$last_int);
279  
280 32 last;
281 }
282 32 my $cidr_string=join('/',int_to_ip($first_cidr->first_int),$output_cidr);
283  
284 32 ▶ if($first_cidr->cmp_last_int($s)==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_cidr->cmp_last_int($s) == 0) { } 16 16 32
285 16 return ( $first_cidr ,$cidr_string);
286 } else {
287 return (
288 16 $first_cidr
289 ,$cidr_string
290 ,$class->new(
291 $first_cidr->next_first_int
292 ,$s->last_int
293 )
294 );
295 }
296  
297 }
298  
299 1 ▶sub is_cidr () {
Subroutine is_cidr: called CC=2 SCAR=7.0
Pod undocumented
300 1 my ($s)=@_;
301 1 my ($range,$cidr,$next)=$s->get_first_cidr;
302 1 ▶ my $is_cidr=defined($next) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 1 0 1
303 1 $is_cidr
304 }
305  
306 1 ▶sub is_range () {
Subroutine is_range: called CC=2 SCAR=7.0
Pod undocumented
307 1 my ($s)=@_;
308 1 my ($range,$cidr,$next)=$s->get_first_cidr;
309 1 ▶ my $is_range=defined($next) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 1 0 1
310 1 $is_range
311 }
312  
313 3 ▶sub nth ($) {
Subroutine nth: called CC=2 SCAR=6.9
Pod documented
314 3 my ($s,$offset)=@_;
315 3 my $int=$s->first_int + $offset;
316 3 ▶ return undef if cmp_int($int,$s->last_int)==1;
Branch: 1 branch
BranchTrueFalseTotal
if cmp_int($int, $s->last_int) == 1 1 2 3
317 2 int_to_ip($int);
318 }
319  
320 6 ▶sub _internal_ip_list_func ($) {
Subroutine _internal_ip_list_func: called CC=8 SCAR=20.8
321 6 my ($s,$mode)=@_;
322 6 my $next=$s;
323 6 my @list;
324 my $ip;
325 6 my $cidr;
326 6 ▶ while($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 15 6 21
327 15 ($ip,$cidr,$next)=$next->get_first_cidr;
328 15 ▶ if($mode eq 'first_int') {
Branch: 6 branches
BranchTrueFalseTotal
if ($mode eq 'first_int') { } 3 12 15
elsif ($mode eq 'first_ip') { } 3 9 12
elsif ($mode eq 'last_ip') { } 3 6 9
elsif ($mode eq 'last_int') { } 3 3 6
elsif ($mode eq 'netmask_int') { } 1 2 3
elsif ($mode eq 'netmask') { } 2 0 2
329 3 push @list,$ip->first_int;
330 } elsif($mode eq 'first_ip') {
331 3 push @list,$ip->first_ip;
332 } elsif($mode eq 'last_ip') {
333 3 push @list,$ip->last_ip;
334 } elsif($mode eq 'last_int') {
335 3 push @list,$ip->last_int;
336 } elsif($mode eq 'netmask_int') {
337 1 my ($cidr_int)=($cidr=~ /(\d+)$/);
338 1 push @list,cidr_to_int($cidr_int);
339 } elsif($mode eq 'netmask') {
340 2 my ($cidr_int)=($cidr=~ /(\d+)$/);
341 2 push @list,int_to_ip(cidr_to_int($cidr_int));
342 }
343 }
344 6 @list;
345 }
346  
347 1 ▶sub netmask_int_list { $_[0]->_internal_ip_list_func('netmask_int') }
Subroutine netmask_int_list: called CC=1 SCAR=0.0
Pod documented
348 1 ▶sub netmask_list { $_[0]->_internal_ip_list_func('netmask') }
Subroutine netmask_list: called CC=1 SCAR=0.0
Pod documented
349 1 ▶sub base_list_int () { $_[0]->_internal_ip_list_func('first_int') }
Subroutine base_list_int: called CC=1 SCAR=0.0
Pod documented
350 1 ▶sub base_list_ip () { $_[0]->_internal_ip_list_func('first_ip') }
Subroutine base_list_ip: called CC=1 SCAR=0.0
Pod documented
351 1 ▶sub broadcast_list_int () { $_[0]->_internal_ip_list_func('last_int') }
Subroutine broadcast_list_int: called CC=1 SCAR=0.0
Pod documented
352 1 ▶sub broadcast_list_ip () { $_[0]->_internal_ip_list_func('last_ip') }
Subroutine broadcast_list_ip: called CC=1 SCAR=0.0
Pod documented
353  
354 3 ▶sub enumerate {
Subroutine enumerate: called CC=2 SCAR=6.9
Pod documented
355 3 my ($s,$cidr)=@_;
356 3 ▶ $cidr=MAX_CIDR unless $cidr;
Branch: 1 branch
BranchTrueFalseTotal
unless $cidr 1 2 3
357 3 my $mask=cidr_to_int($cidr);
358 3 my $hostmask=hostmask($mask);
359 3 my $n=$s;
360 3 my $class=blessed $s;
361 9 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
362 9 ▶ return undef unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 3 6 9
363 #my $cidr_end=($n->first_int & $mask) + $hostmask;
364 6 my $cidr_end=broadcast_int($n->first_int , $mask);
365 6 my $return_ref;
366 6 ▶ if(cmp_int($cidr_end,$n->last_int)!=-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp_int($cidr_end, $n->last_int) != -1) { } 3 3 6
367 3 $return_ref=$n;
368 3 $n=undef;
369 } else {
370 3 $return_ref=$class->new(
371 $n->first_int
372 ,$cidr_end
373 );
374 3 $n=$class->new(
375 $return_ref->next_first_int
376 ,$n->last_int
377 );
378 }
379 6 $return_ref;
380 }
381 3 }
382  
383 2 ▶sub enumerate_size {
Subroutine enumerate_size: called CC=2 SCAR=6.9
Pod documented
384 2 my ($s,$inc)=@_;
385 2 my $class=blessed $s;
386 2 ▶ $inc=1 unless $inc;
Branch: 1 branch
BranchTrueFalseTotal
unless $inc 1 1 2
387 2 my $done;
388 8 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
389 8 ▶ return undef if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 2 6 8
390 6 my $first=$s->first_int;
391 6 my $next=$first + $inc;
392 6 my $last;
393 6 ▶ if(cmp_int($s->last_int,$next)!=-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp_int($s->last_int, $next) != -1) { } 4 2 6
394 4 $last=$next;
395 } else {
396 2 $last=$s->last_int;
397 }
398 6 my $new_range=$class->new($first,$last);
399 6 ▶ $done=1 if $s->cmp_last_int($new_range)==0;
Branch: 1 branch
BranchTrueFalseTotal
if $s->cmp_last_int($new_range) == 0 2 4 6
400 6 $s=$class->new($new_range->next_first_int,$s->last_int);
401 6 $new_range;
402 }
403 2 }
404  
405 84 ▶sub cmp_first_int($) {
Subroutine cmp_first_int: called CC=1 SCAR=0.0
Pod documented
406 84 my ($s,$cmp)=@_;
407 84 cmp_int($s->first_int,$cmp->first_int)
408 }
409  
410 86 ▶sub cmp_last_int($) {
Subroutine cmp_last_int: called CC=1 SCAR=0.0
Pod documented
411 86 my ($s,$cmp)=@_;
412 86 cmp_int($s->last_int,$cmp->last_int)
413 }
414  
415 1017 ▶sub mod_first_int ($) { $_[0]->first_int % $_[1] }
Subroutine mod_first_int: called CC=1 SCAR=0.0
Pod documented
416  
417 12 *cmp_int=\&cmp_values;
418  
419 30 ▶sub get_common_range {
Subroutine get_common_range: called CC=5 SCAR=16.5
Pod documented
420 30 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 0 30 30
421 30 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 0 30 30
422 30 my $ranges=shift;
423 30 ▶ croak 'empty range reference' if $#$ranges==-1;
Branch: 1 branch
BranchTrueFalseTotal
if $ranges->$#* == -1 0 30 30
424 30 my $range=Net::IP::RangeCompare->SUPER::get_common_range(\%HELPER,$ranges);
425 30 ▶ return undef if cmp_values($range->first_int,$range->last_int)==1;
Branch: 1 branch
BranchTrueFalseTotal
if cmp_values($range->first_int, $range->last_int) == 1 1 29 30
426 29 $range;
427 }
428  
429 1 ▶sub grep_non_overlap {
Subroutine grep_non_overlap: called CC=3 SCAR=11.1
Pod documented
430 1 my ($range,$list)=@_;
431 1 my $result=[];
432 1 my $cmp=Net::IP::RangeCompare->parse_new_range($range);
433 1 ▶ return $result unless defined($cmp);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmp 0 1 1
434 @$result=(grep {
435 1 my $range=Net::IP::RangeCompare->parse_new_range($_);
436 3 ▶ defined($range) ? !$cmp->overlap($range) : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $range ? : 3 0 3
437 } @$list);
438 1 $result;
439 }
440 2 ▶sub grep_overlap {
Subroutine grep_overlap: called CC=3 SCAR=11.1
Pod documented
441 2 my ($range,$list)=@_;
442 2 my $result=[];
443 2 my $cmp=Net::IP::RangeCompare->parse_new_range($range);
444 2 ▶ return $result unless defined($cmp);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmp 0 2 2
445 @$result=(grep {
446 2 my $range=Net::IP::RangeCompare->parse_new_range($_);
447 4 ▶ defined($range) ? $cmp->overlap($range) : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $range ? : 4 0 4
448 } @$list);
449 2 $result;
450 }
451  
452 5 ▶sub get_overlapping_range {
Subroutine get_overlapping_range: called CC=3 SCAR=11.0
Pod documented
453 5 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 4 1 5
454 5 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 4 1 5
455 5 Net::IP::RangeCompare->SUPER::get_overlapping_range(\%HELPER,@_);
456 }
457  
458 22 ▶sub consolidate_ranges {
Subroutine consolidate_ranges: called CC=3 SCAR=11.0
Pod documented
459 22 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 10 12 22
460 22 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 10 12 22
461 22 Net::IP::RangeCompare->SUPER::consolidate_ranges(\%HELPER,@_)
462 }
463  
464 2 ▶sub fill_missing_ranges {
Subroutine fill_missing_ranges: called CC=3 SCAR=11.7
Pod documented
465 2 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 0 2 2
466 2 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 0 2 2
467 2 Net::IP::RangeCompare->SUPER::fill_missing_ranges(\%HELPER,@_);
468 }
469  
470 1 ▶sub range_start_end_fill {
Subroutine range_start_end_fill: called CC=3 SCAR=11.7
Pod documented
471 1 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 0 1 1
472 1 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 0 1 1
473 1 Net::IP::RangeCompare->SUPER::range_start_end_fill(\%HELPER,@_);
474 }
475  
476 6 ▶sub range_compare {
Subroutine range_compare: called CC=3 SCAR=11.1
Pod documented
477 6 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 0 6 6
478 6 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 0 6 6
479 6 my $sub=Net::IP::RangeCompare->SUPER::range_compare(\%HELPER,@_) ;
480 30 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
481 30 my @row=$sub->();
482 30 ▶ return () unless @row;
Branch: 1 branch
BranchTrueFalseTotal
unless @row 6 24 30
483 24 return (get_common_range(\@row),@row);
484 }
485 6 }
486  
487 1 ▶sub range_compare_force_cidr {
Subroutine range_compare_force_cidr: called CC=1 SCAR=0.0
Pod documented
488 1 my $sub=range_compare(@_);
489  
490 1 my ($common,@row)=$sub->();
491 1 my ($cidr,$notation,$next)=$common->get_first_cidr;
492 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
493 7 ▶ return () unless @row;
Branch: 1 branch
BranchTrueFalseTotal
unless @row 1 6 7
494 6 my @return_row=($cidr,$notation,@row);
495 6 ▶ if($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) { } 1 5 6
496 1 ($cidr,$notation,$next)=$next->get_first_cidr;
497 } else {
498 5 ($common,@row)=$sub->();
499 5 ▶ if(@row) {
Branch: 1 branch
BranchTrueFalseTotal
if (@row) { } 4 1 5
500 4 ($cidr,$notation,$next)=$common->get_first_cidr
501 } else {
502 1 $next=undef;
503 }
504 }
505 @return_row
506 6 }
507 1 }
508  
509 50 ▶sub compare_row {
Subroutine compare_row: called CC=4 SCAR=13.9
Pod documented
510 50 ▶ shift if $_[0] eq 'Net::IP::RangeCompare';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "Net::IP::RangeCompare" 33 17 50
511 50 ▶ shift if $_[0] eq \%HELPER;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq \%Net::IP::RangeCompare::HELPER 33 17 50
512 50 my ($ref,$row, $cols)=@_;
513 50 ▶ return Net::IP::RangeCompare->SUPER::init_compare_row(\%HELPER,$ref)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols 4 46 50
514 unless defined($cols);
515 46 Net::IP::RangeCompare->SUPER::compare_row(\%HELPER,$ref,$row, $cols);
516 }
517  
518 =pod
519  
520 =back
521  
522 =cut
523  
524 ############################################
525 #
526 # End of the package
527 12 1;
528  
529 ############################################
530 #
531 # Helper package
532 package Net::IP::RangeCompare::Simple;
533  
534 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
535 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
536 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
537 12 ▶use constant key_sources=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
538 12 ▶use constant key_columns=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
539 12 ▶use constant key_compare=>2;
Subroutine BEGIN: called CC=1 SCAR=0.0
540 12 ▶use constant key_changed=>3;
Subroutine BEGIN: called CC=1 SCAR=0.0
541  
542 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
543 1 my ($class)=@_;
544 1 my $ref=[];
545 1 $ref->[key_sources]={};
546 1 $ref->[key_changed]={};
547 1 $ref->[key_columns]=[];
548 1 $ref->[key_compare]=undef;
549  
550 1 bless $ref,$class;
551 }
552  
553  
554 7 ▶sub add_range ($$) {
Subroutine add_range: called CC=5 SCAR=16.2
555 7 my ($s,$key,$range)=@_;
556 7 ▶ croak "Key is not defined" unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 7 7
557 7 ▶ croak "Range is not defined" unless defined($range);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 0 7 7
558  
559 7 my $obj=Net::IP::RangeCompare->parse_new_range($range);
560 7 ▶ croak "Could not parse: $range" unless $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 7 7
561  
562 7 my $list;
563  
564 7 ▶ if(exists $s->[key_sources]->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[0]{$key}) { } 4 3 7
565 4 $list=$s->[key_sources]->{$key};
566 } else {
567 3 $s->[key_sources]->{$key}=[];
568 3 $list=$s->[key_sources]->{$key};
569 }
570 7 push @$list,$obj;
571 7 $s->[key_changed]->{$key}=1;
572 7 $obj
573 }
574  
575 2 ▶sub get_ranges_by_key ($) {
Subroutine get_ranges_by_key: called CC=3 SCAR=11.0
576 2 my ($s,$key)=@_;
577 2 ▶ croak "key was not defined" unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 2 2
578  
579 1 return [@{$s->[key_sources]->{$key}}]
580 2 ▶ if exists $s->[key_sources]->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->[0]{$key} 1 1 2
581
582 1 return undef;
583 }
584  
585 2 ▶sub compare_ranges {
Subroutine compare_ranges: called CC=6 SCAR=17.9
586 2 my ($s,@keys)=@_;
587 2 my %exclude=map { ($_,1) } @keys;
588 2 ▶ croak "no ranges defined" unless keys %{$s->[key_sources]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$s[0];} 0 2 2
589
590 2 my $columns=$s->[key_columns];
591 2 @$columns=();
592 2 my $compare_ref=[];
593 2 ▶ while(my ($key,$ranges)=each %{$s->[key_sources]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$s[0];}) 6 2 8
594 6 ▶ next if exists $exclude{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $exclude{$key} 1 5 6
595 5 push @$columns,$key;
596 $s->[key_sources]->{$key}=Net::IP::RangeCompare::consolidate_ranges($ranges)
597 5 ▶ if $s->[key_changed]->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $s->[3]{$key} 3 2 5
598 5 $s->[key_changed]->{$key}=0;
599 5 push @$compare_ref,$s->[key_sources]->{$key};
600  
601 }
602 2 ▶ croak "no ranges defined" if $#$columns==-1;
Branch: 1 branch
BranchTrueFalseTotal
if $columns->$#* == -1 0 2 2
603  
604 2 $s->[key_compare]=Net::IP::RangeCompare::range_compare(
605 $compare_ref
606 ,consolidate_ranges=>0
607 );
608  
609 2 1
610 }
611  
612 14 ▶sub get_row () {
Subroutine get_row: called CC=4 SCAR=13.9
613 14 my ($s)=@_;
614  
615 14 ▶ croak "no ranges defined" unless keys %{$s->[key_sources]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$s[0];} 0 14 14
616  
617 #make sure we have something to compare
618 14 ▶ $s->compare_ranges
Branch: 1 branch
BranchTrueFalseTotal
unless $s->[2] 1 13 14
619 unless $s->[key_compare];
620 14 my %row;
621 14 my (@cols)=$s->[key_compare]->();
622 14 ▶ return () unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 2 12 14
623 12 my $common;
624  
625 12 ($common,@row{@{$s->[key_columns]}})=@cols;
626  
627 12 $common,%row
628  
629 }
630  
631 0 ▶sub get_keys () {
Subroutine get_keys: not called CC=1 SCAR=6.9
632 0 keys %{$_[0]->[key_sources]}
633 }
634  
635 ############################################
636 #
637 # End of the package
638 12 1;
639  
640 __END__
↑ Summary