blib/lib/Text/Split.pm

filter: Statementstmt 85.6% 107 / 125 Branchbran 73.5% 50 / 68 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 80.9% 17 / 21 Podpod 7.6% 1 / 13 totaltotal 75.7% 187 / 247 CC 41 SCAR 18.7
CC
41
Cov
80%
slurp
12.0
is
12.0
_parse_slurp
8.7
CRAP 6.5
↑ Summary
1 package Text::Split;
2 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 $Text::Split::VERSION = '0.0013';
4 }
5 # ABSTRACT: Text splitting with fine-grained control
6  
7  
8 4 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 has data => qw/ reader data writer _data required 1 /;
11 4 has [qw/ start head tail mhead mtail /] => qw/ is rw required 1 isa Int default 0 /;
12 4 has _parent => qw/ is ro isa Maybe[Text::Split] init_arg parent /;
13  
14 4 has found => qw/ is ro required 1 isa Str /, default => '';
15 4 has content => qw/ is ro required 1 isa Str /, default => '';
16 7 ▶has _matched => qw/ init_arg matched is ro isa ArrayRef /, default => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
17 0 ▶sub matched { return @{ $_[0]->matched } }
Subroutine matched: not called CC=1 SCAR=6.9
Pod undocumented
18 4 has matcher => qw/ is ro /, default => undef;
19  
20 4 has default => qw/ is ro lazy_build 1 isa HashRef /;
21 7 ▶sub _build_default { {
Subroutine _build_default: called CC=1 SCAR=0.0
22 7 slurp => '[)',
23 } }
24  
25 22 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
26 22 my $self = shift;
27 22 my $data = $self->data;
28 22 ▶ if ( ref $data ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "SCALAR") 7 15 22
29 7 chomp $data;
30 7 ▶ $data .= "\n" if length $data;
Branch: 1 branch
BranchTrueFalseTotal
if length $data 7 0 7
31 7 $self->_data( \$data );
32 }
33 }
34  
35 15 ▶sub _fhead ($$) {
Subroutine _fhead: called CC=2 SCAR=7.6
36 15 my ( $data, $from ) = @_;
37 15 my $i0 = rindex $$data, "\n", $from;
38 15 ▶ return $i0 + 1 unless -1 == $i0;
Branch: 1 branch
BranchTrueFalseTotal
unless -1 == $i0 15 0 15
39 0 return 0;
40 }
41  
42 15 ▶sub _ftail ($$) {
Subroutine _ftail: called CC=2 SCAR=7.6
43 15 my ( $data, $from ) = @_;
44 15 my $i0 = index $$data, "\n", $from;
45 15 ▶ return $i0 unless -1 == $i0;
Branch: 1 branch
BranchTrueFalseTotal
unless -1 == $i0 15 0 15
46 0 return -1 + length $$data;
47 }
48  
49 11 ▶sub parent {
Subroutine parent: called CC=2 SCAR=7.6
Pod undocumented
50 11 my $self = shift;
51 11 ▶ if ( my $parent = $self->_parent ) { return $parent }
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->_parent) 11 0 11
52 0 return $self; # We are the base (root) split
53 }
54  
55 5 ▶sub is_root {
Subroutine is_root: called CC=1 SCAR=0.0
Pod undocumented
56 5 my $self = shift;
57 5 return ! $self->_parent;
58 }
59  
60 35 ▶sub _strip_edness ($) {
Subroutine _strip_edness: called CC=5 SCAR=17.5
61 35 my $slurp = $_[0];
62 $slurp->{chomp} = delete $slurp->{chomped} if
63 35 ▶ exists $slurp->{chomped} && not exists $slurp->{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if exists $slurp->{"chomped"} and not exists $slurp->{"chomp"} 0 35 35
Condition33%1/3
exists $slurp->{"chomped"} and not exists $slurp->{"chomp"}
!ll&&!rl&&r
3500
MC/DC0%0/2
exists $slurp->{"chomped"} and not exists $slurp->{"chomp"}
exists $slurp->{"chomped"} not exists $slurp->{"chomp"}
Truth table33%1/3
A and not(B)
A= exists $slurp->{"chomped"}B= not exists $slurp->{"chomp"}
ABresult
0X0
100
111
64 $slurp->{trim} = delete $slurp->{trimmed} if
65 35 ▶ exists $slurp->{trimmed} && not exists $slurp->{trim};
Branch: 1 branch
BranchTrueFalseTotal
if exists $slurp->{"trimmed"} and not exists $slurp->{"trim"} 2 33 35
Condition66%2/3
exists $slurp->{"trimmed"} and not exists $slurp->{"trim"}
!ll&&!rl&&r
3302
MC/DC50%1/2
exists $slurp->{"trimmed"} and not exists $slurp->{"trim"}
exists $slurp->{"trimmed"} not exists $slurp->{"trim"}
Truth table66%2/3
A and not(B)
A= exists $slurp->{"trimmed"}B= not exists $slurp->{"trim"}
ABresult
0X0
100
111
66 }
67  
68 22 ▶sub _parse_slurp ($@) {
Subroutine _parse_slurp: called CC=8 SCAR=21.6
69 22 my $slurp = shift;
70 22 my %slurp = @_; # Can/will be overidden
71  
72 22 _strip_edness \%slurp;
73  
74 22 ▶ if ( ref $slurp eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $slurp eq 'HASH') { } 0 22 22
75 0 $slurp = { %$slurp };
76 0 _strip_edness $slurp;
77 0 %slurp = ( %slurp, %$slurp );
78 }
79 else {
80 22 ▶ $slurp =~
Branch: 1 branch
BranchTrueFalseTotal
$slurp =~ m<^ ([\@\$])? ([\(\[]) ([\)\]]) (/)? >x or die "Invalid slurp pattern ($slurp)" 22 0 22
81 m{^
82 ([\@\$])?
83 ([\(\[])
84 ([\)\]])
85 (/)?
86 }x or die "Invalid slurp pattern ($slurp)";
87  
88 22 ▶ $slurp{wantlist} = $1 eq '@' ? 1 : 0 if $1;
Branch: 2 branches
BranchTrueFalseTotal
if $1 5 17 22
$1 eq '@' ? : 4 1 5
89 22 ▶ $slurp{slurpl} = $2 eq '[' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '[' ? : 20 2 22
90 22 ▶ $slurp{slurpr} = $3 eq ']' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq ']' ? : 3 19 22
91 22 ▶ $slurp{chomp} = 1 if $4;
Branch: 1 branch
BranchTrueFalseTotal
if $4 0 22 22
92 }
93  
94 22 return %slurp;
95 }
96  
97 12 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod undocumented
98 12 return shift->split( @_ );
99 }
100  
101 16 ▶sub split {
Subroutine split: called CC=8 SCAR=20.9
Pod undocumented
102 16 my $self = shift;
103 16 my $matcher;
104 16 ▶ $matcher = shift if @_ % 2; # Odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 16 0 16
105 16 my %given = @_;
106  
107 16 my $data = $self->data;
108 16 my $length = length $$data;
109 16 ▶ return unless $length; # Nothing to split
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 16 16
110  
111 16 ▶ my $from = $self->_parent ? $self->tail + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_parent ? : 9 7 16
112 16 ▶ return if $length <= $from; # Was already at end of data
Branch: 1 branch
BranchTrueFalseTotal
if $length <= $from 1 15 16
113  
114 15 pos $data = $from;
115 15 ▶ return unless $$data =~ m/\G[[:ascii:]]*?($matcher)/mgc;
Branch: 1 branch
BranchTrueFalseTotal
unless $$data =~ /\G[[:ascii:]]*?($matcher)/cgm 0 15 15
116 15 my @match = map { substr $$data, $-[$_], $+[$_] - $-[$_] } ( 0 .. -1 + scalar @- );
117 15 shift @match;
118 15 my $found = shift @match;
119 15 my ( $mhead, $mtail ) = ( $-[1], $+[1] - 1 );
120  
121 15 my $head = _fhead $data, $mhead;
122 15 my $tail = _ftail $data, $mtail;
123  
124 # TODO This is hacky
125 15 my @matched = @match;
126  
127 15 my $content = substr $$data, $head, 1 + $tail - $head;
128  
129 15 my $split = __PACKAGE__->new(
130 data => $data, parent => $self,
131 start => $from, mhead => $mhead, mtail => $mtail, head => $head, tail => $tail,
132 matcher => $matcher, found => $found, matched => \@matched,
133 content => $content,
134 default => $self->default,
135 );
136  
137 15 ▶ return $split unless wantarray && ( my $slurp = delete $given{slurp} );
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray and my $slurp = delete $given{"slurp"} 12 3 15
Condition66%2/3
wantarray and my $slurp = delete $given{"slurp"}
!ll&&!rl&&r
1203
MC/DC50%1/2
wantarray and my $slurp = delete $given{"slurp"}
wantarray my $slurp = delete $given{"slurp"}
Truth table66%2/3
A and B
A= wantarrayB= my $slurp = delete $given{"slurp"}
ABresult
0X0
100
111
138 3 return ( $split, $split->slurp( $slurp, %given ) );
139 }
140  
141 13 ▶sub slurp {
Subroutine slurp: called CC=12 SCAR=24.9
Pod undocumented
142 13 my $self = shift;
143 13 my $slurp = 1;
144 13 ▶ $slurp = shift if @_ % 2; # Odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 9 4 13
145 13 my %given = @_;
146  
147 13 my $split = $self;
148  
149 13 _strip_edness \%given;
150 13 my %slurp = _parse_slurp $self->default->{slurp};
151 13 ▶ exists $given{$_} and $slurp{$_} = $given{$_} for qw/ chomp trim /;
Branch: 1 branch
BranchTrueFalseTotal
exists $given{$_} and $slurp{$_} = $given{$_} 8 18 26
152 13 ▶ %slurp = _parse_slurp $slurp, %slurp unless $slurp eq 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $slurp eq 1 9 4 13
153  
154 13 my @content;
155 13 ▶ push @content, $self->parent->content if $slurp{slurpl};
Branch: 1 branch
BranchTrueFalseTotal
if $slurp{"slurpl"} 11 2 13
156 13 push @content, $split->preceding;
157 13 ▶ push @content, $split->content if $slurp{slurpr};
Branch: 1 branch
BranchTrueFalseTotal
if $slurp{"slurpr"} 3 10 13
158  
159 13 my $content = join '', @content;
160 13 ▶ if ( $slurp{trim} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurp{"trim"}) 3 10 13
161 3 s/^\s*//, s/\s*$//, for $content;
162 }
163  
164 13 ▶ if ( wantarray && $slurp{wantlist} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and $slurp{'wantlist'}) { } 4 9 13
Condition100%3/3
wantarray and $slurp{"wantlist"}
!ll&&!rl&&r
364
MC/DC100%2/2
wantarray and $slurp{"wantlist"}
wantarray $slurp{"wantlist"}
Truth table100%3/3
A and B
A= wantarrayB= $slurp{"wantlist"}
ABresult
0X0
100
111
165 4 @content = grep { $_ ne "\n" } split m/(\n)/, $content;
166 0 ▶ @content = map { "$_\n" } @content unless $slurp{chomp};
Branch: 1 branch
BranchTrueFalseTotal
unless $slurp{"chomp"} 0 4 4
167 4 return @content;
168 }
169 else {
170 9 return $content;
171 }
172 }
173  
174 17 ▶sub preceding {
Subroutine preceding: called CC=2 SCAR=6.9
Pod undocumented
175 17 my $self = shift;
176  
177 17 my $data = $self->data;
178 17 my $length = $self->head - $self->start;
179 17 ▶ return '' unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 1 16 17
180 16 return substr $$data, $self->start, $length;
181 }
182 0 ▶sub pre { return shift->preceding( @_ ) }
Subroutine pre: not called CC=1 SCAR=6.9
Pod undocumented
183  
184 5 ▶sub remaining {
Subroutine remaining: called CC=3 SCAR=11.0
Pod undocumented
185 5 my $self = shift;
186  
187 5 my $data = $self->data;
188 5 ▶ return $$data if $self->is_root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 1 4 5
189  
190 4 my $from = $self->tail + 1;
191  
192 4 my $length = length( $$data ) - $from + 1;
193 4 ▶ return '' unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 4 4
194 4 return substr $$data, $from, $length;
195 }
196 0 ▶sub re { return shift->remaining( @_ ) }
Subroutine re: not called CC=1 SCAR=6.9
Pod undocumented
197  
198 4 ▶sub match {
Subroutine match: called CC=2 SCAR=7.0
Pod undocumented
199 4 my $self = shift;
200 4 my $ii = shift;
201 4 ▶ return $self->found if $ii == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ii == -1 0 4 4
202 4 return $self->_matched->[$ii];
203 }
204  
205 0 ▶sub is {
Subroutine is: not called CC=3 SCAR=24.8
Pod undocumented
206 0 my $self = shift;
207 0 my $ii = shift;
208 0 my $is = shift;
209  
210 0 ▶ return unless defined ( my $match = $self->match( $ii ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $match = $self->match($ii)) 0 0 0
211 0 ▶ if ( ref $is eq 'Regexp' ) { $match =~ $is }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $is eq 'Regexp') { } 0 0 0
212 0 else { return $match eq $is }
213 }
214  
215 4 1;
216  
217 __END__
↑ Summary