blib/lib/Pinto/Editor/Clip.pm

filter: Statementstmt 10.4% 13 / 125 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 5.0% 1 / 20 Podpod 0.0% 0 / 13 totaltotal 5.6% 14 / 246 CC 41 SCAR 40.2
CC
41
Cov
6%
slurp
156.0
_parse_slurp
72.0
split
72.0
CRAP 55.9
« blib/lib/App/Pinto.pm ↑ Summary blib/lib/Pinto/Schema/Result/Distribution.pm »
1 # ABSTRACT: Internal class for Pinto::Editor
2  
3 package Pinto::Editor::Clip;
4  
5 66 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #-----------------------------------------------------------------------------
8  
9 66 our $VERSION = '0.14'; # VERSION
10  
11 #-----------------------------------------------------------------------------
12  
13 66 has data => qw/ reader data writer _data required 1 /;
14 66 has [qw/ start head tail mhead mtail /] => qw/ is rw required 1 isa Int default 0 /;
15 66 has _parent => qw/ is ro isa Maybe[Pinto::Editor::Clip] init_arg parent /;
16  
17 66 has found => qw/ is ro required 1 isa Str /, default => '';
18 66 has content => qw/ is ro required 1 isa Str /, default => '';
19 0 ▶has _matched => qw/ init_arg matched is ro isa ArrayRef /, default => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20 0 ▶sub matched { return @{ $_[0]->matched } }
Subroutine matched: not called CC=1 SCAR=6.9
Pod undocumented
21 66 has matcher => qw/ is ro /, default => undef;
22  
23 66 has default => qw/ is ro lazy_build 1 isa HashRef /;
24  
25 #-----------------------------------------------------------------------------
26  
27 0 ▶sub _build_default { {
Subroutine _build_default: not called CC=1 SCAR=6.9
28 0 slurp => '[)',
29 } }
30  
31 #-----------------------------------------------------------------------------
32  
33 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
34 0 my $self = shift;
35 0 my $data = $self->data;
36 0 ▶ if ( ref $data ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "SCALAR") 0 0 0
37 0 chomp $data;
38 0 ▶ $data .= "\n" if length $data;
Branch: 1 branch
BranchTrueFalseTotal
if length $data 0 0 0
39 0 $self->_data( \$data );
40 }
41 }
42  
43 #-----------------------------------------------------------------------------
44  
45 0 ▶sub _fhead ($$) {
Subroutine _fhead: not called CC=2 SCAR=17.9
46 0 my ( $data, $from ) = @_;
47 0 my $i0 = rindex $$data, "\n", $from;
48 0 ▶ return $i0 + 1 unless -1 == $i0;
Branch: 1 branch
BranchTrueFalseTotal
unless -1 == $i0 0 0 0
49 0 return 0;
50 }
51  
52 #-----------------------------------------------------------------------------
53  
54 0 ▶sub _ftail ($$) {
Subroutine _ftail: not called CC=2 SCAR=17.9
55 0 my ( $data, $from ) = @_;
56 0 my $i0 = index $$data, "\n", $from;
57 0 ▶ return $i0 unless -1 == $i0;
Branch: 1 branch
BranchTrueFalseTotal
unless -1 == $i0 0 0 0
58 0 return -1 + length $$data;
59 }
60  
61 #-----------------------------------------------------------------------------
62  
63 0 ▶sub parent {
Subroutine parent: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my $self = shift;
65 0 ▶ if ( my $parent = $self->_parent ) { return $parent }
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->_parent) 0 0 0
66 0 return $self; # We are the base (root) split
67 }
68  
69 #-----------------------------------------------------------------------------
70  
71 0 ▶sub is_root {
Subroutine is_root: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my $self = shift;
73 0 return ! $self->_parent;
74 }
75  
76 #-----------------------------------------------------------------------------
77  
78 0 ▶sub _strip_edness ($) {
Subroutine _strip_edness: not called CC=5 SCAR=34.0
79 0 my $slurp = $_[0];
80 $slurp->{chomp} = delete $slurp->{chomped} if
81 0 ▶ exists $slurp->{chomped} && not exists $slurp->{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if exists $slurp->{"chomped"} and not exists $slurp->{"chomp"} 0 0 0
Condition0%0/3
exists $slurp->{"chomped"} and not exists $slurp->{"chomp"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $slurp->{"chomped"} and not exists $slurp->{"chomp"}
exists $slurp->{"chomped"} not exists $slurp->{"chomp"}
Truth table0%0/3
A and not(B)
A= exists $slurp->{"chomped"}B= not exists $slurp->{"chomp"}
ABresult
0X0
100
111
82 $slurp->{trim} = delete $slurp->{trimmed} if
83 0 ▶ exists $slurp->{trimmed} && not exists $slurp->{trim};
Branch: 1 branch
BranchTrueFalseTotal
if exists $slurp->{"trimmed"} and not exists $slurp->{"trim"} 0 0 0
Condition0%0/3
exists $slurp->{"trimmed"} and not exists $slurp->{"trim"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $slurp->{"trimmed"} and not exists $slurp->{"trim"}
exists $slurp->{"trimmed"} not exists $slurp->{"trim"}
Truth table0%0/3
A and not(B)
A= exists $slurp->{"trimmed"}B= not exists $slurp->{"trim"}
ABresult
0X0
100
111
84 }
85  
86 #-----------------------------------------------------------------------------
87  
88 0 ▶sub _parse_slurp ($@) {
Subroutine _parse_slurp: not called CC=8 SCAR=42.8
89 0 my $slurp = shift;
90 0 my %slurp = @_; # Can/will be overidden
91  
92 0 _strip_edness \%slurp;
93  
94 0 ▶ if ( ref $slurp eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $slurp eq 'HASH') { } 0 0 0
95 0 $slurp = { %$slurp };
96 0 _strip_edness $slurp;
97 0 %slurp = ( %slurp, %$slurp );
98 }
99 else {
100 0 ▶ $slurp =~
Branch: 1 branch
BranchTrueFalseTotal
$slurp =~ m<^ ([\@\$])? ([\(\[]) ([\)\]]) (/)? >x or die "Invalid slurp pattern ($slurp)" 0 0 0
101 m{^
102 ([\@\$])?
103 ([\(\[])
104 ([\)\]])
105 (/)?
106 }x or die "Invalid slurp pattern ($slurp)";
107  
108 0 ▶ $slurp{wantlist} = $1 eq '@' ? 1 : 0 if $1;
Branch: 2 branches
BranchTrueFalseTotal
if $1 0 0 0
$1 eq '@' ? : 0 0 0
109 0 ▶ $slurp{slurpl} = $2 eq '[' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '[' ? : 0 0 0
110 0 ▶ $slurp{slurpr} = $3 eq ']' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq ']' ? : 0 0 0
111 0 ▶ $slurp{chomp} = 1 if $4;
Branch: 1 branch
BranchTrueFalseTotal
if $4 0 0 0
112 }
113  
114 0 return %slurp;
115 }
116  
117 #-----------------------------------------------------------------------------
118  
119 0 ▶sub find {
Subroutine find: not called CC=1 SCAR=6.9
Pod undocumented
120 0 return shift->split( @_ );
121 }
122  
123 #-----------------------------------------------------------------------------
124  
125 0 ▶sub split {
Subroutine split: not called CC=8 SCAR=42.8
Pod undocumented
126 0 my $self = shift;
127 0 my $matcher;
128 0 ▶ $matcher = shift if @_ % 2; # Odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
129 0 my %given = @_;
130  
131 0 my $data = $self->data;
132 0 my $length = length $$data;
133 0 ▶ return unless $length; # Nothing to split
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 0 0
134  
135 0 ▶ my $from = $self->_parent ? $self->tail + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_parent ? : 0 0 0
136 0 ▶ return if $length <= $from; # Was already at end of data
Branch: 1 branch
BranchTrueFalseTotal
if $length <= $from 0 0 0
137  
138 0 pos $data = $from;
139 0 ▶ return unless $$data =~ m/\G[[:ascii:]]*?($matcher)/mgc;
Branch: 1 branch
BranchTrueFalseTotal
unless $$data =~ /\G[[:ascii:]]*?($matcher)/cgm 0 0 0
140 0 my @match = map { substr $$data, $-[$_], $+[$_] - $-[$_] } ( 0 .. -1 + scalar @- );
141 0 shift @match;
142 0 my $found = shift @match;
143 0 my ( $mhead, $mtail ) = ( $-[1], $+[1] - 1 );
144  
145 0 my $head = _fhead $data, $mhead;
146 0 my $tail = _ftail $data, $mtail;
147  
148 # TODO This is hacky
149 0 my @matched = @match;
150  
151 0 my $content = substr $$data, $head, 1 + $tail - $head;
152  
153 0 my $split = __PACKAGE__->new(
154 data => $data, parent => $self,
155 start => $from, mhead => $mhead, mtail => $mtail, head => $head, tail => $tail,
156 matcher => $matcher, found => $found, matched => \@matched,
157 content => $content,
158 default => $self->default,
159 );
160  
161 0 ▶ return $split unless wantarray && ( my $slurp = delete $given{slurp} );
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray and my $slurp = delete $given{"slurp"} 0 0 0
Condition0%0/3
wantarray and my $slurp = delete $given{"slurp"}
!ll&&!rl&&r
000
MC/DC0%0/2
wantarray and my $slurp = delete $given{"slurp"}
wantarray my $slurp = delete $given{"slurp"}
Truth table0%0/3
A and B
A= wantarrayB= my $slurp = delete $given{"slurp"}
ABresult
0X0
100
111
162 0 return ( $split, $split->slurp( $slurp, %given ) );
163 }
164  
165 #-----------------------------------------------------------------------------
166  
167 0 ▶sub slurp {
Subroutine slurp: not called CC=12 SCAR=50.5
Pod undocumented
168 0 my $self = shift;
169 0 my $slurp = 1;
170 0 ▶ $slurp = shift if @_ % 2; # Odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
171 0 my %given = @_;
172  
173 0 my $split = $self;
174  
175 0 _strip_edness \%given;
176 0 my %slurp = _parse_slurp $self->default->{slurp};
177 0 ▶ exists $given{$_} and $slurp{$_} = $given{$_} for qw/ chomp trim /;
Branch: 1 branch
BranchTrueFalseTotal
exists $given{$_} and $slurp{$_} = $given{$_} 0 0 0
178 0 ▶ %slurp = _parse_slurp $slurp, %slurp unless $slurp eq 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $slurp eq 1 0 0 0
179  
180 0 my @content;
181 0 ▶ push @content, $self->parent->content if $slurp{slurpl};
Branch: 1 branch
BranchTrueFalseTotal
if $slurp{"slurpl"} 0 0 0
182 0 push @content, $split->preceding;
183 0 ▶ push @content, $split->content if $slurp{slurpr};
Branch: 1 branch
BranchTrueFalseTotal
if $slurp{"slurpr"} 0 0 0
184  
185 0 my $content = join '', @content;
186 0 ▶ if ( $slurp{trim} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurp{"trim"}) 0 0 0
187 0 s/^\s*//, s/\s*$//, for $content;
188 }
189  
190 0 ▶ if ( wantarray && $slurp{wantlist} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and $slurp{'wantlist'}) { } 0 0 0
Condition0%0/3
wantarray and $slurp{"wantlist"}
!ll&&!rl&&r
000
MC/DC0%0/2
wantarray and $slurp{"wantlist"}
wantarray $slurp{"wantlist"}
Truth table0%0/3
A and B
A= wantarrayB= $slurp{"wantlist"}
ABresult
0X0
100
111
191 0 @content = grep { $_ ne "\n" } split m/(\n)/, $content;
192 0 ▶ @content = map { "$_\n" } @content unless $slurp{chomp};
Branch: 1 branch
BranchTrueFalseTotal
unless $slurp{"chomp"} 0 0 0
193 0 return @content;
194 }
195 else {
196 0 return $content;
197 }
198 }
199  
200 #-----------------------------------------------------------------------------
201  
202 0 ▶sub preceding {
Subroutine preceding: not called CC=2 SCAR=17.9
Pod undocumented
203 0 my $self = shift;
204  
205 0 my $data = $self->data;
206 0 my $length = $self->head - $self->start;
207 0 ▶ return '' unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 0 0
208 0 return substr $$data, $self->start, $length;
209 }
210  
211 #-----------------------------------------------------------------------------
212  
213 0 ▶sub pre { return shift->preceding( @_ ) }
Subroutine pre: not called CC=1 SCAR=6.9
Pod undocumented
214  
215 #-----------------------------------------------------------------------------
216  
217 0 ▶sub remaining {
Subroutine remaining: not called CC=3 SCAR=24.8
Pod undocumented
218 0 my $self = shift;
219  
220 0 my $data = $self->data;
221 0 ▶ return $$data if $self->is_root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 0 0 0
222  
223 0 my $from = $self->tail + 1;
224  
225 0 my $length = length( $$data ) - $from + 1;
226 0 ▶ return '' unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 0 0
227 0 return substr $$data, $from, $length;
228 }
229  
230 #-----------------------------------------------------------------------------
231  
232 0 ▶sub re { return shift->remaining( @_ ) }
Subroutine re: not called CC=1 SCAR=6.9
Pod undocumented
233  
234 #-----------------------------------------------------------------------------
235  
236 0 ▶sub match {
Subroutine match: not called CC=2 SCAR=17.9
Pod undocumented
237 0 my $self = shift;
238 0 my $ii = shift;
239 0 ▶ return $self->found if $ii == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ii == -1 0 0 0
240 0 return $self->_matched->[$ii];
241 }
242  
243 #-----------------------------------------------------------------------------
244  
245 0 ▶sub is {
Subroutine is: not called CC=3 SCAR=24.8
Pod undocumented
246 0 my $self = shift;
247 0 my $ii = shift;
248 0 my $is = shift;
249  
250 0 ▶ return unless defined ( my $match = $self->match( $ii ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $match = $self->match($ii)) 0 0 0
251 0 ▶ if ( ref $is eq 'Regexp' ) { $match =~ $is }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $is eq 'Regexp') { } 0 0 0
252 0 else { return $match eq $is }
253 }
254  
255 #-----------------------------------------------------------------------------
256 66 1;
257  
258 =pod
259  
260 =encoding UTF-8
261  
262 =for :stopwords Jeffrey Ryan Thalhammer
263  
264 =head1 NAME
265  
266 Pinto::Editor::Clip - Internal class for Pinto::Editor
267  
268 =head1 VERSION
269  
270 version 0.14
271  
272 =head1 DESCRIPTION
273  
274 This is a forked version of L<Text::Clip> which does not use the deprecated
275 module L<Any::Moose>. My thanks to Robert Krimen for authoring the original.
276 No user-servicable parts in here.
277  
278 =head1 AUTHOR
279  
280 Jeffrey Ryan Thalhammer <jeff@stratopan.com>
281  
282 =head1 COPYRIGHT AND LICENSE
283  
284 This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
285  
286 This is free software; you can redistribute it and/or modify it under
287 the same terms as the Perl 5 programming language system itself.
288  
289 =cut
290  
291 __END__
« blib/lib/App/Pinto.pm ↑ Summary blib/lib/Pinto/Schema/Result/Distribution.pm »