blib/lib/Zuzu/Module/String.pm

filter: Statementstmt 26.4% 80 / 302 Branchbran 5.2% 7 / 134 Conditioncond 3.0% 2 / 66 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 27.4% 14 / 51 Podpod 0.0% 0 / 1 totaltotal 17.3% 103 / 594 CC 92 SCAR 40.0
CC
92
Cov
18%
_regexp_from
240.0
_repeat
156.0
_split_text
90.0
CRAP 54.4
« blib/lib/Zuzu/Module/Internals.pm ↑ Summary blib/lib/Zuzu/Module/Time.pm »
1 package Zuzu::Module::String;
2  
3 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 our $VERSION = '0.007002';
6  
7 12 our %REGEXP_CACHE;
8 12 our %SPLIT_LITERAL_CACHE;
9  
10 12 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Regexp::Util qw( deserialize_regexp );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 native_function
16 perl_to_zuzu
17 zuzu_to_perl
18 zuzu_bool
19 12 );
20 12 ▶use Zuzu::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use Zuzu::Value::Regexp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 505 ▶sub _str {
Subroutine _str: called CC=5 SCAR=21.7
25 505 my ( $value, $default, $label ) = @_;
26  
27 505 ▶ return $default if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 505 505
28 505 ▶ if ( blessed($value) and $value->isa('Zuzu::Value::BinaryString') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")) 0 505 505
Condition33%1/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
50500
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
29 0 ▶ $label //= 'std/string function';
Condition0%0/2
$label //= "std/string function"
l!l
00
MC/DC0%0/1
$label //= "std/string function"
$label
Truth table0%0/2
A
A= $label
Aresult
11
00
30 0 die Zuzu::Error->new_runtime(
31 message => "TypeException: $label expects String, got BinaryString",
32 file => '<std/string>',
33 line => 0,
34 );
35 }
36 505 return "$value";
37 }
38  
39 920 ▶sub _num {
Subroutine _num: called CC=2 SCAR=7.1
40 920 my ( $value, $default ) = @_;
41  
42 920 ▶ return $default if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 920 920
43 920 return 0 + $value;
44 }
45  
46 0 ▶sub _int {
Subroutine _int: not called CC=2 SCAR=17.9
47 0 my ( $value, $default, $label ) = @_;
48  
49 0 my $num = _num( $value, $default );
50 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if $num != int $num 0 0 0
51 message => "$label expects an integer",
52 file => '<std/string>',
53 line => 0,
54 ) if $num != int($num);
55 0 return int($num);
56 }
57  
58 0 ▶sub _validate_codepoint {
Subroutine _validate_codepoint: not called CC=5 SCAR=34.0
59 0 my ( $codepoint, $label ) = @_;
60  
61 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint < 0 or $codepoint > 1114111 0 0 0
Condition0%0/3
$codepoint < 0 or $codepoint > 1114111
l!l&&r!l&&!r
000
MC/DC0%0/2
$codepoint < 0 or $codepoint > 1114111
$codepoint < 0 $codepoint > 1114111
Truth table0%0/3
A or B
A= $codepoint < 0B= $codepoint > 1114111
ABresult
1X1
011
000
62 message => "$label expects a Unicode code point in 0..0x10FFFF",
63 file => '<std/string>',
64 line => 0,
65 ) if $codepoint < 0 or $codepoint > 0x10FFFF;
66 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if $codepoint >= 55296 and $codepoint <= 57343 0 0 0
Condition0%0/3
$codepoint >= 55296 and $codepoint <= 57343
!ll&&!rl&&r
000
MC/DC0%0/2
$codepoint >= 55296 and $codepoint <= 57343
$codepoint >= 55296 $codepoint <= 57343
Truth table0%0/3
A and B
A= $codepoint >= 55296B= $codepoint <= 57343
ABresult
0X0
100
111
67 message => "$label rejects surrogate code points",
68 file => '<std/string>',
69 line => 0,
70 ) if $codepoint >= 0xD800 and $codepoint <= 0xDFFF;
71 0 return $codepoint;
72 }
73  
74 0 ▶sub _regexp_from {
Subroutine _regexp_from: not called CC=15 SCAR=54.8
75 0 my ( $pattern, $flags ) = @_;
76  
77 0 my $want_i = 0;
78 0 my $want_g = 0;
79 0 my $pat = '';
80  
81 0 ▶ if ( blessed($pattern) and $pattern->isa('Zuzu::Value::Regexp') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($pattern) and $pattern->isa('Zuzu::Value::Regexp')) { } 0 0 0
elsif (ref $pattern eq 'Regexp') { } 0 0 0
Condition0%0/3
builtin::blessed($pattern) and $pattern->isa("Zuzu::Value::Regexp")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($pattern) and $pattern->isa("Zuzu::Value::Regexp")
builtin::blessed($pattern) $pattern->isa("Zuzu::Value::Regexp")
Truth table0%0/3
A and B
A= builtin::blessed($pattern)B= $pattern->isa("Zuzu::Value::Regexp")
ABresult
0X0
100
111
82 0 ▶ $pat = defined $pattern->pattern ? $pattern->pattern : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern->pattern ? : 0 0 0
83 0 ▶ my $lit_flags = defined $pattern->flags ? $pattern->flags : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern->flags ? : 0 0 0
84 0 ▶ $want_i = 1 if $lit_flags =~ /i/;
Branch: 1 branch
BranchTrueFalseTotal
if $lit_flags =~ /i/ 0 0 0
85 }
86 elsif ( ref($pattern) eq 'Regexp' ) {
87 0 ▶ my $f = defined $flags ? "$flags" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $flags ? : 0 0 0
88 0 ▶ $want_g = 1 if $f =~ /g/;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /g/ 0 0 0
89 0 return ( $pattern, $want_g );
90 }
91 else {
92 0 $pat = _str( $pattern, '' );
93 }
94  
95 0 ▶ my $extra = defined $flags ? "$flags" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $flags ? : 0 0 0
96 0 ▶ $want_i = 1 if $extra =~ /i/;
Branch: 1 branch
BranchTrueFalseTotal
if $extra =~ /i/ 0 0 0
97 0 ▶ $want_g = 1 if $extra =~ /g/;
Branch: 1 branch
BranchTrueFalseTotal
if $extra =~ /g/ 0 0 0
98  
99 0 my $mods = '';
100 0 ▶ $mods = '(?i)' if $want_i;
Branch: 1 branch
BranchTrueFalseTotal
if $want_i 0 0 0
101 0 my $cache_key = join "\x1f", $mods, $pat;
102 0 my $regex = $REGEXP_CACHE{$cache_key};
103 0 ▶ if ( not defined $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 0 0 0
104 0 $regex = eval { qr/$mods$pat/ };
105 0 ▶ die "invalid regexp pattern: $@" if not defined $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regex 0 0 0
106 0 $REGEXP_CACHE{$cache_key} = $regex;
107 }
108  
109 0 return ( $regex, $want_g );
110 }
111  
112 0 ▶sub _pattern_to_regexp {
Subroutine _pattern_to_regexp: not called CC=6 SCAR=37.4
113 0 my ( $pattern, $case_insensitive ) = @_;
114  
115 0 my $pat = _str( $pattern, '' );
116 0 ▶ my $flags = zuzu_bool( $case_insensitive, 0 ) ? 'i' : '';
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($case_insensitive, 0) ? : 0 0 0
117  
118 0 for my $candidate ( qw( / | : " ' ), '#' ) {
119 0 ▶ next if CORE::index( $pat, $candidate ) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($pat, $candidate) != -1 0 0 0
120 0 my $literal = sprintf( 'qr%s%s%s%s', $candidate, $pat, $candidate, $flags );
121 0 my $regex = deserialize_regexp($literal);
122 0 ▶ die "invalid regexp pattern" if not defined $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regex 0 0 0
123 0 return Zuzu::Value::Regexp->new(
124 pattern => $pat,
125 flags => $flags,
126 );
127 }
128  
129 0 my $escaped = $pat;
130 0 $escaped =~ s{/}{\\/}g;
131 0 my $literal = sprintf( 'qr/%s/%s', $escaped, $flags );
132 0 my $regex = deserialize_regexp($literal);
133 0 ▶ die "invalid regexp pattern" if not defined $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regex 0 0 0
134 0 return Zuzu::Value::Regexp->new(
135 pattern => $pat,
136 flags => $flags,
137 );
138 }
139  
140 0 ▶sub _quotemeta {
Subroutine _quotemeta: not called CC=1 SCAR=6.9
141 0 my ( $text ) = @_;
142  
143 0 my $value = _str( $text, '', 'quotemeta()' );
144 0 $value =~ s{([\\/\^\$\.\|\?\*\+\(\)\[\]\{\}"'])}{\\$1}g;
145  
146 0 return $value;
147 }
148  
149 0 ▶sub _repeat {
Subroutine _repeat: not called CC=12 SCAR=50.5
150 0 my ( $count, $text, $separator ) = @_;
151  
152 0 my $repetitions = _num( $count, 0 );
153 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if $repetitions < 0 0 0 0
154 message => 'repeat() count cannot be negative',
155 file => '<std/string>',
156 line => 0,
157 ) if $repetitions < 0;
158 0 $repetitions = int($repetitions);
159  
160 0 ▶ my $text_is_binary = blessed($text)
Condition0%0/3
builtin::blessed($text) && $text->isa('Zuzu::Value::BinaryString')
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($text) && $text->isa('Zuzu::Value::BinaryString')
builtin::blessed($text) $text->isa('Zuzu::Value::BinaryString')
Truth table0%0/3
A && B
A= builtin::blessed($text)B= $text->isa('Zuzu::Value::BinaryString')
ABresult
0X0
100
111
161 && $text->isa('Zuzu::Value::BinaryString');
162 0 ▶ my $separator_is_binary = defined $separator
Condition0%0/6
defined $separator && builtin::blessed($separator) && $separator->isa('Zuzu::Value::BinaryString')
!ll&&!rl&&r
000
defined $separator and builtin::blessed($separator)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $separator && builtin::blessed($separator) && $separator->isa('Zuzu::Value::BinaryString')
defined $separator builtin::blessed($separator) $separator->isa('Zuzu::Value::BinaryString')
Truth table0%0/4
A and B && C
A= defined $separatorB= builtin::blessed($separator)C= $separator->isa('Zuzu::Value::BinaryString')
ABCresult
0XX0
10X0
1100
1111
163 && blessed($separator)
164 && $separator->isa('Zuzu::Value::BinaryString');
165  
166 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if defined $separator and $text_is_binary != $separator_is_binary 0 0 0
Condition0%0/3
defined $separator and $text_is_binary != $separator_is_binary
!ll&&!rl&&r
000
MC/DC0%0/2
defined $separator and $text_is_binary != $separator_is_binary
defined $separator $text_is_binary != $separator_is_binary
Truth table0%0/3
A and B
A= defined $separatorB= $text_is_binary != $separator_is_binary
ABresult
0X0
100
111
167 message => 'TypeException: repeat() cannot mix String and BinaryString values',
168 file => '<std/string>',
169 line => 0,
170 ) if defined $separator && $text_is_binary != $separator_is_binary;
171  
172 0 ▶ if ($text_is_binary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_is_binary) 0 0 0
173 0 ▶ my $bytes = $text->bytes // '';
Condition0%0/2
$text->bytes // ""
l!l
00
MC/DC0%0/1
$text->bytes // ""
$text->bytes
Truth table0%0/2
A
A= $text->bytes
Aresult
11
00
174 0 ▶ my $sep = defined $separator ? $separator->bytes // '' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $separator ? : 0 0 0
Condition0%0/2
$separator->bytes // ""
l!l
00
MC/DC0%0/1
$separator->bytes // ""
$separator->bytes
Truth table0%0/2
A
A= $separator->bytes
Aresult
11
00
175 0 return Zuzu::Value::BinaryString->new(
176 bytes => join $sep, (($bytes) x $repetitions),
177 );
178 }
179  
180 0 my $value = _str( $text, '', 'repeat()' );
181 0 ▶ my $sep = defined $separator ? _str( $separator, '', 'repeat()' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $separator ? : 0 0 0
182 0 return join $sep, (($value) x $repetitions);
183 }
184  
185 0 ▶sub _split_text {
Subroutine _split_text: not called CC=9 SCAR=45.0
186 0 my ( $text, $separator, $limit ) = @_;
187  
188 0 my $value = _str( $text, '' );
189 0 my @parts;
190  
191 0 ▶ if ( defined $separator and blessed($separator) and $separator->isa('Zuzu::Value::Regexp') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $separator and builtin::blessed($separator) and $separator->isa('Zuzu::Value::Regexp')) { } 0 0 0
elsif (ref $separator eq 'Regexp') { } 0 0 0
Condition0%0/6
defined $separator and builtin::blessed($separator) and $separator->isa("Zuzu::Value::Regexp")
!ll&&!rl&&r
000
defined $separator and builtin::blessed($separator)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $separator and builtin::blessed($separator) and $separator->isa("Zuzu::Value::Regexp")
defined $separator builtin::blessed($separator) $separator->isa("Zuzu::Value::Regexp")
Truth table0%0/4
A and B and C
A= defined $separatorB= builtin::blessed($separator)C= $separator->isa("Zuzu::Value::Regexp")
ABCresult
0XX0
10X0
1100
1111
192 0 my ( $regex ) = _regexp_from( $separator, '' );
193 0 ▶ @parts = defined $limit
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 0 0 0
194 ? CORE::split( /$regex/, $value, _num( $limit, 0 ) )
195 : CORE::split( /$regex/, $value );
196 }
197 elsif ( ref($separator) eq 'Regexp' ) {
198 0 ▶ @parts = defined $limit
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 0 0 0
199 ? CORE::split( /$separator/, $value, _num( $limit, 0 ) )
200 : CORE::split( /$separator/, $value );
201 }
202 else {
203 0 my $sep = _str( $separator, '' );
204 0 my $regex = $SPLIT_LITERAL_CACHE{$sep};
205 0 ▶ if ( not defined $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 0 0 0
206 0 my $quoted = quotemeta($sep);
207 0 $regex = qr/$quoted/;
208 0 $SPLIT_LITERAL_CACHE{$sep} = $regex;
209 }
210 0 ▶ @parts = defined $limit
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 0 0 0
211 ? CORE::split( /$regex/, $value, _num( $limit, 0 ) )
212 : CORE::split( /$regex/, $value );
213 }
214  
215 0 return \@parts;
216 }
217  
218 0 ▶sub _word_segments {
Subroutine _word_segments: not called CC=2 SCAR=17.9
219 0 my ( $text ) = @_;
220  
221 0 my $value = _str( $text, '' );
222 0 $value =~ s/([a-z0-9])([A-Z])/$1 $2/g;
223 0 $value =~ s/[^A-Za-z0-9]+/ /g;
224 0 $value =~ s/^\s+//;
225 0 $value =~ s/\s+$//;
226  
227 0 ▶ return () if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 0 0 0
228 0 return grep { $_ ne '' } split /\s+/, $value;
229 }
230  
231 0 ▶sub _trim {
Subroutine _trim: not called CC=1 SCAR=6.9
232 0 my ( $text ) = @_;
233  
234 0 my $value = _str( $text, '' );
235 0 $value =~ s/^\s+//;
236 0 $value =~ s/\s+$//;
237 0 return $value;
238 }
239  
240 0 ▶sub _pad {
Subroutine _pad: not called CC=4 SCAR=30.0
241 0 my ( $text, $width, $pad_char, $side ) = @_;
242  
243 0 my $value = _str( $text, '' );
244 0 my $want_width = _num( $width, 0 );
245 0 my $fill = _str( $pad_char, ' ' );
246 0 ▶ $fill = ' ' if $fill eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $fill eq "" 0 0 0
247 0 my $direction = _str( $side, 'right' );
248  
249 0 my $need = $want_width - length($value);
250 0 ▶ return $value if $need <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $need <= 0 0 0 0
251 0 my $extra = $fill x $need;
252  
253 0 ▶ if ( $direction eq 'left' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq "left") 0 0 0
254 0 return $extra . $value;
255 }
256  
257 0 return $value . $extra;
258 }
259  
260 0 ▶sub _camel {
Subroutine _camel: not called CC=2 SCAR=17.9
261 0 my ( $text ) = @_;
262  
263 0 my @parts = _word_segments( $text );
264 0 ▶ return '' if scalar @parts == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @parts == 0 0 0 0
265  
266 0 my $head = lc shift @parts;
267 my @tail = map {
268 0 my $piece = lc $_;
269 0 substr( $piece, 0, 1 ) = uc substr( $piece, 0, 1 );
270 0 $piece;
271 } @parts;
272  
273 0 return $head . join( '', @tail );
274 }
275  
276 0 ▶sub _title {
Subroutine _title: not called CC=1 SCAR=6.9
277 0 my ( $text ) = @_;
278  
279 0 my @parts = _word_segments( $text );
280 my @words = map {
281 0 my $piece = lc $_;
282 0 substr( $piece, 0, 1 ) = uc substr( $piece, 0, 1 );
283 0 $piece;
284 } @parts;
285  
286 0 return join ' ', @words;
287 }
288  
289 0 ▶sub _snake {
Subroutine _snake: not called CC=1 SCAR=6.9
290 0 my ( $text ) = @_;
291  
292 0 my @parts = _word_segments( $text );
293 0 return join '_', map { lc $_ } @parts;
294 }
295  
296 0 ▶sub _kebab {
Subroutine _kebab: not called CC=1 SCAR=6.9
297 0 my ( $text ) = @_;
298  
299 0 my @parts = _word_segments( $text );
300 0 return join '-', map { lc $_ } @parts;
301 }
302  
303 120 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=3.8
Pod undocumented
304 120 my ( $class, $runtime ) = @_;
305  
306 my $substr_fn = native_function(
307 name => 'substr',
308 460 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
309 460 my ( @args ) = @_;
310 460 my $text = _str( $args[0], '' );
311 460 my $start = _num( $args[1], 0 );
312 460 ▶ if ( scalar @args >= 3 and defined $args[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args >= 3 and defined $args[2]) 460 0 460
Condition33%1/3
scalar @args >= 3 and defined $args[2]
!ll&&!rl&&r
00460
MC/DC0%0/2
scalar @args >= 3 and defined $args[2]
scalar @args >= 3 defined $args[2]
Truth table33%1/3
A and B
A= scalar @args >= 3B= defined $args[2]
ABresult
0X0
100
111
313 460 my $len = _num( $args[2], 0 );
314 460 return CORE::substr( $text, $start, $len );
315 }
316 0 return CORE::substr( $text, $start );
317 },
318 120 );
319  
320 my $index_fn = native_function(
321 name => 'index',
322 22 ▶ native => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
323 22 my ( @args ) = @_;
324 22 my $text = _str( $args[0], '' );
325 22 my $needle = _str( $args[1], '' );
326 22 ▶ my $start = scalar @args >= 3 ? _num( $args[2], 0 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args >= 3 ? : 0 22 22
327 22 return CORE::index( $text, $needle, $start );
328 },
329 120 );
330  
331 my $rindex_fn = native_function(
332 name => 'rindex',
333 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
334 0 my ( @args ) = @_;
335 0 my $text = _str( $args[0], '' );
336 0 my $needle = _str( $args[1], '' );
337 0 ▶ if ( scalar @args >= 3 and defined $args[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args >= 3 and defined $args[2]) 0 0 0
Condition0%0/3
scalar @args >= 3 and defined $args[2]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args >= 3 and defined $args[2]
scalar @args >= 3 defined $args[2]
Truth table0%0/3
A and B
A= scalar @args >= 3B= defined $args[2]
ABresult
0X0
100
111
338 0 my $start = _num( $args[2], 0 );
339 0 return CORE::rindex( $text, $needle, $start );
340 }
341 0 return CORE::rindex( $text, $needle );
342 },
343 120 );
344  
345 my $contains_fn = native_function(
346 name => 'contains',
347 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
348 0 my ( @args ) = @_;
349 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args != 2 0 0 0
350 message => 'contains() expects two arguments',
351 file => '<std/string>',
352 line => 0,
353 ) if scalar @args != 2;
354 0 my $text = _str( $args[0], '' );
355 0 my $needle = _str( $args[1], '' );
356 0 ▶ return Zuzu::Value::Boolean->new(
Branch: 1 branch
BranchTrueFalseTotal
index($text, $needle) != -1 ? : 0 0 0
357 value => CORE::index( $text, $needle ) >= 0 ? 1 : 0,
358 );
359 },
360 120 );
361  
362 my $starts_with_fn = native_function(
363 name => 'starts_with',
364 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
365 0 my ( @args ) = @_;
366 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args != 2 0 0 0
367 message => 'starts_with() expects two arguments',
368 file => '<std/string>',
369 line => 0,
370 ) if scalar @args != 2;
371 0 my $text = _str( $args[0], '' );
372 0 my $prefix = _str( $args[1], '' );
373 0 ▶ return Zuzu::Value::Boolean->new(
Branch: 1 branch
BranchTrueFalseTotal
index($text, $prefix) == 0 ? : 0 0 0
374 value => CORE::index( $text, $prefix ) == 0 ? 1 : 0,
375 );
376 },
377 120 );
378  
379 my $ends_with_fn = native_function(
380 name => 'ends_with',
381 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
382 0 my ( @args ) = @_;
383 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args != 2 0 0 0
384 message => 'ends_with() expects two arguments',
385 file => '<std/string>',
386 line => 0,
387 ) if scalar @args != 2;
388 0 my $text = _str( $args[0], '' );
389 0 my $suffix = _str( $args[1], '' );
390 0 ▶ return Zuzu::Value::Boolean->new(
Branch: 1 branch
BranchTrueFalseTotal
$suffix eq '' || substr($text, -length($suffix)) eq $suffix ? : 0 0 0
Condition0%0/3
$suffix eq "" or substr($text, -length($suffix)) eq $suffix
l!l&&r!l&&!r
000
MC/DC0%0/2
$suffix eq "" or substr($text, -length($suffix)) eq $suffix
$suffix eq "" substr($text, -length($suffix)) eq $suffix
Truth table0%0/3
A or B
A= $suffix eq ""B= substr($text, -length($suffix)) eq $suffix
ABresult
1X1
011
000
391 value => $suffix eq ''
392 || CORE::substr( $text, -length($suffix) ) eq $suffix
393 ? 1
394 : 0,
395 );
396 },
397 120 );
398  
399 my $chr_fn = native_function(
400 name => 'chr',
401 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
402 0 my ( @args ) = @_;
403 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args != 1 0 0 0
404 message => 'chr() expects one argument',
405 file => '<std/string>',
406 line => 0,
407 ) if scalar @args != 1;
408 0 my $codepoint = _validate_codepoint(
409 _int( $args[0], 0, 'chr()' ),
410 'chr()',
411 );
412 0 return chr($codepoint);
413 },
414 120 );
415  
416 my $ord_fn = native_function(
417 name => 'ord',
418 0 ▶ native => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
419 0 my ( @args ) = @_;
420 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args < 1 or scalar @args > 2 0 0 0
Condition0%0/3
scalar @args < 1 or scalar @args > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @args < 1 or scalar @args > 2
scalar @args < 1 scalar @args > 2
Truth table0%0/3
A or B
A= scalar @args < 1B= scalar @args > 2
ABresult
1X1
011
000
421 message => 'ord() expects one or two arguments',
422 file => '<std/string>',
423 line => 0,
424 ) if scalar @args < 1 or scalar @args > 2;
425 0 my $text = _str( $args[0], '', 'ord()' );
426 0 ▶ my $index = _int(
Branch: 1 branch
BranchTrueFalseTotal
scalar @args >= 2 ? : 0 0 0
427 scalar @args >= 2 ? $args[1] : 0,
428 0,
429 'ord()',
430 );
431 0 my $length = length($text);
432 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $length 0 0 0
Condition0%0/3
$index < 0 or $index >= $length
l!l&&r!l&&!r
000
MC/DC0%0/2
$index < 0 or $index >= $length
$index < 0 $index >= $length
Truth table0%0/3
A or B
A= $index < 0B= $index >= $length
ABresult
1X1
011
000
433 message => 'ord() index out of range',
434 file => '<std/string>',
435 line => 0,
436 ) if $index < 0 or $index >= $length;
437 0 return ord( CORE::substr( $text, $index, 1 ) );
438 },
439 120 );
440  
441 my $replace_fn = native_function(
442 name => 'replace',
443 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
444 0 my ( @args ) = @_;
445 0 my $text = _str( $args[0], '' );
446 0 my $replacement = _str( $args[2], '' );
447 0 my ( $regex, $global ) = _regexp_from( $args[1], $args[3] );
448 0 ▶ if ( $global ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) { } 0 0 0
449 0 $text =~ s/$regex/$replacement/g;
450 }
451 else {
452 0 $text =~ s/$regex/$replacement/;
453 }
454 0 return $text;
455 },
456 120 );
457  
458 my $search_fn = native_function(
459 name => 'search',
460 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
461 0 my ( @args ) = @_;
462 0 my $text = _str( $args[0], '' );
463 0 my ( $regex ) = _regexp_from( $args[1], $args[2] );
464 0 ▶ return $1 if $text =~ /($regex)/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /($regex)/ 0 0 0
465 0 return undef;
466 },
467 120 );
468  
469 my $matches_fn = native_function(
470 name => 'matches',
471 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
472 0 my ( @args ) = @_;
473 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args < 2 or scalar @args > 3 0 0 0
Condition0%0/3
scalar @args < 2 or scalar @args > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @args < 2 or scalar @args > 3
scalar @args < 2 scalar @args > 3
Truth table0%0/3
A or B
A= scalar @args < 2B= scalar @args > 3
ABresult
1X1
011
000
474 message => 'matches() expects two or three arguments',
475 file => '<std/string>',
476 line => 0,
477 ) if scalar @args < 2 or scalar @args > 3;
478 0 my $text = _str( $args[0], '' );
479 0 my ( $regex ) = _regexp_from( $args[1], $args[2] );
480 0 ▶ return Zuzu::Value::Boolean->new(
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /$regex/ ? : 0 0 0
481 value => $text =~ /$regex/ ? 1 : 0,
482 );
483 },
484 120 );
485  
486 my $pattern_to_regexp_fn = native_function(
487 name => 'pattern_to_regexp',
488 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
489 0 my ( @args ) = @_;
490 0 return _pattern_to_regexp( $args[0], $args[1] );
491 },
492 120 );
493  
494 my $quotemeta_fn = native_function(
495 name => 'quotemeta',
496 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
497 0 my ( @args ) = @_;
498 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args != 1 0 0 0
499 message => 'quotemeta() expects one argument',
500 file => '<std/string>',
501 line => 0,
502 ) if scalar @args != 1;
503 0 return _quotemeta( $args[0] );
504 },
505 120 );
506  
507 my $sprint_fn = native_function(
508 name => 'sprint',
509 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
510 0 my ( @args ) = @_;
511 0 my $fmt = _str( $args[0], '' );
512 0 ▶ my @vals = map { defined $_ ? $_ : '' } @args[ 1 .. $#args ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
513 0 return sprintf( $fmt, @vals );
514 },
515 120 );
516  
517 my $repeat_fn = native_function(
518 name => 'repeat',
519 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
520 0 my ( @args ) = @_;
521 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args < 2 or scalar @args > 3 0 0 0
Condition0%0/3
scalar @args < 2 or scalar @args > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @args < 2 or scalar @args > 3
scalar @args < 2 scalar @args > 3
Truth table0%0/3
A or B
A= scalar @args < 2B= scalar @args > 3
ABresult
1X1
011
000
522 message => 'repeat() expects two or three arguments',
523 file => '<std/string>',
524 line => 0,
525 ) if scalar @args < 2 or scalar @args > 3;
526 0 ▶ return _repeat(
Branch: 1 branch
BranchTrueFalseTotal
scalar @args >= 3 ? : 0 0 0
527 $args[0],
528 $args[1],
529 scalar @args >= 3 ? $args[2] : undef,
530 );
531 },
532 120 );
533  
534 my $split_fn = native_function(
535 name => 'split',
536 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
537 0 my ( @args ) = @_;
538 0 ▶ return perl_to_zuzu(
Branch: 1 branch
BranchTrueFalseTotal
scalar @args >= 3 ? : 0 0 0
539 _split_text(
540 $args[0],
541 $args[1],
542 scalar @args >= 3 ? $args[2] : undef,
543 )
544 );
545 },
546 120 );
547  
548 my $join_fn = native_function(
549 name => 'join',
550 1 ▶ native => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
551 1 my ( @args ) = @_;
552 1 my $sep = _str( $args[0], '' );
553 1 my $items = zuzu_to_perl( $args[1] );
554 1 ▶ my @parts = ref($items) eq 'ARRAY' ? @$items : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $items eq 'ARRAY' ? : 1 0 1
555 1 ▶ return join $sep, map { defined $_ ? "$_" : '' } @parts;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 1 0 1
556 },
557 120 );
558  
559 my $trim_fn = native_function(
560 name => 'trim',
561 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
562 0 my ( @args ) = @_;
563 0 return _trim( $args[0] );
564 },
565 120 );
566  
567 my $pad_fn = native_function(
568 name => 'pad',
569 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
570 0 my ( @args ) = @_;
571 0 ▶ return _pad(
Branch: 2 branches
BranchTrueFalseTotal
scalar @args >= 3 ? : 0 0 0
scalar @args >= 4 ? : 0 0 0
572 $args[0],
573 $args[1],
574 scalar @args >= 3 ? $args[2] : undef,
575 scalar @args >= 4 ? $args[3] : undef,
576 );
577 },
578 120 );
579  
580 my $chomp_fn = native_function(
581 name => 'chomp',
582 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
583 0 my ( @args ) = @_;
584 0 my $value = _str( $args[0], '' );
585 0 $value =~ s/(?:\r\n|\n|\r)\z//;
586 0 return $value;
587 },
588 120 );
589  
590 my $title_fn = native_function(
591 name => 'title',
592 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
593 0 my ( @args ) = @_;
594 0 return _title( $args[0] );
595 },
596 120 );
597  
598 my $snake_fn = native_function(
599 name => 'snake',
600 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
601 0 my ( @args ) = @_;
602 0 return _snake( $args[0] );
603 },
604 120 );
605  
606 my $kebab_fn = native_function(
607 name => 'kebab',
608 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
609 0 my ( @args ) = @_;
610 0 return _kebab( $args[0] );
611 },
612 120 );
613  
614 my $camel_fn = native_function(
615 name => 'camel',
616 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
617 0 my ( @args ) = @_;
618 0 return _camel( $args[0] );
619 },
620 120 );
621  
622 my $to_binary_fn = native_function(
623 name => 'to_binary',
624 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
625 0 my ( $value ) = @_;
626 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or builtin::blessed($value) or ref $value 0 0 0
Condition0%0/6
not defined $value or builtin::blessed($value) or ref $value
l!l&&r!l&&!r
000
not defined $value or builtin::blessed($value)
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $value or builtin::blessed($value) or ref $value
not defined $value builtin::blessed($value) ref $value
Truth table0%0/4
not(A) or B or C
A= not defined $valueB= builtin::blessed($value)C= ref $value
ABCresult
1XX1
01X1
0011
0000
627 message => 'TypeException: to_binary expects String',
628 file => '<std/string>',
629 line => 0,
630 ) if not defined $value or blessed($value) or ref($value);
631 0 return Zuzu::Value::BinaryString->from_utf8_string( $value );
632 },
633 120 );
634  
635 my $to_string_fn = native_function(
636 name => 'to_string',
637 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
638 0 my ( $value ) = @_;
639 0 ▶ die Zuzu::Error->new_runtime(
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
640 message => 'TypeException: to_string expects BinaryString',
641 file => '<std/string>',
642 line => 0,
643 ) if not( blessed($value) and $value->isa('Zuzu::Value::BinaryString') );
644 0 return $value->to_utf8_string;
645 },
646 120 );
647  
648 return {
649 120 to_binary => $to_binary_fn,
650 to_string => $to_string_fn,
651 substr => $substr_fn,
652 index => $index_fn,
653 rindex => $rindex_fn,
654 contains => $contains_fn,
655 starts_with => $starts_with_fn,
656 ends_with => $ends_with_fn,
657 chr => $chr_fn,
658 ord => $ord_fn,
659 replace => $replace_fn,
660 search => $search_fn,
661 matches => $matches_fn,
662 pattern_to_regexp => $pattern_to_regexp_fn,
663 quotemeta => $quotemeta_fn,
664 sprint => $sprint_fn,
665 repeat => $repeat_fn,
666 split => $split_fn,
667 join => $join_fn,
668 trim => $trim_fn,
669 pad => $pad_fn,
670 chomp => $chomp_fn,
671 title => $title_fn,
672 snake => $snake_fn,
673 kebab => $kebab_fn,
674 camel => $camel_fn,
675 };
676 }
677  
678 12 1;
679  
680 =pod
681  
682 =head1 COPYRIGHT AND LICENCE
683  
684 B<< Zuzu::Module::String >> is copyright Toby Inkster.
685  
686 It is free software; you may redistribute it and/or modify it under
687 the terms of either the Artistic License 1.0 or the GNU General Public
688 License version 2.
689  
690 =cut
« blib/lib/Zuzu/Module/Internals.pm ↑ Summary blib/lib/Zuzu/Module/Time.pm »