Regex/CompoundRange.pm

filter: Statementstmt 95.7% 178 / 186 Branchbran 78.7% 63 / 80 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 90.4% 19 / 21 Podpod 0.0% 0 / 14 totaltotal 86.7% 282 / 325 CC 47 SCAR 20.4
CC
47
Cov
91%
sectionify
16.0
union
9.3
regex
8.4
CRAP 7.7
« Regex/Iterator.pm ↑ Summary Regex.pm »
1 # Number::Range::Regex::CompoundRange
2 #
3 # Copyright 2012 Brian Szymanski. All rights reserved. This module is
4 # free software; you can redistribute it and/or modify it under the same
5 # terms as Perl itself.
6  
7 package Number::Range::Regex::CompoundRange;
8  
9 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 ▶use vars qw ( @ISA @EXPORT @EXPORT_OK $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 eval { require warnings; }; #it's ok if we can't load warnings
13  
14 14 require Exporter;
15 14 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 @ISA = qw( Exporter Number::Range::Regex::Range );
17  
18 14 $VERSION = '0.32';
19  
20 14 ▶use Number::Range::Regex::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶use Number::Range::Regex::Util::inf qw ( neg_inf pos_inf );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 918 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
24 918 ▶ my $opts = option_mangler( ref $_[-1] eq 'HASH' ? pop : undef );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 918 918
25 918 my ($class, @ranges) = @_;
26 # TODO: do we need to collapse&sort the ranges? either by calling
27 # multi_union (which has a collapsing effect) or by an explicit sort
28 # by min + verify no overlaps + _collapse_ranges ?
29 918 return bless { ranges => [ @ranges ], opts => $opts }, $class;
30 }
31  
32 403 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
33 403 my ($self, $passed_opts) = @_;
34 403 return join(',', map { $_->to_string() } @{$self->{ranges}});
35 }
36  
37 216 ▶sub regex {
Subroutine regex: called CC=8 SCAR=21.3
Pod undocumented
38 216 my ($self, $passed_opts) = @_;
39  
40 216 my $opts = option_mangler( $self->{opts}, $passed_opts );
41  
42 216 ▶ my $separator = $opts->{readable} ? ' | ' : '|';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'readable'} ? : 0 216 216
43 216 my $regex_str;
44 216 ▶ if(@{$self->{ranges}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'ranges'};}) { } 197 19 216
45 197 $regex_str = join $separator, map { $_->regex( { %$opts, comment => 0 } ) } @{$self->{ranges}};
46 } else {
47 19 $regex_str = '(?!)'; # never matches
48 }
49 216 ▶ $regex_str = " $regex_str " if $opts->{readable};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"readable"} 0 216 216
50  
51 216 ▶ my $modifier_maybe = $opts->{readable} ? '(?x)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'readable'} ? : 0 216 216
52 216 my ($begin_comment_maybe, $end_comment_maybe) = ('', '');
53 216 ▶ if($opts->{comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"comment"}) 216 0 216
54 216 my $comment = "Number::Range::Regex::CompoundRange[".$self->to_string."]";
55 216 ▶ $begin_comment_maybe = $opts->{readable} ? " # begin $comment" : "(?# begin $comment )";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'readable'} ? : 0 216 216
56 216 ▶ $end_comment_maybe = $opts->{readable} ? " # end $comment" : "(?# end $comment )";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'readable'} ? : 0 216 216
57 }
58 216 return qr/(?:$begin_comment_maybe$modifier_maybe(?:$regex_str)$end_comment_maybe)/;
59 }
60  
61 513 ▶sub _do_unequal_min {
Subroutine _do_unequal_min: called CC=3 SCAR=11.0
62 #warn "in _do_unequal_min";
63 513 my ($self, $lower, $upper, $ptr, $ranges) = @_;
64 513 ▶ if( $lower->{max} > $upper->{max} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lower->{'max'} > $upper->{'max'}) { } 47 466 513
elsif ($lower->{'max'} >= $upper->{'min'}) { } 32 434 466
65 # 3 ranges, last of which may yet overlap
66 47 my $r1 = Number::Range::Regex::SimpleRange->new( $lower->{min}, $upper->{min}-1 );
67 47 my $r2 = $upper;
68 47 my $r3 = Number::Range::Regex::SimpleRange->new( $upper->{max}+1, $lower->{max} );
69 #warn "l: $lower->{min}..$lower->{max} -> $r1->{min}..$r1->{max},$r2->{min}..$r2->{max},$r3->{min}..$r3->{max}";
70 47 splice( @$ranges, $$ptr, 1, ($r1, $r2, $r3) );
71 47 $$ptr += 2; # $r3 may overlap something else
72 } elsif( $lower->{max} >= $upper->{min} ) {
73 # 2 ranges, latter of which may yet overlap
74 32 my $r1 = Number::Range::Regex::SimpleRange->new( $lower->{min}, $upper->{min}-1 );
75 32 my $r2 = Number::Range::Regex::SimpleRange->new( $upper->{min}, $lower->{max} );
76 #warn "l: $lower->{min}..$lower->{max} -> $r1->{min}..$r1->{max},$r2->{min}..$r2->{max}";
77 32 splice( @$ranges, $$ptr, 1, ($r1, $r2 ) );
78 32 $$ptr += 1;
79 } else { # $lower->{max} < $upper->{min}
80 # 1 range, no overlap
81 #warn "l: $lower->{min}..$lower->{max} is ok";
82 434 $$ptr++;
83 }
84 }
85  
86 725 ▶sub sectionify {
Subroutine sectionify: called CC=16 SCAR=27.7
Pod undocumented
87 725 my ($self, $other) = @_;
88  
89 725 my @s_ranges = @{$self->{ranges}};
90 725 ▶ my @o_ranges = $other->isa('Number::Range::Regex::CompoundRange') ? @{$other->{ranges}} :
Branch: 2 branches
BranchTrueFalseTotal
$other->isa('Number::Range::Regex::CompoundRange') ? : 44 681 725
$other->isa('Number::Range::Regex::SimpleRange') ? : 681 0 681
91 $other->isa('Number::Range::Regex::SimpleRange') ? ( $other ) :
92 die "other is neither a simple nor compound range!";
93  
94 #warn "s_ranges1: ".join ",", map { "$_->{min}..$_->{max}" } @s_ranges;
95 #warn "o_ranges1: ".join ",", map { "$_->{min}..$_->{max}" } @o_ranges;
96  
97 # munge ranges so that there are no partial overlaps - only
98 # non-overlaps and complete overlaps e.g:
99 # if s=(6..12) and o=(7..13):
100 # s=(6,7..12) and o=(7..12,13);
101 # if s=(6..12) and o=(7..9):
102 # s=(6,7..9,10..12) and o=(7..9);
103 725 my ($s_ptr, $o_ptr) = (0, 0);
104 725 ▶ while( ($s_ptr < @s_ranges) && ($o_ptr < @o_ranges) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_ptr < @s_ranges and $o_ptr < @o_ranges) 585 725 1310
Condition100%3/3
$s_ptr < @s_ranges and $o_ptr < @o_ranges
!ll&&!rl&&r
67352585
MC/DC100%2/2
$s_ptr < @s_ranges and $o_ptr < @o_ranges
$s_ptr < @s_ranges $o_ptr < @o_ranges
Truth table100%3/3
A and B
A= $s_ptr < @s_rangesB= $o_ptr < @o_ranges
ABresult
0X0
100
111
105 #warn "s_ranges: @s_ranges, o_ranges: @o_ranges";
106 585 my $this_s = $s_ranges[$s_ptr];
107 585 my $this_o = $o_ranges[$o_ptr];
108 #warn "checking this_s: $this_s->{min}..$this_s->{max}, this_o: $this_o->{min}..$this_o->{max}";
109 585 ▶ if( $this_s->{min} < $this_o->{min} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_s->{'min'} < $this_o->{'min'}) { } 368 217 585
elsif ($this_s->{'min'} > $this_o->{'min'}) { } 145 72 217
110 #printf STDERR "l==s, ";
111 368 $self->_do_unequal_min($this_s, $this_o, \$s_ptr, \@s_ranges );
112 } elsif( $this_s->{min} > $this_o->{min} ) {
113 #printf STDERR "l==o, ";
114 145 $self->_do_unequal_min($this_o, $this_s, \$o_ptr, \@o_ranges );
115 } else { # $this_s->{min} == $this_o->{min}
116 72 ▶ if( $this_s->{max} < $this_o->{max} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_s->{'max'} < $this_o->{'max'}) { } 13 59 72
elsif ($this_s->{'max'} > $this_o->{'max'}) { } 19 40 59
117 # 2 ranges, latter of which may yet overlap
118 13 my $r1 = $this_s;
119 13 my $r2 = Number::Range::Regex::SimpleRange->new($this_s->{max}+1, $this_o->{max} );
120 13 splice( @o_ranges, $o_ptr, 1, ($r1, $r2) );
121 #warn "o: $this_o->{min}..$this_o->{max} -> $r1->{min}..$r1->{max},$r2->{min}..$r2->{max}";
122 13 $o_ptr++; # $r2 may overlap something else
123 } elsif( $this_s->{max} > $this_o->{max} ) {
124 # 2 ranges, latter of which may yet overlap
125 19 my $r1 = $this_o;
126 19 my $r2 = Number::Range::Regex::SimpleRange->new($this_o->{max}+1, $this_s->{max} );
127 19 splice( @s_ranges, $s_ptr, 1, ($r1, $r2) );
128 #warn "s: $this_s->{min}..$this_s->{max} -> $r1->{min}..$r1->{max},$r2->{min}..$r2->{max}";
129 19 $s_ptr++; # $r2 may overlap something else
130 } else { # $this_s->{max} == $this_o->{min}
131 # 1 range, no overlap
132 #warn "s/o: $this_o->{min}..$this_o->{max} is ok";
133 40 $s_ptr++;
134 40 $o_ptr++;
135 }
136 }
137 }
138  
139 #warn "s_ranges2: ".join ",", map { "$_->{min}..$_->{max}" } @s_ranges;
140 #warn "o_ranges2: ".join ",", map { "$_->{min}..$_->{max}" } @o_ranges;
141  
142 725 my $sections;
143 725 ($s_ptr, $o_ptr) = (0, 0);
144 725 ▶ while( ($s_ptr < @s_ranges) && ($o_ptr < @o_ranges) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_ptr < @s_ranges and $o_ptr < @o_ranges) 553 725 1278
Condition100%3/3
$s_ptr < @s_ranges and $o_ptr < @o_ranges
!ll&&!rl&&r
67352553
MC/DC100%2/2
$s_ptr < @s_ranges and $o_ptr < @o_ranges
$s_ptr < @s_ranges $o_ptr < @o_ranges
Truth table100%3/3
A and B
A= $s_ptr < @s_rangesB= $o_ptr < @o_ranges
ABresult
0X0
100
111
145 553 my $this_s = $s_ranges[$s_ptr];
146 553 my $this_o = $o_ranges[$o_ptr];
147 553 ▶ if( $this_s->{min} < $this_o->{min} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_s->{'min'} < $this_o->{'min'}) { } 328 225 553
elsif ($this_o->{'min'} < $this_s->{'min'}) { } 106 119 225
148 328 push @{$sections->{just_self}}, $this_s;
149 328 push @{$sections->{in_either}}, $this_s;
150 328 $s_ptr++;
151 } elsif( $this_o->{min} < $this_s->{min} ) {
152 106 push @{$sections->{just_other}}, $this_o;
153 106 push @{$sections->{in_either}}, $this_o;
154 106 $o_ptr++;
155 } else { # $this_s->{min} == $this_o->{min}
156 119 ▶ die "internal error in sectionify" unless $this_s->{max} == $this_o->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $this_s->{"max"} == $this_o->{"max"} 0 119 119
157 119 push @{$sections->{in_both}}, $this_s;
158 119 push @{$sections->{in_either}}, $this_s;
159 119 $s_ptr++;
160 119 $o_ptr++;
161 }
162 }
163 725 ▶ if( $o_ptr < @o_ranges ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($o_ptr < @o_ranges) { } 661 64 725
elsif ($s_ptr < @s_ranges) { } 52 12 64
164 661 push @{$sections->{just_other}}, @o_ranges[$o_ptr..$#o_ranges];
165 661 push @{$sections->{in_either}}, @o_ranges[$o_ptr..$#o_ranges];
166 } elsif( $s_ptr < @s_ranges ) {
167 52 push @{$sections->{just_self}}, @s_ranges[$s_ptr..$#s_ranges];
168 52 push @{$sections->{in_either}}, @s_ranges[$s_ptr..$#s_ranges];
169 }
170  
171 #warn "just_self: ".join ",", map { "$_->{min}..$_->{max}" } @{$sections->{just_self}};
172 #warn "in_both: ".join ",", map { "$_->{min}..$_->{max}" } @{$sections->{in_both}};
173 #warn "in_either: ".join ",", map { "$_->{min}..$_->{max}" } @{$sections->{in_either}};
174 #warn "just_other: ".join ",", map { "$_->{min}..$_->{max}" } @{$sections->{just_other}};
175  
176 725 return $sections;
177 }
178  
179  
180 10 ▶sub intersection {
Subroutine intersection: called CC=1 SCAR=0.0
Pod undocumented
181 10 my ($self, $other) = @_;
182 10 my $sections = $self->sectionify( $other );
183 10 return multi_union( @{$sections->{in_both}} );
184 }
185  
186 22 ▶sub subtract {
Subroutine subtract: called CC=1 SCAR=0.0
Pod undocumented
187 22 my ($self, $other) = @_;
188 22 my $sections = $self->sectionify( $other );
189 22 return multi_union( @{$sections->{just_self}} );
190 }
191  
192 12 ▶sub xor {
Subroutine xor: called CC=1 SCAR=0.0
Pod undocumented
193 12 my ($self, $other) = @_;
194 12 my $sections = $self->sectionify( $other );
195 12 return multi_union( @{$sections->{just_self}}, @{$sections->{just_other}} );
196 }
197  
198 35 ▶sub invert {
Subroutine invert: called CC=6 SCAR=17.9
Pod undocumented
199 35 my ($self) = @_;
200 35 my @included = @{$self->{ranges}};
201 35 ▶ return Number::Range::Regex::SimpleRange->new( neg_inf, pos_inf ) unless @included;
Branch: 1 branch
BranchTrueFalseTotal
unless @included 10 25 35
202 25 my @excluded = ();
203 25 ▶ if($included[0]->{min} != neg_inf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($included[0]{"min"} != neg_inf()) 22 3 25
204 22 push @excluded, Number::Range::Regex::SimpleRange->new( neg_inf, $included[0]->{min}-1 );
205 }
206 25 ▶ for(my $c=1; $c<@included; ++$c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < @included) 34 25 59
207 34 my $last = $included[$c-1];
208 34 my $this = $included[$c];
209 34 ▶ if($last->{max}+1 > $this->{min}-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{'max'} + 1 > $this->{'min'} - 1) { } 0 34 34
210 0 die "internal error - overlapping SRs?";
211 } else {
212 34 push @excluded, Number::Range::Regex::SimpleRange->new( $last->{max}+1, $this->{min}-1 );
213 }
214 }
215 25 ▶ if($included[-1]->{max} != pos_inf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($included[-1]{"max"} != pos_inf()) 22 3 25
216 22 push @excluded, Number::Range::Regex::SimpleRange->new( $included[-1]->{max}+1, pos_inf );
217 }
218 25 return __PACKAGE__->new( @excluded );
219 }
220  
221 681 ▶sub union {
Subroutine union: called CC=9 SCAR=22.3
Pod undocumented
222 681 ▶ my $opts = option_mangler( ref $_[-1] eq 'HASH' ? pop : undef );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 660 21 681
223 681 my ($self, @other) = @_;
224 #warn "cr::u, wo: $opts->{warn_overlap}, $self, @other";
225 681 ▶ return multi_union( $self, @other ) if @other > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @other > 1 0 681 681
226 681 my $sections = $self->sectionify( $other[0] );
227 681 ▶ if( $opts->{warn_overlap} && $sections->{in_both} && @{ $sections->{in_both} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"warn_overlap"} and $sections->{"in_both"} and @{$sections->{"in_both"};}) 2 679 681
Condition83%5/6
$opts->{"warn_overlap"} and $sections->{"in_both"} and @{$sections->{"in_both"};}
!ll&&!rl&&r
67902
$opts->{"warn_overlap"} and $sections->{"in_both"}
!ll&&!rl&&r
1415382
MC/DC66%2/3
$opts->{"warn_overlap"} and $sections->{"in_both"} and @{$sections->{"in_both"};}
$opts->{"warn_overlap"} $sections->{"in_both"} @{$sections->{"in_both"};}
Truth table75%3/4
A and B and C
A= $opts->{"warn_overlap"}B= $sections->{"in_both"}C= @{$sections->{"in_both"};}
ABCresult
0XX0
10X0
1100
1111
228 2 ▶ my $subname = $opts->{warn_overlap} eq '1' ? 'union' : $opts->{warn_overlap};
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'warn_overlap'} eq '1' ? : 2 0 2
229 2 warn "$subname call got overlap(s): ", join ",", @{ $sections->{in_both} };
230 }
231 681 my @in_either = _collapse_ranges( @{$sections->{in_either}} );
232 681 ▶ if( @in_either == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@in_either == 0) { } 0 681 681
elsif (@in_either == 1) { } 570 111 681
233 0 return empty_set();
234 } elsif( @in_either == 1 ) {
235 570 return $in_either[0];
236 } else {
237 111 return __PACKAGE__->new( @in_either );
238 }
239 }
240  
241 684 ▶sub _collapse_ranges {
Subroutine _collapse_ranges: called CC=3 SCAR=11.0
242 684 my @ranges = @_;
243 684 my $last_r;
244 684 my $this_r = $ranges[0];
245 684 ▶ for (my $rpos = 1; $rpos < @ranges; $rpos++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rpos < @ranges) 370 684 1054
246 370 $last_r = $this_r;
247 370 $this_r = $ranges[$rpos];
248 370 ▶ if($last_r->touches($this_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_r->touches($this_r)) 69 301 370
249 69 $this_r = $last_r->union( $this_r );
250 69 splice(@ranges, $rpos-1, 2, $this_r);
251 69 $rpos--;
252 }
253 }
254 684 return @ranges;
255 }
256  
257 #sub _is_contiguous {
258 # my ($self) = @_;
259 # my $last_r;
260 # my $this_r = $self->{ranges}->[0];
261 # for (my $rpos = 1; $rpos < @{$self->{ranges}}; $rpos++ ) {
262 # $last_r = $this_r;
263 # $this_r = $self->{ranges}->[$rpos];
264 # return if $last_r->{max}+1 < $this_r->{min};
265 # }
266 # return ($self->{ranges}->[0]->{min}, $self->{ranges}->[-1]->{max});
267 #}
268  
269 536 ▶sub contains {
Subroutine contains: called CC=3 SCAR=11.0
Pod undocumented
270 536 my ($self, $n) = @_;
271 536 foreach my $r (@{$self->{ranges}}) {
272 857 ▶ return 1 if $r->contains( $n );
Branch: 1 branch
BranchTrueFalseTotal
if $r->contains($n) 207 650 857
273 }
274 329 return;
275 }
276  
277 95 ▶sub is_empty {
Subroutine is_empty: called CC=1 SCAR=0.0
Pod undocumented
278 95 my ($self) = @_;
279 95 return !@{$self->{ranges}};
280 }
281  
282 0 ▶sub has_lower_bound {
Subroutine has_lower_bound: not called CC=2 SCAR=17.9
Pod undocumented
283 0 my ($self) = @_;
284 0 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 0 0
285 0 return $self->{ranges}->[0]->has_lower_bound;
286 }
287  
288 0 ▶sub has_upper_bound {
Subroutine has_upper_bound: not called CC=2 SCAR=17.9
Pod undocumented
289 0 my ($self) = @_;
290 0 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 0 0
291 0 return $self->{ranges}->[-1]->has_upper_bound;
292 }
293  
294 44 ▶sub is_infinite {
Subroutine is_infinite: called CC=3 SCAR=11.0
Pod undocumented
295 44 my ($self) = @_;
296 44 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 5 39 44
297 39 ▶ return ! ( $self->{ranges}->[0]->has_lower_bound && $self->{ranges}->[-1]->has_upper_bound );
Condition100%3/3
$self->{'ranges'}[0]->has_lower_bound && $self->{'ranges'}[-1]->has_upper_bound
!ll&&!rl&&r
13323
MC/DC100%2/2
$self->{'ranges'}[0]->has_lower_bound && $self->{'ranges'}[-1]->has_upper_bound
$self->{'ranges'}[0]->has_lower_bound $self->{'ranges'}[-1]->has_upper_bound
Truth table100%3/3
A && B
A= $self->{'ranges'}[0]->has_lower_boundB= $self->{'ranges'}[-1]->has_upper_bound
ABresult
0X0
100
111
298 }
299  
300 14 1;
301  
« Regex/Iterator.pm ↑ Summary Regex.pm »