lib/Template/Plugin/String.pm

filter: Statementstmt 98.2% 172 / 175 Branchbran 80.9% 34 / 42 Conditioncond 68.6% 35 / 51 MC/DCmcdc 37.9% 11 / 29 Subroutinesub 97.1% 34 / 35 Podpod 83.3% 25 / 30 totaltotal 85.9% 311 / 362 CC 46 SCAR 14.0
CC
46
Cov
90%
new
9.0
output_filter
9.0
replace
5.6
CRAP 4.1
« lib/Template/VMethods.pm ↑ Summary lib/Template/Grammar.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::String
4 #
5 # DESCRIPTION
6 # Template Toolkit plugin to implement a basic String object.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2001-2022 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 #============================================================================
18  
19 package Template::Plugin::String;
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use overload q|""| => "text",
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 fallback => 1;
28  
29 1 our $VERSION = '3.106';
30 1 our $ERROR = '';
31  
32 1 *centre = \*center;
33 1 *append = \*push;
34 1 *prepend = \*unshift;
35  
36 #------------------------------------------------------------------------
37  
38 71 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
39 71 my ($class, @args) = @_;
40 71 ▶ my $context = ref $class ? undef : shift(@args);
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 25 46 71
41 71 ▶ my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { };
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[-1] eq 'HASH' ? : 13 58 71
Condition100%3/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
45413
MC/DC100%2/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table100%3/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
42  
43 71 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
25460
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
44  
45 71 ▶ my $text = $config->{ text } // (@args ? shift(@args) : '');
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 62 4 66
Condition66%2/3
$config->{"text"} // (@args ? CORE::shift @args : "")
l!l&&r!l&&!r
5660
MC/DC0%0/2
$config->{"text"} // (@args ? CORE::shift @args : "")
$config->{"text"} (@args ? CORE::shift @args : "")
Truth table66%2/3
A // B
A= $config->{"text"}B= (@args ? CORE::shift @args : "")
ABresult
1X1
011
000
46  
47 # print STDERR "text: [$text]\n";
48 # print STDERR "class: [$class]\n";
49  
50 71 my $self = bless {
51 text => $text,
52 filters => [ ],
53 _CONTEXT => $context,
54 }, $class;
55  
56 71 ▶ my $filter = $config->{ filter } || $config->{ filters };
Condition100%3/3
$config->{'filter'} || $config->{'filters'}
l!l&&r!l&&!r
3563
MC/DC100%2/2
$config->{'filter'} || $config->{'filters'}
$config->{'filter'} $config->{'filters'}
Truth table100%3/3
A || B
A= $config->{'filter'}B= $config->{'filters'}
ABresult
1X1
011
000
57  
58 # install any output filters specified as 'filter' or 'filters' option
59 71 ▶ $self->output_filter($filter)
Branch: 1 branch
BranchTrueFalseTotal
if $filter 8 63 71
60 if $filter;
61  
62 71 return $self;
63 }
64  
65  
66 70 ▶sub text {
Subroutine text: called CC=4 SCAR=13.9
Pod documented
67 70 my $self = shift;
68 70 ▶ return $self->{ text } unless @{ $self->{ filters } };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"filters"};} 60 10 70
69  
70 10 my $text = $self->{ text };
71 10 my $context = $self->{ _CONTEXT };
72  
73 10 foreach my $dispatch (@{ $self->{ filters } }) {
74 18 my ($name, $args) = @$dispatch;
75 18 ▶ my $code = $context->filter($name, $args)
Condition33%1/3
$context->filter($name, $args) || $self->throw($context->error)
l!l&&r!l&&!r
1800
MC/DC0%0/2
$context->filter($name, $args) || $self->throw($context->error)
$context->filter($name, $args) $self->throw($context->error)
Truth table33%1/3
A || B
A= $context->filter($name, $args)B= $self->throw($context->error)
ABresult
1X1
011
000
76 || $self->throw($context->error());
77 18 $text = &$code($text);
78 }
79 10 return $text;
80 }
81  
82  
83 14 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
84 14 my $self = shift;
85 14 $self->new($self->{ text });
86 }
87  
88  
89 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
90 0 my $self = shift;
91  
92 0 die (Template::Exception->new('String', join('', @_)));
93 }
94  
95  
96 #------------------------------------------------------------------------
97 # output_filter($filter)
98 #
99 # Install automatic output filter(s) for the string. $filter can a list:
100 # [ 'name1', 'name2' => [ ..args.. ], name4 => { ..args.. } ] or a hash
101 # { name1 => '', name2 => [ args ], name3 => { args } }
102 #------------------------------------------------------------------------
103  
104 10 ▶sub output_filter {
Subroutine output_filter: called CC=9 SCAR=22.0
Pod undocumented
105 10 my ($self, $filter) = @_;
106 10 my ($name, $args, $dispatch);
107 10 my $filters = $self->{ filters };
108 10 my $count = 0;
109  
110 10 ▶ if (ref $filter eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter eq 'HASH') { } 2 8 10
elsif (ref $filter ne 'ARRAY') { } 6 2 8
111 2 $filter = [ %$filter ];
112 }
113 elsif (ref $filter ne 'ARRAY') {
114 6 $filter = [ split(/\s*\W+\s*/, $filter) ];
115 }
116  
117 10 ▶ while (@$filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$filter) 17 10 27
118 17 $name = shift @$filter;
119  
120 # args may follow as a reference (or empty string, e.g. { foo => '' }
121 17 ▶ if (@$filter && (ref($filter->[0]) || ! length $filter->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$filter and ref $filter->[0] || !CORE::length($filter->[0])) { } 5 12 17
Condition100%6/6
@$filter and ref $filter->[0] || !CORE::length($filter->[0])
!ll&&!rl&&r
845
ref $filter->[0] or not CORE::length $filter->[0]
l!l&&r!l&&!r
324
MC/DC100%3/3
@$filter and ref $filter->[0] || !CORE::length($filter->[0])
@$filter ref $filter->[0] not CORE::length $filter->[0]
Truth table100%4/4
A and B or not(C)
A= @$filterB= ref $filter->[0]C= not CORE::length $filter->[0]
ABCresult
0XX0
1000
11X1
1011
122 5 $args = shift @$filter;
123 5 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 3 2 5
124 3 ▶ $args = [ $args ] unless ref $args eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 3 3
125 }
126 else {
127 2 $args = [ ];
128 }
129 }
130 else {
131 12 $args = [ ];
132 }
133  
134 # $self->DEBUG("adding output filter $name(@$args)\n");
135  
136 17 push(@$filters, [ $name, $args ]);
137 17 $count++;
138 }
139  
140 10 return '';
141 }
142  
143  
144 #------------------------------------------------------------------------
145  
146 6 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
147 6 my $self = shift;
148 6 $self->{ text } .= join('', @_);
149 6 return $self;
150 }
151  
152  
153 2 ▶sub unshift {
Subroutine unshift: called CC=1 SCAR=0.0
Pod documented
154 2 my $self = shift;
155 2 $self->{ text } = join('', @_) . $self->{ text };
156 2 return $self;
157 }
158  
159  
160 1 ▶sub pop {
Subroutine pop: called CC=2 SCAR=7.0
Pod documented
161 1 my $self = shift;
162 1 ▶ my $strip = shift || return $self;
Condition50%1/2
CORE::shift() || (return $self)
l!l
10
MC/DC0%0/1
CORE::shift() || (return $self)
CORE::shift()
Truth table50%1/2
A
A= CORE::shift()
Aresult
11
00
163 1 $self->{ text } =~ s/$strip$//;
164 1 return $self;
165 }
166  
167  
168 1 ▶sub shift {
Subroutine shift: called CC=2 SCAR=7.0
Pod documented
169 1 my $self = shift;
170 1 ▶ my $strip = shift || return $self;
Condition50%1/2
CORE::shift() || (return $self)
l!l
10
MC/DC0%0/1
CORE::shift() || (return $self)
CORE::shift()
Truth table50%1/2
A
A= CORE::shift()
Aresult
11
00
171 1 $self->{ text } =~ s/^$strip//;
172 1 return $self;
173 }
174  
175 #------------------------------------------------------------------------
176  
177 2 ▶sub center {
Subroutine center: called CC=3 SCAR=11.1
Pod documented
178 2 my ($self, $width) = @_;
179 2 my $text = $self->{ text };
180 2 my $len = length $text;
181 2 ▶ $width ||= 0;
Condition50%1/2
$width ||= 0
l!l
20
MC/DC0%0/1
$width ||= 0
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
182  
183 2 ▶ if ($len < $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $width) 2 0 2
184 2 my $lpad = int(($width - $len) / 2);
185 2 my $rpad = $width - $len - $lpad;
186 2 $self->{ text } = (' ' x $lpad) . $self->{ text } . (' ' x $rpad);
187 }
188  
189 2 return $self;
190 }
191  
192  
193 1 ▶sub left {
Subroutine left: called CC=3 SCAR=11.3
Pod documented
194 1 my ($self, $width) = @_;
195 1 my $len = length $self->{ text };
196 1 ▶ $width ||= 0;
Condition50%1/2
$width ||= 0
l!l
10
MC/DC0%0/1
$width ||= 0
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
197  
198 1 ▶ $self->{ text } .= (' ' x ($width - $len))
Branch: 1 branch
BranchTrueFalseTotal
if $width > $len 1 0 1
199 if $width > $len;
200  
201 1 return $self;
202 }
203  
204  
205 1 ▶sub right {
Subroutine right: called CC=3 SCAR=11.3
Pod documented
206 1 my ($self, $width) = @_;
207 1 my $len = length $self->{ text };
208 1 ▶ $width ||= 0;
Condition50%1/2
$width ||= 0
l!l
10
MC/DC0%0/1
$width ||= 0
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
209  
210 $self->{ text } = (' ' x ($width - $len)) . $self->{ text }
211 1 ▶ if $width > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $width > $len 1 0 1
212  
213 1 return $self;
214 }
215  
216  
217 1 ▶sub format {
Subroutine format: called CC=2 SCAR=7.0
Pod documented
218 1 my ($self, $format) = @_;
219 1 ▶ $format //= '%s';
Condition50%1/2
$format //= "%s"
l!l
10
MC/DC0%0/1
$format //= "%s"
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
220 1 $self->{ text } = sprintf($format, $self->{ text });
221 1 return $self;
222 }
223  
224  
225 2 ▶sub filter {
Subroutine filter: called CC=2 SCAR=7.4
Pod undocumented
226 2 my ($self, $name, @args) = @_;
227  
228 2 my $context = $self->{ _CONTEXT };
229  
230 2 ▶ my $code = $context->filter($name, \@args)
Condition33%1/3
$context->filter($name, \@args) || $self->throw($context->error)
l!l&&r!l&&!r
200
MC/DC0%0/2
$context->filter($name, \@args) || $self->throw($context->error)
$context->filter($name, \@args) $self->throw($context->error)
Truth table33%1/3
A || B
A= $context->filter($name, \@args)B= $self->throw($context->error)
ABresult
1X1
011
000
231 || $self->throw($context->error());
232 2 return &$code($self->{ text });
233 }
234  
235  
236 #------------------------------------------------------------------------
237  
238 1 ▶sub upper {
Subroutine upper: called CC=1 SCAR=0.0
Pod documented
239 1 my $self = CORE::shift;
240 1 $self->{ text } = uc $self->{ text };
241 1 return $self;
242 }
243  
244  
245 5 ▶sub lower {
Subroutine lower: called CC=1 SCAR=0.0
Pod documented
246 5 my $self = CORE::shift;
247 5 $self->{ text } = lc $self->{ text };
248 5 return $self;
249 }
250  
251  
252 1 ▶sub capital {
Subroutine capital: called CC=1 SCAR=0.0
Pod documented
253 1 my $self = CORE::shift;
254 1 $self->{ text } =~ s/^(.)/\U$1/;
255 1 return $self;
256 }
257  
258 #------------------------------------------------------------------------
259  
260 2 ▶sub chop {
Subroutine chop: called CC=1 SCAR=0.0
Pod documented
261 2 my $self = CORE::shift;
262 2 chop $self->{ text };
263 2 return $self;
264 }
265  
266  
267 1 ▶sub chomp {
Subroutine chomp: called CC=1 SCAR=0.0
Pod documented
268 1 my $self = CORE::shift;
269 1 chomp $self->{ text };
270 1 return $self;
271 }
272  
273  
274 1 ▶sub trim {
Subroutine trim: called CC=2 SCAR=6.9
Pod documented
275 1 my $self = CORE::shift;
276 1 for ($self->{ text }) {
277 1 s/^\s+//;
278 1 s/\s+$//;
279 }
280 1 return $self;
281 }
282  
283  
284 1 ▶sub collapse {
Subroutine collapse: called CC=2 SCAR=6.9
Pod documented
285 1 my $self = CORE::shift;
286 1 for ($self->{ text }) {
287 1 s/^\s+//;
288 1 s/\s+$//;
289 1 s/\s+/ /g
290 }
291 1 return $self;
292  
293 }
294  
295 #------------------------------------------------------------------------
296  
297 1 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
298 1 my $self = CORE::shift;
299 1 return length $self->{ text };
300 }
301  
302  
303 3 ▶sub truncate {
Subroutine truncate: called CC=4 SCAR=13.9
Pod documented
304 3 my ($self, $length, $suffix) = @_;
305 3 ▶ return $self unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 0 3 3
306 3 ▶ $suffix ||= '';
Condition100%2/2
$suffix ||= ""
l!l
21
MC/DC100%1/1
$suffix ||= ""
$suffix
Truth table100%2/2
A
A= $suffix
Aresult
11
00
307 3 ▶ return $self if CORE::length $self->{ text } <= $length;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length $self->{"text"} <= $length 1 2 3
308 2 $self->{ text } = CORE::substr($self->{ text }, 0,
309 $length - CORE::length($suffix)) . $suffix;
310 2 return $self;
311 }
312  
313  
314 6 ▶sub substr {
Subroutine substr: called CC=4 SCAR=13.9
Pod undocumented
315 6 my ($self, $offset, $length, $replacement) = @_;
316 6 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
33
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
317  
318 6 ▶ if(defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length) { } 4 2 6
319 4 ▶ if (defined $replacement) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement) { } 2 2 4
320 2 my $removed = CORE::substr( $self->{text}, $offset, $length );
321 2 CORE::substr( $self->{text}, $offset, $length ) = $replacement;
322 2 return $removed;
323 }
324 else {
325 2 return CORE::substr( $self->{text}, $offset, $length );
326 }
327 }
328 else {
329 2 return CORE::substr( $self->{text}, $offset );
330 }
331 }
332  
333  
334 1 ▶sub repeat {
Subroutine repeat: called CC=2 SCAR=7.0
Pod documented
335 1 my ($self, $n) = @_;
336 1 ▶ return $self unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 1 1
337 1 $self->{ text } = $self->{ text } x $n;
338 1 return $self;
339 }
340  
341  
342 1 ▶sub replace {
Subroutine replace: called CC=5 SCAR=17.2
Pod documented
343 1 my ($self, $search, $replace) = @_;
344 1 ▶ return $self unless defined $search;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search 0 1 1
345 1 ▶ $replace //= '';
Condition50%1/2
$replace //= ""
l!l
10
MC/DC0%0/1
$replace //= ""
$replace
Truth table50%1/2
A
A= $replace
Aresult
11
00
346 1 my $prev_end = -1;
347 1 $self->{ text } =~ s{$search}{
348 2 my $s = $-[0];
349 2 my $e = $+[0];
350 2 ▶ if ($s == $e && $s == $prev_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == $e and $s == $prev_end) { } 0 2 2
Condition33%1/3
$s == $e and $s == $prev_end
!ll&&!rl&&r
200
MC/DC0%0/2
$s == $e and $s == $prev_end
$s == $e $s == $prev_end
Truth table33%1/3
A and B
A= $s == $eB= $s == $prev_end
ABresult
0X0
100
111
351 0 '';
352 } else {
353 2 $prev_end = $e;
354 2 $replace;
355 }
356 }eg;
357 1 return $self;
358 }
359  
360  
361 2 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
362 2 my ($self, $search) = @_;
363 2 ▶ $search //= '';
Condition50%1/2
$search //= ""
l!l
20
MC/DC0%0/1
$search //= ""
$search
Truth table50%1/2
A
A= $search
Aresult
11
00
364 2 $self->{ text } =~ s/$search//g;
365 2 return $self;
366 }
367  
368  
369 5 ▶sub split {
Subroutine split: called CC=3 SCAR=11.0
Pod documented
370 5 my $self = CORE::shift;
371 5 ▶ my $split = CORE::shift // '\s+';
Condition100%2/2
CORE::shift() // "\\s+"
l!l
41
MC/DC100%1/1
CORE::shift() // "\\s+"
CORE::shift()
Truth table100%2/2
A
A= CORE::shift()
Aresult
11
00
372 5 ▶ my $limit = CORE::shift || 0;
Condition100%2/2
CORE::shift() || 0
l!l
14
MC/DC100%1/1
CORE::shift() || 0
CORE::shift()
Truth table100%2/2
A
A= CORE::shift()
Aresult
11
00
373  
374 5 return [ split($split, $self->{ text }, $limit) ];
375 }
376  
377  
378 4 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
379 4 my ($self, $pattern) = @_;
380 4 return $self->{ text } =~ /$pattern/;
381 }
382  
383  
384 1 ▶sub equals {
Subroutine equals: called CC=1 SCAR=0.0
Pod undocumented
385 1 my ($self, $comparison) = @_;
386 1 return $self->{ text } eq $comparison;
387 }
388  
389  
390 1 1;
391  
392 __END__
« lib/Template/VMethods.pm ↑ Summary lib/Template/Grammar.pm »