Merge.pm

filter: Statementstmt 97.4% 116 / 119 Branchbran 86.1% 31 / 36 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 93.7% 15 / 16 Podpod 100% 3 / 3 totaltotal 92.5% 175 / 189 CC 28 SCAR 16.5
CC
28
Cov
94%
_combine
9.0
_add_to_stack
8.0
_split
5.2
CRAP 5.2
↑ Summary Merge/Boilerplate.pm »
1 #!/usr/bin/perl
2  
3 #
4 # Copyright (C) 2016-2025 Joelle Maslak
5 # All Rights Reserved - See License
6 #
7  
8 package Range::Merge;
9 8 $Range::Merge::VERSION = '2.253531';
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Range::Merge::Boilerplate 'script';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 require Exporter;
16 8 our @ISA = qw(Exporter);
17 8 our @EXPORT_OK = qw(merge merge_discrete merge_ipv4);
18  
19 8 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Net::CIDR;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # ABSTRACT: Merges ranges of data including subset/superset ranges
24  
25  
26  
27 15 ▶sub merge($ranges) {
Subroutine merge: called CC=1 SCAR=0.0
Pod documented
28 15 my $sorted = _sort($ranges);
29 15 my $split = [];
30 15 _split( $sorted, $split );
31 15 return _combine($split);
32 }
33  
34  
35 4 ▶sub merge_discrete($values) {
Subroutine merge_discrete: called CC=5 SCAR=16.1
Pod documented
36 4 my $ranges = [];
37  
38 4 my $run;
39  
40 4 for my $num ( sort { $a <=> $b } @$values ) {
41 9 ▶ if ( !defined $run ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $run) { } 3 6 9
42 3 $run = [ $num, $num ];
43 3 push @$ranges, $run;
44 } else {
45 6 ▶ if ( $run->[1] == $num ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($run->[1] == $num) { } 1 5 6
elsif ($run->[1] == $num - 1) { } 2 3 5
46 # Do nothing
47 } elsif ( $run->[1] == $num - 1 ) {
48 2 $run->[1] = $num;
49 } else {
50 3 $run = [ $num, $num ];
51 3 push @$ranges, $run;
52 }
53 }
54 }
55  
56 4 return $ranges;
57 }
58  
59  
60 13 ▶sub merge_ipv4($cidr) {
Subroutine merge_ipv4: called CC=1 SCAR=0.0
Pod documented
61 13 my $ranges = [];
62 13 @$ranges = map { _cidr2range($_) } @$cidr;
63 12 my $combined = merge($ranges);
64 12 return _range2cidr($combined);
65 }
66  
67 66125 ▶sub _cidr2range($cidr) {
Subroutine _cidr2range: called CC=2 SCAR=6.9
68 66125 my ( $ip, @a ) = @$cidr;
69 66125 ▶ die("Invalid IP address: $ip") if ( grep { /^0[0-9]/ } split( /[\/.]/, $ip ) );
Branch: 1 branch
BranchTrueFalseTotal
if grep {/^0[0-9]/u;} split(m[[/.]]u, $ip, 0) 1 66124 66125
70 66124 my ($range) = Net::CIDR::cidr2range($ip);
71 66124 my (@parts) = map { unpack( 'N', inet_aton($_) ) } split( /-/, $range );
72  
73 66124 return [ @parts, @a ];
74 }
75  
76 12 ▶sub _range2cidr($ranges) {
Subroutine _range2cidr: called CC=3 SCAR=11.0
77 12 my @output;
78 12 foreach my $range (@$ranges) {
79 24 my ( $start, $end, @other ) = @$range;
80 24 $start = inet_ntoa( pack( 'N', $start ) );
81 24 $end = inet_ntoa( pack( 'N', $end ) );
82 24 foreach my $cidr ( Net::CIDR::range2cidr("$start-$end") ) {
83 32 push @output, [ $cidr, @other ];
84 }
85 }
86 12 return \@output;
87 }
88  
89 # Sorts by starting address and then by reverse (less specific to more
90 # specific)
91 15 ▶sub _sort($ranges) {
Subroutine _sort: called CC=2 SCAR=7.0
92 15 ▶ my (@output) = sort { ( $a->[0] <=> $b->[0] ) || ( $b->[1] <=> $a->[1] ) } @$ranges;
Condition66%2/3
$Range::Merge::a->[0] <=> $Range::Merge::b->[0] || $Range::Merge::b->[1] <=> $Range::Merge::a->[1]
l!l&&r!l&&!r
6614610
MC/DC0%0/2
$Range::Merge::a->[0] <=> $Range::Merge::b->[0] || $Range::Merge::b->[1] <=> $Range::Merge::a->[1]
$Range::Merge::a->[0] <=> $Range::Merge::b->[0] $Range::Merge::b->[1] <=> $Range::Merge::a->[1]
Truth table66%2/3
A || B
A= $Range::Merge::a->[0] <=> $Range::Merge::b->[0]B= $Range::Merge::b->[1] <=> $Range::Merge::a->[1]
ABresult
1X1
011
000
93 15 return \@output;
94 }
95  
96 0 ▶sub _merge($ranges) {
Subroutine _merge: not called CC=1 SCAR=6.9
97 0 my $split = [];
98 0 _split( $ranges, $split );
99 0 return _combine($split);
100 }
101  
102 15 ▶sub _combine($ranges) {
Subroutine _combine: called CC=9 SCAR=22.0
103 15 my @output;
104  
105 my $last;
106 15 foreach my $range (@$ranges) {
107 66138 ▶ if ( !defined($last) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $last) 15 66123 66138
108 15 $last = [@$range];
109 15 next;
110 }
111 66123 ▶ if ( ( $last->[1] == $range->[0] - 1 ) && ( scalar(@$last) == scalar(@$range) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->[1] == $range->[0] - 1 and scalar @$last == scalar @$range) { } 66108 15 66123
Condition66%2/3
$last->[1] == $range->[0] - 1 and scalar @$last == scalar @$range
!ll&&!rl&&r
15066108
MC/DC50%1/2
$last->[1] == $range->[0] - 1 and scalar @$last == scalar @$range
$last->[1] == $range->[0] - 1 scalar @$last == scalar @$range
Truth table66%2/3
A and B
A= $last->[1] == $range->[0] - 1B= scalar @$last == scalar @$range
ABresult
0X0
100
111
112 66108 my $nomatch;
113 66108 ▶ for ( my $i = 2; $i < scalar(@$range); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$range) 6 66103 66109
114 6 ▶ if ( $last->[$i] ne $range->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->[$i] ne $range->[$i]) 5 1 6
115 5 $nomatch = 1;
116 5 last;
117 }
118 }
119 66108 ▶ if ($nomatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nomatch) { } 5 66103 66108
120 5 push @output, $last;
121 5 $last = [@$range];
122 } else {
123 66103 $last->[1] = $range->[1];
124 }
125 } else {
126 15 push @output, $last;
127 15 $last = [@$range];
128 }
129 }
130 15 ▶ if ( defined($last) ) { push @output, $last }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last) 15 0 15
131  
132 15 return \@output;
133 }
134  
135 15 ▶sub _split ( $ranges, $output, $stack = [] ) {
Subroutine _split: called CC=5 SCAR=16.4
136 # Termination condition
137 15 ▶ return if scalar( $ranges->@* ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$ranges == 0 0 15 15
138  
139 # We just repeatedly call _add_to_stack
140 15 foreach my $range ( $ranges->@* ) {
141 66136 _add_to_stack( $range, $stack, $output );
142 }
143  
144 # Return stack
145 15 ▶ if ( scalar( $stack->@* ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$stack) 15 0 15
146 15 push $output->@*, $stack->@*;
147 }
148  
149 15 return;
150 }
151  
152 66136 ▶sub _add_to_stack ( $range, $stack, $output ) {
Subroutine _add_to_stack: called CC=8 SCAR=20.8
153 66136 ▶ if ( !scalar( $stack->@* ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$stack) 15 66121 66136
154 # Empty stack
155 15 push $stack->@*, $range;
156 15 return;
157 }
158  
159 # We know the following:
160 #
161 # 1. The stack is sorted
162 # 2. There are no overlapping elements
163 # 2a. Thus we only have to split 1 element max
164 # 3. The stack has at least one element
165  
166 66121 my (@lstack) = grep { $_->[1] < $range->[0] } @$stack;
167 66121 my (@rstack) = grep { $_->[0] > $range->[1] } @$stack;
168 66121 ▶ my (@mid) = grep { ( $_->[0] <= $range->[1] ) && ( $_->[1] >= $range->[0] ) } @$stack;
Condition100%3/3
$_->[0] <= $range->[1] && $_->[1] >= $range->[0]
!ll&&!rl&&r
3661178
MC/DC100%2/2
$_->[0] <= $range->[1] && $_->[1] >= $range->[0]
$_->[0] <= $range->[1] $_->[1] >= $range->[0]
Truth table100%3/3
A && B
A= $_->[0] <= $range->[1]B= $_->[1] >= $range->[0]
ABresult
0X0
100
111
169  
170 # Clear stack
171 66121 @$stack = ();
172  
173 # Output the stuff completely to the left of the new range
174 66121 push @$output, @lstack;
175  
176 # Option 1 -> No middle element, so just add the range (and the
177 # right stack) to the stack
178 66121 ▶ if ( !scalar(@mid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @mid) 66113 8 66121
179 66113 push @$stack, $range, @rstack;
180 66113 return;
181 }
182  
183 # We start with the left and right parts of the element that might
184 # need to be split.
185 8 my (@left) = $mid[0]->@*;
186 8 my (@right) = $mid[0]->@*;
187  
188 # Does the ele needing split start before the range? If so, add the piece
189 # needed to the output
190 8 ▶ if ( $left[0] < $range->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left[0] < $range->[0]) 6 2 8
191 6 @left[1] = $range->[0] - 1;
192 6 ▶ if ( $left[0] <= $left[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left[0] <= $left[1]) 6 0 6
193 6 push @$output, \@left;
194 }
195 }
196  
197 # We need to add the range to the stack
198 8 push @$stack, $range;
199  
200 # Does the ele needing split end after the range? If so, add the
201 # piece to the stack
202 8 ▶ if ( $right[1] > $range->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right[1] > $range->[1]) 4 4 8
203 4 @right[0] = $range->[1] + 1;
204 4 ▶ if ( $right[0] <= $right[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right[0] <= $right[1]) 4 0 4
205 4 push @$stack, \@right;
206 }
207 }
208  
209 8 push @$stack, @rstack;
210  
211 8 return;
212 }
213  
214 8 1;
215  
216 __END__
↑ Summary Merge/Boilerplate.pm »