| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | my $input = ref $_[0] eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | ? join ' ', @{$_[0]} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} ||= ""
MC/DC100%1/1
$opts->{"delimiter"} ||= ""
$opts->{"delimiter"}
Truth table100%2/2
A
A= $opts->{"delimiter"}
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | $opts->{range} ||= '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"range"} ||= "-"
MC/DC100%1/1
$opts->{"range"} ||= "-"
$opts->{"range"}
Truth table100%2/2
A
A= $opts->{"range"}
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | ▶ | $opts->{separator} ||= ','; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"separator"} ||= ","
MC/DC100%1/1
$opts->{"separator"} ||= ","
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | # handy variables | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 97 | ▶ | my $prev_number = $nums[$i-1] || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$nums[$i - 1] || 0
MC/DC0%0/1
$nums[$i - 1] || 0
$nums[$i - 1]
Truth table50%1/2
A
A= $nums[$i - 1]
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 97 | ▶ | my $current_number = $nums[$i ] || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$nums[$i] || 0
MC/DC0%0/1
$nums[$i] || 0
$nums[$i]
Truth table50%1/2
A
A= $nums[$i]
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 97 | ▶ | my $next_number = $nums[$i+1] || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$nums[$i + 1] || 0
MC/DC100%1/1
$nums[$i + 1] || 0
$nums[$i + 1]
Truth table100%2/2
A
A= $nums[$i + 1]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$constructed{"begin"} and $constructed{"middle"} and $constructed{"end"}
$constructed{"begin"} and $constructed{"middle"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # previous number greater than current one | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 97 | ▶ | if ($prev_number > $current_number && $i != 0 && !$have_neg_continuation) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$prev_number > $current_number and $i != 0 and not $have_neg_continuation
$prev_number > $current_number and $i != 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | # previous number *exactly* greater 1 | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | if ($prev_number - $current_number == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (@lists) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if (@lists) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | push @output, [ @lists ]; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | undef @lists; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | $output .= "$opts->{separator} " unless $prev_continuation; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # reset processing continuation state | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | $reset = 1; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # processing new continuation | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 97 | ▶ | if ($reset) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 47 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 22 | push @lists, $nums[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 22 | ▶ | push @output, [ @lists ] if $i == $#nums; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 25 | ▶ | $output .= $opts->{delimiters}->[0] if $opts->{delimiters}->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 25 | $output .= $nums[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 47 | ▶ | if (($next_number - $current_number) > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 14 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | ▶ | if (@lists) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$next_number - $current_number == 1 or $current_number - $next_number == 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | || ($current_number - $next_number) == 1)) { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 23 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 15 | ▶ | $output .= "$opts->{separator} " unless $i == $#nums; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 27 | $reset = 1; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 27 | $constructed{end} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 7 | ▶ | return wantarray ? @output : $output; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 7 | my $RE_valid = qr{(?:[\d\-]+\ ?)+}; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 7 | ▶ | 1 while $set =~ /\G$RE_valid/gc; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 7 | ▶ | unless ($set =~ /\G$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | croak "continuation(): invalid set provided: '$set`"; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||||||||||