| 1 | package DTL::Fast::Filter::Slice; | ||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; use utf8; use warnings FATAL => 'all'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 3 | ▶ | use parent 'DTL::Fast::Filter'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 3 | $DTL::Fast::FILTER_HANDLERS{'slice'} = __PACKAGE__; | |||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 3 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | #@Override | ||||||||||||||||||||||
| 10 | sub parse_parameters | ||||||||||||||||||||||
| 11 | { | ||||||||||||||||||||||
| 12 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
parse_parameters: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||
| 13 | die $self->get_parse_error("no slicing settings specified") | ||||||||||||||||||||||
| 14 | 24 | ▶ | if not scalar @{$self->{'parameter'}}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 15 | 23 | $self->{'settings'} = $self->{'parameter'}->[0]; | |||||||||||||||||||||
| 16 | 23 | return $self; | |||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | #@Override | ||||||||||||||||||||||
| 20 | sub filter | ||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||
| 22 | 26 | ▶ | my ($self, $filter_manager, $value, $context) = @_; | ||||||||||||||||||||
|
Subroutine
filter: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | 26 | my $settings = $self->{'settings'}->render($context); | |||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 26 | ▶ | die $self->get_render_error( $context, 'slicing format is not defined in current context') | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 27 | if not defined $settings; | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 25 | my $value_type = reftype $value; | |||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 25 | ▶ | if ( not defined $value_type ) | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||
| 33 | 12 | ▶ | if ( defined $value ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||
| 35 | 11 | eval{ | |||||||||||||||||||||
| 36 | 11 | $value = $self->slice_scalar($value, $settings); | |||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||
| 38 | 11 | ▶ | die $self->get_render_error($context, $@) if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||
| 40 | else | ||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||
| 42 | 1 | die $self->get_render_error( $context, 'unable to slice undef value'); | |||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||
| 45 | elsif( $value_type eq 'ARRAY' ) | ||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||
| 47 | eval | ||||||||||||||||||||||
| 48 | 11 | { | |||||||||||||||||||||
| 49 | 11 | $value = $self->slice_array($value, $settings); | |||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||
| 51 | 11 | ▶ | die $self->get_render_error($context, $@) if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||
| 53 | elsif( $value_type eq 'HASH' ) | ||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||
| 55 | 1 | $value = $self->slice_hash($value, $settings); | |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | elsif( $value_type eq 'SCALAR' ) | ||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||
| 59 | 0 | $value = $self->slice_scalar($$value, $settings); | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | else | ||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||
| 63 | 1 | ▶ | die $self->get_render_error( | ||||||||||||||||||||
|
Condition50%1/2
ref $value || 'SCALAR'
MC/DC0%0/1
ref $value || 'SCALAR'
ref $value
Truth table50%1/2
A
A= ref $value
| |||||||||||||||||||||||
| 64 | $context | ||||||||||||||||||||||
| 65 | , sprintf( | ||||||||||||||||||||||
| 66 | "can slice only HASH, ARRAY or SCALAR values, not %s (%s)" | ||||||||||||||||||||||
| 67 | , $value_type | ||||||||||||||||||||||
| 68 | , ref $value || 'SCALAR' | ||||||||||||||||||||||
| 69 | ) | ||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | 22 | return $value; | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | sub slice_scalar | ||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||
| 78 | 11 | ▶ | my ($self, $scalar, $settings ) = @_; | ||||||||||||||||||||
|
Subroutine
slice_scalar: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | 11 | my ($start, $end) = $self->parse_indexes($settings, length($scalar) - 1 ); | |||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | 10 | return substr $scalar, $start, $end - $start + 1; | |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | sub slice_array | ||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||
| 87 | 11 | ▶ | my ($self, $array, $settings ) = @_; | ||||||||||||||||||||
|
Subroutine
slice_array: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 11 | my ($start, $end) = $self->parse_indexes($settings, $#$array); | |||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 11 | return [@{$array}[$start .. $end]]; | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | sub slice_hash | ||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||
| 97 | 1 | ▶ | my ($self, $hash, $settings) = @_; | ||||||||||||||||||||
|
Subroutine
slice_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 98 | 1 | return [@{$hash}{(split /\s*,\s*/, $settings)}]; | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | sub parse_indexes | ||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||
| 103 | 22 | ▶ | my ($self, $settings, $last_index) = @_; | ||||||||||||||||||||
|
Subroutine
parse_indexes: called CC=11 SCAR=26.2
Pod
undocumented
| |||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 22 | my $start = 0; | |||||||||||||||||||||
| 106 | 22 | my $end; | |||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | 22 | ▶ | if( $settings =~ /^([-\d]+)?\:([-\d]+)?$/ ) # python's format | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||
| 110 | 12 | ▶ | $start = $self->python_index_map($1, $last_index) // $start; | ||||||||||||||||||||
|
Condition66%2/3
$self->python_index_map($1, $last_index) // $start
MC/DC50%1/2
$self->python_index_map($1, $last_index) // $start
$self->python_index_map($1, $last_index)
$start
Truth table66%2/3
A // B
A= $self->python_index_map($1, $last_index)B= $start
| |||||||||||||||||||||||
| 111 | 12 | ▶ | $end = defined $2 ? | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 112 | $self->python_index_map($2, $last_index) - 1 | ||||||||||||||||||||||
| 113 | : $last_index; | ||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||
| 115 | elsif( $settings =~ /^([-\d]+)?\s*\.\.\s*([-\d]+)?$/ ) # perl's format | ||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||
| 117 | 9 | ▶ | $start = $1 // $start; | ||||||||||||||||||||
|
Condition33%1/3
$1 // $start
MC/DC0%0/2
$1 // $start
$1
$start
Truth table33%1/3
A // B
A= $1B= $start
| |||||||||||||||||||||||
| 118 | 9 | ▶ | $end = $2 // $last_index; | ||||||||||||||||||||
|
Condition33%1/3
$2 // $last_index
MC/DC0%0/2
$2 // $last_index
$2
$last_index
Truth table33%1/3
A // B
A= $2B= $last_index
| |||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | else | ||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||
| 122 | 1 | ▶ | die sprintf( | ||||||||||||||||||||
|
Condition50%1/2
$settings // undef
MC/DC0%0/1
$settings // undef
$settings
Truth table50%1/2
A
A= $settings
| |||||||||||||||||||||||
| 123 | "array slicing option may be specified in one of the following formats:\npython: [from_index]:[to_index+1]\n perl: [from_index]..[to_index]\ngot `%s` instead.\n" | ||||||||||||||||||||||
| 124 | , $settings // undef | ||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | 21 | ▶ | $start = $last_index if $start > $last_index; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 129 | 21 | ▶ | $end = $last_index if $end > $last_index; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 21 | ▶ | if ( $start > $end ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 132 | 0 | my $var = $start; | |||||||||||||||||||||
| 133 | 0 | $start = $end; | |||||||||||||||||||||
| 134 | 0 | $end = $var; | |||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | 21 | return ($start, $end); | |||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | sub python_index_map | ||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||
| 142 | 19 | ▶ | my( $self, $pyvalue, $lastindex ) = @_; | ||||||||||||||||||||
|
Subroutine
python_index_map: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | 19 | ▶ | return $pyvalue if not defined $pyvalue; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | 14 | ▶ | return $pyvalue < 0 ? | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 147 | $lastindex + $pyvalue + 1 | ||||||||||||||||||||||
| 148 | : $pyvalue; | ||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | 3 | 1; | |||||||||||||||||||||