blib/lib/Number/Continuation.pm

filter: Statementstmt 91.8% 79 / 86 Branchbran 81.6% 49 / 60 Conditioncond 90.0% 27 / 30 MC/DCmcdc 80.0% 12 / 15 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 87.9% 175 / 199 SCAR 38.3
CC
43
Cov
88%
continuation
43.1
_validate
4.2
BEGIN
1.0
CRAP 46.1
↑ Summary Build »
1 package Number::Continuation;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our ($VERSION, @EXPORT_OK);
11  
12 4 $VERSION = '0.04';
13 4 @EXPORT_OK = qw(continuation);
14  
15 sub continuation
16 {
17 7 ▶ my $opts = pop if ref $_[-1] eq 'HASH';
Subroutine continuation: called CC=40 SCAR=37.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "HASH" 1 6 7
18  
19 my $input = ref $_[0] eq 'ARRAY'
20 7 ▶ ? join ' ', @{$_[0]}
Branch: 3 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 7 7
@_ > 1 ? : 0 7 7
!builtin::refaddr($_[0]) ? : 7 0 7
21 : @_ > 1
22 ? join ' ', @_
23 : !refaddr $_[0]
24 ? $_[0]
25 : croak 'continuation($set | @set | \@set [, { options } ])';
26  
27 7 _validate($input);
28  
29 7 my $wantarray = wantarray;
30  
31 7 ▶ $opts->{delimiter} ||= '';
Condition100%2/2
$opts->{"delimiter"} ||= ""
l!l
16
MC/DC100%1/1
$opts->{"delimiter"} ||= ""
$opts->{"delimiter"}
Truth table100%2/2
A
A= $opts->{"delimiter"}
Aresult
11
00
32 7 ▶ $opts->{range} ||= '-';
Condition100%2/2
$opts->{"range"} ||= "-"
l!l
52
MC/DC100%1/1
$opts->{"range"} ||= "-"
$opts->{"range"}
Truth table100%2/2
A
A= $opts->{"range"}
Aresult
11
00
33 7 ▶ $opts->{separator} ||= ',';
Condition100%2/2
$opts->{"separator"} ||= ","
l!l
52
MC/DC100%1/1
$opts->{"separator"} ||= ","
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
Aresult
11
00
34  
35 7 @{$opts->{delimiters}} = split //, $opts->{delimiter};
36  
37 7 my @nums = split /\s+/, $input;
38  
39 7 my (%constructed, $have_neg_continuation, @lists, $output, @output);
40  
41 7 my $reset = 1;
42  
43 7 ▶ for (my $i = 0; $i < @nums; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nums) 97 7 104
44 # handy variables
45 97 ▶ my $prev_number = $nums[$i-1] || 0;
Condition50%1/2
$nums[$i - 1] || 0
l!l
970
MC/DC0%0/1
$nums[$i - 1] || 0
$nums[$i - 1]
Truth table50%1/2
A
A= $nums[$i - 1]
Aresult
11
00
46 97 ▶ my $current_number = $nums[$i ] || 0;
Condition50%1/2
$nums[$i] || 0
l!l
970
MC/DC0%0/1
$nums[$i] || 0
$nums[$i]
Truth table50%1/2
A
A= $nums[$i]
Aresult
11
00
47 97 ▶ my $next_number = $nums[$i+1] || 0;
Condition100%2/2
$nums[$i + 1] || 0
l!l
907
MC/DC100%1/1
$nums[$i + 1] || 0
$nums[$i + 1]
Truth table100%2/2
A
A= $nums[$i + 1]
Aresult
11
00
48  
49 # set if preceeded by continuation
50 my $prev_continuation = 1 if $constructed{begin}
51 && $constructed{middle}
52 97 ▶ && $constructed{end};
Branch: 1 branch
BranchTrueFalseTotal
if $constructed{"begin"} and $constructed{"middle"} and $constructed{"end"} 24 73 97
Condition100%6/6
$constructed{"begin"} and $constructed{"middle"} and $constructed{"end"}
!ll&&!rl&&r
502324
$constructed{"begin"} and $constructed{"middle"}
!ll&&!rl&&r
113947
MC/DC100%3/3
$constructed{"begin"} and $constructed{"middle"} and $constructed{"end"}
$constructed{"begin"} $constructed{"middle"} $constructed{"end"}
Truth table100%4/4
A and B and C
A= $constructed{"begin"}B= $constructed{"middle"}C= $constructed{"end"}
ABCresult
0XX0
10X0
1100
1111
53  
54 # set if negative continuation sensed (i.e. 3 2 1)
55 97 ▶ $have_neg_continuation = 1 if ($prev_number - $next_number == 2);
Branch: 1 branch
BranchTrueFalseTotal
if $prev_number - $next_number == 2 4 93 97
56  
57 # previous number greater than current one
58 97 ▶ if ($prev_number > $current_number && $i != 0 && !$have_neg_continuation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_number > $current_number and $i != 0 and not $have_neg_continuation) 6 91 97
Condition100%6/6
$prev_number > $current_number and $i != 0 and not $have_neg_continuation
!ll&&!rl&&r
81106
$prev_number > $current_number and $i != 0
!ll&&!rl&&r
78316
MC/DC100%3/3
$prev_number > $current_number and $i != 0 and not $have_neg_continuation
$prev_number > $current_number $i != 0 not $have_neg_continuation
Truth table100%4/4
A and B and not(C)
A= $prev_number > $current_numberB= $i != 0C= not $have_neg_continuation
ABCresult
0XX0
10X0
1100
1111
59 # previous number *exactly* greater 1
60 6 ▶ if ($prev_number - $current_number == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_number - $current_number == 1) { } 0 6 6
61 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
62 0 ▶ if (@lists) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lists) 0 0 0
63 0 push @output, [ @lists ];
64 0 undef @lists;
65 }
66 } else {
67 0 $output .= "$opts->{separator} ";
68 }
69 # previous number greater than 1 and no previous continuation
70 } else {
71 6 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 3 3 6
72 3 ▶ if (@lists) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lists) 2 1 3
73 2 push @output, [ @lists ];
74 2 undef @lists;
75 }
76 } else {
77 3 ▶ $output .= "$opts->{separator} " unless $prev_continuation;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev_continuation 2 1 3
78 }
79 }
80 # reset processing continuation state
81 6 $reset = 1;
82 }
83 # processing new continuation
84 97 ▶ if ($reset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reset) { } 47 50 97
elsif (defined $next_number and $next_number - $current_number == 1 || $current_number - $next_number == 1) { } 23 27 50
85 47 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 22 25 47
86 22 push @lists, $nums[$i];
87 22 ▶ push @output, [ @lists ] if $i == $#nums;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#nums 1 21 22
88 } else {
89 25 ▶ $output .= $opts->{delimiters}->[0] if $opts->{delimiters}->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"delimiters"}[0] 3 22 25
90 25 $output .= $nums[$i];
91 }
92 47 ▶ if (($next_number - $current_number) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_number - $current_number > 1) 14 33 47
93 14 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 7 7 14
94 7 ▶ if (@lists) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lists) 7 0 7
95 7 push @output, [ @lists ];
96 7 undef @lists;
97 }
98 } else {
99 7 $output .= "$opts->{separator} ";
100 }
101 14 next;
102 }
103 33 ($have_neg_continuation, $reset) = (0,0);
104 33 undef %constructed;
105  
106 33 $constructed{begin} = 1;
107 # process numbers in between (skipping if scalar context)
108 ▶ } elsif (defined($next_number) && (($next_number - $current_number) == 1
Condition83%5/6
defined $next_number and $next_number - $current_number == 1 || $current_number - $next_number == 1
!ll&&!rl&&r
02723
$next_number - $current_number == 1 or $current_number - $next_number == 1
l!l&&r!l&&!r
19427
MC/DC66%2/3
defined $next_number and $next_number - $current_number == 1 || $current_number - $next_number == 1
defined $next_number $next_number - $current_number == 1 $current_number - $next_number == 1
Truth table75%3/4
A and B or C
A= defined $next_numberB= $next_number - $current_number == 1C= $current_number - $next_number == 1
ABCresult
0XX0
1000
11X1
1011
109 || ($current_number - $next_number) == 1)) {
110 23 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 10 13 23
111 10 push @lists, $current_number;
112 } else { # blissfully do nothing when scalar context
113 }
114 23 $constructed{middle} = 1;
115 # end processing current continuation
116 } else {
117 27 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 12 15 27
118 12 push @lists, $current_number;
119 12 push @output, [ @lists ];
120 12 undef @lists;
121 } else {
122 15 $output .= $opts->{range}.$current_number;
123 15 ▶ $output .= $opts->{delimiters}->[-1] if $opts->{delimiters}->[-1];
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"delimiters"}[-1] 3 12 15
124 15 ▶ $output .= "$opts->{separator} " unless $i == $#nums;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $#nums 12 3 15
125 }
126 27 $reset = 1;
127 27 $constructed{end} = 1;
128 }
129 }
130  
131 7 ▶ return wantarray ? @output : $output;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 4 7
132 }
133  
134 sub _validate
135 {
136 7 ▶ my ($set) = @_;
Subroutine _validate: called CC=4 SCAR=14.5
137  
138 7 ▶ croak 'continuation(): empty set provided' unless defined $set;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set 0 7 7
139  
140 7 my $RE_valid = qr{(?:[\d\-]+\ ?)+};
141 7 ▶ 1 while $set =~ /\G$RE_valid/gc;
Branch: 1 branch
BranchTrueFalseTotal
$set =~ /\G$RE_valid/cg and do { '???' } 7 7 14
142 7 ▶ unless ($set =~ /\G$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($set =~ /\G$/) 0 7 7
143 0 croak "continuation(): invalid set provided: '$set`";
144 }
145 }
146  
147 4 1;
148 __END__
↑ Summary Build »