blib/lib/String/Rexx.pm

filter: Statementstmt 100% 166 / 166 Branchbran 97.9% 47 / 48 Conditioncond 93.8% 46 / 49 MC/DCmcdc 88.0% 22 / 25 Subroutinesub 100% 55 / 55 Podpod 78.0% 32 / 41 totaltotal 95.8% 368 / 384 CC 47 SCAR 9.6
CC
47
Cov
98%
datatype
5.0
translate
5.0
verify
5.0
CRAP 2.6
↑ Summary
1 package String::Rexx;
2  
3 42 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 42 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 42 ▶use Params::Validate ':all' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 42 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 42 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #use AutoLoader qw(AUTOLOAD);
12  
13  
14 42 our %EXPORT_TAGS = ( 'all' => [ qw(
15 centre center changestr compare copies countstr
16 delstr delword datatype d2c b2d d2x
17 x2b b2x x2c x2d c2x
18 d2b errortext insert lastpos left Length
19 overlay Pos right Reverse Abbrev sign
20 space Substr strip subword translate verify
21 word wordindex wordlength wordpos words xrange
22 ) ] );
23  
24 42 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25  
26 42 our $VERSION = '0.08';
27  
28 42 ▶use constant BINARY => qr/^[01]{0,32}$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 42 ▶use constant REAL => $RE{num}{real};
Subroutine BEGIN: called CC=1 SCAR=0.0
30 42 ▶use constant HEX8 => qr/^(?:0x)?[\dabcdef]{0,8}$/i ;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 42 ▶use constant HEX => qr/^(?:0x)?[\dabcdef]*$/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 42 ▶use constant NONNEGATIVE => qr/^[+]?[\d]+$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 42 ▶use constant POSITIVE => qr/^[+]?[1-9]+$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 42 1;
37 =pod
38  
39 =head1 NAME
40  
41 String::Rexx - Perl implementation of Rexx string functions
42  
43 =head1 SYNOPSIS
44  
45 use String::Rexx qw( :all );
46  
47 use String::Rexx qw( functions... );
48  
49  
50 =head1 DESCRIPTION
51  
52 This module implements almost all string functions
53 of regina2-rexx .
54  
55  
56 =over
57  
58 =item Abbrev( 'long', 'short' [, len] )
59  
60 Return 1 if string $short is a shortcut for string $long. And optionally, $len
61 must not be greater than what the number of charecters matched with string $shoft.
62 Otherwise, return 0.
63  
64 =item countstr( 'pat', 'string' )
65  
66 Counts the number of occurences of 'pat' within 'string'
67  
68 =item center( 'string' , len [, char ] )
69  
70 Returns a string of length len, with the proper padding so that 'string' is centered
71  
72  
73 =item changestr( 'old', 'string' , 'new' )
74  
75 Changes all instances of substing 'old' inside 'string' to the new string 'new'
76  
77 =item compare( 'string1' , 'string2' [, char ] )
78  
79 Returns 0 when both strings are equal, or N when they are not.
80 The number N denotes the length of either string -- strings are always
81 compared after padding, so they always have equal length when they are compared.
82 If the comparison fails, and the string contents are unequal, it will
83 return N, the size of either string since by now hey both have the same length.
84 The default pad character is space , ' ' .
85  
86  
87 =item copies( 'string' , N )
88  
89 Returns a string consisting of N concatenated copies. N = 0, 1, 2, ...
90  
91 =item delstr( 'string' , start [,length] )
92  
93 Deletes the substring which starts at $start.
94 Length defaults to the rest of string.
95  
96 =item delword( 'string' , start [,length] )
97  
98 Deletes $length words, starting from $start.
99 Length defaults to the rest of string.
100  
101 =item errortext( N )
102  
103 Returns the error string that describes the error number N .
104  
105 =item datatype( 'string' [, option ] )
106  
107 When 'string' represents a number or a non-number literal, this fuction
108 returns 'NUM' or 'LIT', respectively.
109 Option can be either 'NUM' or 'LIT'; when specified, the fuction returns 1 (true) or
110 0 (false) depending whether 'string' is a NUM or a LIT .
111 If the user-supplied option is neither NUM or LOT, the return value is set to undef ;
112  
113 =item d2c( N )
114  
115 Same as chr(N) . Converts decimal N to its char in the character set.
116  
117 =item d2x( N [, length] )
118  
119 Converts decimal N to a hex string of size $lenght .
120  
121 =item insert( 'source' , 'target' [,'position' ] [,'length'] [, char ] )
122  
123 Inserts string 'source' into string 'targer'.
124 Position defaults to 0, length defaults to len of $source , and
125 padding char defaults to ' ' .
126  
127 =item lastpos( 'needle' , 'haystack', [, start ] )
128  
129 Returns the position of $needle in $haystack (searching from the end) .
130 Returns 0 if not found, and 1 when neelde occurs at start of haystack
131  
132 =item left( 'string' , length, [, 'char'] )
133  
134 Returns the leftmost $length chars. If there are not
135 enough characters, the string is padded (prepended) with char characters.
136 Char defaults to space.
137  
138 =item Length( 'string' )
139  
140 Returns the length of string.
141  
142 =item overlay( 'source', 'target' [, start] [, length] [, pad] )
143  
144 Overstrikes the $target string, with $source .
145  
146  
147 =item Reverse( 'string' )
148  
149 Reverses the order of characters in string.
150  
151 =item Pos( 'needle' , 'haystack', [, start ] )
152  
153 Returns the position of $needle in $haystack .
154 Returns 0 if not found, and 1 when neelde occurs at start of haystack
155  
156 =item right( 'string' , length, [, 'char'] )
157  
158 Returns the $length chars from the end of string. If there are not
159 enough characters, the string is padded (prepended) with char characters.
160 Char defaults to space.
161  
162  
163 =item space( 'string' [, 'length'] [, 'char'] )
164  
165 After removing leading and trailing spaces, internal whitespace
166 change to $length chars. Char defaults to ' ' , and length defaults to 1 .
167  
168  
169 =item strip( 'string' [, 'Option'] [, 'char'] )
170
171 Strips leading whitespace from string.
172 The optional 2nd param can be set to 'leading', 'trailing', or 'both' .
173 The optional 3rd param will strip chars instead of whitespace.
174 Returns the striped string.
175  
176 =item Substr( 'string', start, [, length ] [, padchar ] )
177
178 Returns a substring of string. If string does not have enough chars
179 to fill the request, we use padding with character padchar .
180  
181 =item wordindex( 'string', N )
182  
183 Returns the index in the string for the Nth word.
184  
185 =item wordlength( 'string', N )
186  
187 Returns the length of the Nth word in the string. Where N=1,2,...
188 Returns 0 if there are less worlds than N .
189 Raises exception if is N < 1
190  
191 =item subword( 'string', start [, N ] )
192  
193 Returns a string of words staring from start. N denotes how many
194 words to return (default is as many as possible.)
195 start = N = 1,2,...
196  
197 =item translate( 'string' [, 'new' , 'old' [, pad] ] )
198  
199 The translitaration oparator. It returns a strings where all characters
200 in 'old' and transformed to the corresponding characters in 'new' .
201 In the special case when all options are absent, it translates $string
202 to upper case.
203  
204 =item verify( 'string', 'chars' [, sense] [, start] )
205  
206 Returns 0 if $string consists from characters in the set $chars , otherwise
207 it returns the position of the 1st character in $sting that failed the match.
208 The sense param is either 'M' for match, or 'N' for non-match, default is 'M' .
209 The start param indicate the position from 'string' to start matching (default is 1,
210 to match from the start of 'string').
211  
212  
213 =item wordpos( 'pattern' , 'string' )
214
215 Returns the position of the word in 'string' containing 'pattern'
216  
217 =item word( 'string', wordno )
218  
219 Returns the nth word in the string.
220  
221 =item words( 'string' )
222  
223 Returns the number of words in the string.
224  
225 =item xrange( S , E )
226  
227 Retruns a sequence of characters, starting with char S, and ending with char E .
228  
229  
230 =back
231  
232 =head2 EXPORT
233  
234 None by default.
235  
236  
237 =head1 AUTHOR
238  
239 Ioannis Tambouras, E<lt>ioannis@earthlink.netE<gt>
240  
241 =head1 SEE ALSO
242  
243 L<regina(1)>.
244  
245 =cut
246 8 ▶sub Abbrev ($$;$) {
Subroutine Abbrev: called CC=4 SCAR=13.9
Pod documented
247 8 ( local $_ , my ( $short , $len )) = validate_pos @_ ,
248 { type => SCALAR },
249 { type => SCALAR },
250 { regex => qr/^\+?\s*\d+$/, optional=>1 };
251  
252 7 ▶ /^$short/ || ((defined $len)&&length $short >= $len) ? 1
Branch: 1 branch
BranchTrueFalseTotal
/^$short/ || defined $len && length $short >= $len ? : 5 2 7
Condition100%6/6
/^$short/ or defined $len and length $short >= $len
l!l&&r!l&&!r
322
defined $len and length $short >= $len
!ll&&!rl&&r
112
MC/DC100%3/3
/^$short/ or defined $len and length $short >= $len
/^$short/ defined $len length $short >= $len
Truth table100%4/4
A or B and C
A= /^$short/B= defined $lenC= length $short >= $len
ABCresult
1XX1
0111
00X0
0100
253 : 0 ;
254 }
255 13 ▶sub center ($$;$) {
Subroutine center: called CC=1 SCAR=0.0
Pod documented
256  
257 13 my ($str , $len , $char) = validate_pos @_ ,
258 { type => SCALAR } ,
259 { regex => qr/^\+?\s*\d+$/ } ,
260 { regex => qr/^.$/ , default => ' ' },
261 ;
262  
263 13 $str = substr( $str, 0, $len ) ; # if they asked for less space than given text
264 13 my $offset = ($len - length $str)/2 ;
265 13 ($char x $offset) . $str . ($char x ($offset+0.6) ) ;
266 }
267 1 ▶sub centre ($$;$) { goto &center }
Subroutine centre: called CC=1 SCAR=0.0
Pod undocumented
268 9 ▶sub changestr ($$$) {
Subroutine changestr: called CC=1 SCAR=0.0
Pod documented
269 9 my ( $old , $string, $new) = @_ ;
270 9 $string =~ s/\Q$old/$new/g , $string;
271 }
272 10 ▶sub compare ($$;$) {
Subroutine compare: called CC=3 SCAR=11.0
Pod documented
273 10 my ( $a, $b , $char) = validate_pos @_ ,
274 { type => SCALAR },
275 { type => SCALAR },
276 { regex => qr/^.$/ , default => ' ' };
277  
278 10 my ($Alen , $Blen) = ( length($a) , length($b) );
279 10 ▶ my $max = ($Alen > $Blen) ? $Alen : $Blen ;
Branch: 1 branch
BranchTrueFalseTotal
$Alen > $Blen ? : 1 9 10
280  
281 10 substr( $a, $Alen ) = $char x ($max - $Alen) ;
282 10 substr( $b, $Blen ) = $char x ($max - $Blen) ;
283 10 ▶ $a cmp $b && $max ;
Branch: 1 branch
BranchTrueFalseTotal
$a cmp $b and $max 6 4 10
284 }
285 6 ▶sub copies ($$) {
Subroutine copies: called CC=1 SCAR=0.0
Pod documented
286 6 my ( $str , $num ) = validate_pos @_ ,
287 { type => SCALAR },
288 { regex => NONNEGATIVE } ;
289 6 $str x $num ;
290 }
291 14 ▶sub countstr ($$) {
Subroutine countstr: called CC=2 SCAR=6.9
Pod documented
292 14 (my ($pat), local $_) = @_ ;
293 14 ▶ ($pat eq '') ? 0 : ( () = /\Q$pat/gx ) ;
Branch: 1 branch
BranchTrueFalseTotal
$pat eq '' ? : 1 13 14
294 }
295 17 ▶sub datatype ($;$) {
Subroutine datatype: called CC=5 SCAR=16.1
Pod documented
296 17 my ($real, $str, $option) = (REAL, validate_pos @_,
297 { type => SCALAR },
298 { regex => qr/^[ablmnuwx]$/i, optional=>1});
299 (defined $option)
300 ? $str =~ { A => '(?i)^[a-z\d]+$' ,
301 B => '^[01]+$' ,
302 L => '^[a-z]+$' ,
303 M => '(?i)^[a-z]+$' ,
304 N => "^$real\$" ,
305 U => '^[A-Z]+$' ,
306 W => '^[-+]?[\d]+$' ,
307 X => '(?i)^[\da-f]+$' ,
308 15 ▶ }->{uc $option} || 0
Branch: 1 branch
BranchTrueFalseTotal
defined $option ? : 11 4 15
Condition100%6/6
$str =~ /{"A", "(?i)^[a-z\\d]+\$", "B", "^[01]+\$", "L", "^[a-z]+\$", "M", "(?i)^[a-z]+\$", "N", "^$real\$", "U", "^[A-Z]+\$", "W", "^[-+]?[\\d]+\$", "X", "(?i)^[\\da-f]+\$"}->{uc $option}/ or 0
l!l
92
$str =~ /^$real$/ and "NUM" or "CHAR"
l!l
22
$str =~ /^$real$/ and "NUM"
!ll
22
MC/DC100%1/1
$str =~ /{"A", "(?i)^[a-z\\d]+\$", "B", "^[01]+\$", "L", "^[a-z]+\$", "M", "(?i)^[a-z]+\$", "N", "^$real\$", "U", "^[A-Z]+\$", "W", "^[-+]?[\\d]+\$", "X", "(?i)^[\\da-f]+\$"}->{uc $option}/ or 0
$str =~ /{"A", "(?i)^[a-z\\d]+\$", "B", "^[01]+\$", "L", "^[a-z]+\$", "M", "(?i)^[a-z]+\$", "N", "^$real\$", "U", "^[A-Z]+\$", "W", "^[-+]?[\\d]+\$", "X", "(?i)^[\\da-f]+\$"}->{uc $option}/
MC/DC100%1/1
$str =~ /^$real$/ and "NUM" or "CHAR"
$str =~ /^$real$/
Truth table100%2/2
A
A= $str =~ /{"A", "(?i)^[a-z\\d]+\$", "B", "^[01]+\$", "L", "^[a-z]+\$", "M", "(?i)^[a-z]+\$", "N", "^$real\$", "U", "^[A-Z]+\$", "W", "^[-+]?[\\d]+\$", "X", "(?i)^[\\da-f]+\$"}->{uc $option}/
Aresult
11
00
Truth table100%2/2
A
A= $str =~ /^$real$/
Aresult
11
00
309 : ($str =~ /^$real$/) && 'NUM' || 'CHAR' ;
310 }
311 1 ▶sub d2c ($) {
Subroutine d2c: called CC=1 SCAR=0.0
Pod documented
312 1 chr(shift) ;
313 }
314 8 ▶sub delstr ($$;$) {
Subroutine delstr: called CC=2 SCAR=6.9
Pod documented
315 8 my ( $string , $start, $len ) = validate_pos @_ ,
316 { type => SCALAR },
317 { regex => qr/^\+?\s*\d+$/ },
318 { regex => qr/^\+?\s*\d+$/ ,
319 default => length $_[0] } ;
320  
321 8 ▶ substr( ($string || return'' ) , $start-1, $len, '' ) ;
Condition100%2/2
$string || (return '')
l!l
53
MC/DC100%1/1
$string || (return '')
$string
Truth table100%2/2
A
A= $string
Aresult
11
00
322 5 $string;
323 }
324 26 ▶sub delword ($$;$) {
Subroutine delword: called CC=2 SCAR=6.9
Pod documented
325 26 (local $_, my ($start,$count))= validate_pos @_ ,
326 {type => SCALAR},
327 {regex=> NONNEGATIVE, default=>1 },
328 {regex=> NONNEGATIVE, default=>3E4};
329 26 $start-- ;
330 26 ▶ / ^ (?:\S+\s*){$start} /xg or return $_ ;
Branch: 1 branch
BranchTrueFalseTotal
/ ^ (?:\S+\s*){$start} /gx or return $_ 25 1 26
331 25 s/(?:(?<=^)|(?<=\s)) \G (\s*\S+){0,$count} \s* //x , $_ ;
332 }
333 1 ▶sub errortext ($) {
Subroutine errortext: called CC=1 SCAR=0.0
Pod documented
334 1 local ($!) = validate_pos @_ , { regex => qr/^\+?\s*\d+$/ } ,;
335 1 $! ;
336 }
337 15 ▶sub insert ($$;$$$) {
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
338 15 my ($source , $target, $pos, $len, $char) = validate_pos @_ ,
339 { type => SCALAR },
340 { type => SCALAR },
341 { regex => qr/^\+?\s*\d+$/ , default => 0 },
342 { regex => qr/^\+?\s*\d+$/ , default => length $_[0] },
343 { regex => qr/^.$/ , default => ' ' };
344  
345 15 my $targlen = length $target ;
346 15 substr( $source, $len ) = '' ;
347  
348 15 ▶ ($pos <= $targlen) ? substr( $target, $pos , 0 , $source )
Branch: 1 branch
BranchTrueFalseTotal
$pos <= $targlen ? : 11 4 15
349 : ($target .= $char x ($pos-$targlen) . $source ) ;
350 15 $target;
351 }
352 11 ▶sub lastpos ($$;$) {
Subroutine lastpos: called CC=2 SCAR=6.9
Pod documented
353 11 my ($needle , $hay, $start ) = validate_pos @_ ,
354 { type => SCALAR },
355 { type => SCALAR },
356 { regex => qr/^\+?\s*\d+$/,
357 default => length $_[1] };
358  
359 11 ▶ 1 + rindex( $hay, ($needle || return 0), --$start );
Condition100%2/2
$needle || (return 0)
l!l
92
MC/DC100%1/1
$needle || (return 0)
$needle
Truth table100%2/2
A
A= $needle
Aresult
11
00
360 }
361 7 ▶sub left ($$;$) {
Subroutine left: called CC=2 SCAR=6.9
Pod documented
362 7 my ($str, $len, $char) = validate_pos @_ ,
363 { type => SCALAR },
364 { regex => qr/^\+?\s*\d+$/ },
365 { type => SCALAR, default=>' '},;
366  
367 7 ▶ my $padding = $char x (($len||return '') - length $str) ;
Condition100%2/2
$len || (return '')
l!l
52
MC/DC100%1/1
$len || (return '')
$len
Truth table100%2/2
A
A= $len
Aresult
11
00
368 5 substr( $str, 0, $len) . $padding ;
369 }
370 2 ▶sub Length ($) {
Subroutine Length: called CC=1 SCAR=0.0
Pod documented
371 2 length shift();
372 }
373 15 ▶sub overlay ($$;$$$) {
Subroutine overlay: called CC=1 SCAR=0.0
Pod documented
374 15 my ($source , $target, $start, $len, $char ) = validate_pos @_ ,
375 { type => SCALAR },
376 { type => SCALAR },
377 { regex => qr/^\+?\s*\d+$/ , default => 1 },
378 { regex => qr/^\+?\s*\d+$/ , default => length $_[0] },
379 { regex => qr/^.$/ , default => ' ' };
380  
381 15 $source .= $char x ($len-length $source) ;
382 15 substr( $source, $len ) = '' ;
383 15 substr( $target, --$start, (length $source) , $source ) ;
384 15 $target;
385 }
386 11 ▶sub Pos ($$;$) {
Subroutine Pos: called CC=3 SCAR=11.0
Pod documented
387 11 my ($needle , $hay, $start ) = @_ ;
388 11 ▶ 1 + index( $hay, ($needle||return 0), ($start||0) );
Condition100%4/4
$needle || (return 0)
l!l
92
$start || 0
l!l
36
MC/DC100%1/1
$needle || (return 0)
$needle
MC/DC100%1/1
$start || 0
$start
Truth table100%2/2
A
A= $needle
Aresult
11
00
Truth table100%2/2
A
A= $start
Aresult
11
00
389 }
390 7 ▶sub right ($$;$) {
Subroutine right: called CC=2 SCAR=6.9
Pod documented
391 7 my ($str, $len, $char) = validate_pos @_ ,
392 { type => SCALAR },
393 { regex => qr/^\+?\s*\d+$/ },
394 { type => SCALAR, default=>' '},;
395  
396 7 ▶ my $padding = $char x (($len||return '') - length $str) ;
Condition100%2/2
$len || (return '')
l!l
52
MC/DC100%1/1
$len || (return '')
$len
Truth table100%2/2
A
A= $len
Aresult
11
00
397 5 $padding . substr( $str, - $len);
398 }
399 2 ▶sub Reverse ($) {
Subroutine Reverse: called CC=1 SCAR=0.0
Pod documented
400 2 scalar reverse shift() ;
401 }
402 15 ▶sub space ($;$$) {
Subroutine space: called CC=1 SCAR=0.0
Pod documented
403 15 (local $_, my ($len , $char)) = validate_pos @_ ,
404 { type => SCALAR },
405 { regex => NONNEGATIVE, default => 1 },
406 { regex => qr/^.$/s , default => ' '};
407  
408 15 s/^\s*|\s*$//g ;
409 15 s/\s+/{ $char x $len }/eg , $_ ;
410 }
411 7 ▶sub Substr ($$;$$) {
Subroutine Substr: called CC=4 SCAR=14.6
Pod documented
412 7 my ($str, $start, $len, $char) = @_ ;
413 7 ▶ my $slen = length($str) || return '' ;
Condition100%2/2
length $str || (return '')
l!l
61
MC/DC100%1/1
length $str || (return '')
length $str
Truth table100%2/2
A
A= length $str
Aresult
11
00
414 6 ▶ my $padding = ( $slen && $start) + $len - ($slen && 1) - $slen ;
Condition40%2/5
$slen && $start
!ll&&!rl&&r
006
$slen && 1
!ll
06
MC/DC0%0/2
$slen && $start
$slen $start
MC/DC0%0/1
$slen && 1
$slen
Truth table33%1/3
A && B
A= $slenB= $start
ABresult
0X0
100
111
Truth table50%1/2
A
A= $slen
Aresult
00
11
415 6 substr($str, $start-1, $len) . $char x $padding ,
416 }
417 23 ▶sub strip ($;$$) {
Subroutine strip: called CC=1 SCAR=0.0
Pod documented
418 23 (local $_, my ( $direction, $char)) = validate_pos @_ ,
419 { type => SCALAR },
420 { regex => qr/^[LTB]$/i ,
421 default => 'B' },
422 { regex => qr/^\S$/, default=>' '};
423 my $pattern = { L => "^[$char]+" ,
424 T => "[$char]+\$" ,
425 B => "^[$char]+|[$char]+\$" ,
426 21 }->{ uc $direction } ;
427  
428 21 s/ $pattern //gx , $_ ;
429 }
430 9 ▶sub translate ($;$$$) {
Subroutine translate: called CC=5 SCAR=16.1
Pod documented
431 9 $_ = shift , my ($new, $old, $pad) = @_ ;
432 9 ▶ return uc unless ( $new || $old );
Branch: 1 branch
BranchTrueFalseTotal
unless $new or $old 1 8 9
Condition100%3/3
$new or $old
l!l&&r!l&&!r
621
MC/DC100%2/2
$new or $old
$new $old
Truth table100%3/3
A or B
A= $newB= $old
ABresult
1X1
011
000
433 8 ▶ return undef unless defined $old;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old 1 7 8
434  
435 7 ▶ $pad = $pad || ' ' ;
Condition100%2/2
$pad || ' '
l!l
43
MC/DC100%1/1
$pad || ' '
$pad
Truth table100%2/2
A
A= $pad
Aresult
11
00
436 42 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
437 7 eval "y/$old/$new$pad/" ;
438 7 $_;
439 }
440 53 ▶sub verify ($$;$$) {
Subroutine verify: called CC=5 SCAR=16.1
Pod documented
441 53 (local $_, my ($ref, $opt, $start)) =
442 validate_pos @_ ,
443 { type => SCALAR },
444 { type => SCALAR },
445 { regex => qr/^[NM]$/i, default=>'N' }, { regex => POSITIVE, default=> 1 };
446 52 ▶ return 0 if $ref eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "" 1 51 52
447 51 my $pattern = qr/ (?: [\Q$ref\E]) /x ;
448 51 pos() = --$start;
449  
450 ($opt =~ /N/i)
451 51 ▶ ? do{ /\G $pattern+/xgc;
Branch: 2 branches
BranchTrueFalseTotal
$opt =~ /N/i ? do { /\G $pattern+/cgx; return pos $_ == length $_ ? 0 : 1 + pos($_) } : /$pattern/cg or return 0 11 8 19
$opt =~ /N/i ? : 32 19 51
452 32 ▶ return (pos == length) ? 0 : 1 + pos }
Branch: 1 branch
BranchTrueFalseTotal
pos $_ == length $_ ? : 23 9 32
453 : /$pattern/gc or return 0;
454 11 pos ;
455 }
456 10 ▶sub word ($$) {
Subroutine word: called CC=2 SCAR=6.9
Pod documented
457 10 (local $_, my $start) = validate_pos @_ ,
458 { type => SCALAR } ,
459 { regex => POSITIVE } ;
460  
461 8 $start--;
462 8 ▶ / ^ (?>\s* (?:\S+\s*){$start}) (\s*\S+) /x
Branch: 1 branch
BranchTrueFalseTotal
/ ^ (?>\s* (?:\S+\s*){$start}) (\s*\S+) /x and $+ 4 4 8
463 and $+;
464 }
465 19 ▶sub wordindex ($$) {
Subroutine wordindex: called CC=3 SCAR=11.0
Pod documented
466 19 my ($str, $n) = validate_pos @_ , { type => SCALAR },
467 { regex => NONNEGATIVE } ;
468  
469 18 ▶ (' '.$str) =~ / (?:\s+ (\S+)){$n} /x
Branch: 1 branch
BranchTrueFalseTotal
(' ' . $str) =~ / (?:\s+ (\S+)){$n} /x and $-[1] or 0 15 3 18
Condition100%3/3
(' ' . $str) =~ / (?:\s+ (\S+)){$n} /x and $-[1]
!ll&&!rl&&r
2115
MC/DC100%2/2
(' ' . $str) =~ / (?:\s+ (\S+)){$n} /x and $-[1]
(' ' . $str) =~ / (?:\s+ (\S+)){$n} /x $-[1]
Truth table100%3/3
A and B
A= (' ' . $str) =~ / (?:\s+ (\S+)){$n} /xB= $-[1]
ABresult
0X0
100
111
470 && $-[1] or 0;
471 }
472 18 ▶sub wordlength ($$) {
Subroutine wordlength: called CC=3 SCAR=11.0
Pod documented
473 18 my ($str, $n) = validate_pos @_ , { type => SCALAR },
474 { regex => NONNEGATIVE };
475  
476 17 ▶ (' '.$str) =~ / (?:\s (\S+)){$n} /x
Branch: 1 branch
BranchTrueFalseTotal
(' ' . $str) =~ / (?:\s (\S+)){$n} /x ? : 12 5 17
Condition100%2/2
$1 || ''
l!l
102
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
477 ? length ($1||'')
478 : 0 ;
479 }
480 28 ▶sub subword ($$;$) {
Subroutine subword: called CC=2 SCAR=7.1
Pod documented
481 28 (local $_, my ($start, $n)) = validate_pos @_ ,
482 { type => SCALAR } ,
483 { regex => POSITIVE } ,
484 { regex => NONNEGATIVE , default => 3e3 };
485  
486 27 $start--;
487 27 ▶ / ^ (?>\s* (?:\S+\s*){$start}) ((?:\s*\S+){0,$n}) /x
Branch: 1 branch
BranchTrueFalseTotal
/ ^ (?>\s* (?:\S+\s*){$start}) ((?:\s*\S+){0,$n}) /x and $+ 27 0 27
488 and $+;
489 }
490 8 ▶sub wordpos ($$) {
Subroutine wordpos: called CC=5 SCAR=16.1
Pod documented
491 8 (my $pat, local $_, my $i) = @_ ;
492  
493 8 ▶ return 0 if $pat eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $pat eq "" 1 7 8
494 7 ▶ $i++ while ( / \G (?>\s*\S+) (?<!\Q$pat\E) /xcg) ;
Branch: 1 branch
BranchTrueFalseTotal
/ \G (?>\s*\S+) (?<!\Q$pat\E) /cgx and do { ++$i } 17 7 24
495 7 ▶ ((pos||0) == length) ? 0 : ++$i ;
Branch: 1 branch
BranchTrueFalseTotal
(pos $_ || 0) == length $_ ? : 2 5 7
Condition100%2/2
pos $_ || 0
l!l
61
MC/DC100%1/1
pos $_ || 0
pos $_
Truth table100%2/2
A
A= pos $_
Aresult
11
00
496 }
497 9 ▶sub words ($) {
Subroutine words: called CC=1 SCAR=0.0
Pod documented
498 9 scalar ( () = shift() =~ /(\S+)/g );
499 }
500 4 ▶sub xrange ($$) {
Subroutine xrange: called CC=2 SCAR=6.9
Pod documented
501 4 my ($start, $end) = @_ ;
502 4 my $len = 1 + ord ($end) - ord ($start);
503 4 ▶ return pack 'A'x$len , $start..$end if $len>0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 0 3 1 4
504 1 pack 'A'x(257+$len) , $start..chr(0xff) , chr(0)..$end ;
505 }
506 6 ▶sub b2d {
Subroutine b2d: called CC=1 SCAR=0.0
Pod undocumented
507 6 (local $_) = validate_pos @_ , { regex => qr/^[01]+$/ } ;
508 4 eval "0b$_";
509  
510 # Method2 (4x faster)
511 # pack 'A*', unpack 'N', pack 'B32', '0'x (32-length) .$_ ;
512 }
513 5 ▶sub d2x {
Subroutine d2x: called CC=2 SCAR=6.9
Pod documented
514 5 validate_pos @_, { regex => NONNEGATIVE } ;
515  
516 3 local $_ = pack '(A*)*', unpack '(H2)*', pack 'N', shift ;
517 3 s/^0*//g ;
518 3 ▶ $_ or '0';
Branch: 1 branch
BranchTrueFalseTotal
$_ or 0 2 1 3
519 }
520 3 ▶sub sign ($) {
Subroutine sign: called CC=3 SCAR=11.0
Pod undocumented
521 3 ▶ ($_[0] > 0) ? 1
Branch: 2 branches
BranchTrueFalseTotal
$_[0] > 0 ? : 1 2 3
$_[0] < 0 ? : 1 1 2
522 : ($_[0]<0) ? -1
523 : 0 ;
524 }
525 8 ▶sub x2b {
Subroutine x2b: called CC=2 SCAR=6.9
Pod undocumented
526 8 local ($_) = validate_pos @_, { regex => HEX } ;
527  
528 6 ▶ $_ = '0'x(8-(length||return '')) . $_ ;
Condition100%2/2
length $_ || (return '')
l!l
51
MC/DC100%1/1
length $_ || (return '')
length $_
Truth table100%2/2
A
A= length $_
Aresult
11
00
529 5 ($_=unpack 'B32', pack 'H*', $_ ) =~ s/^0*(?!$)// , $_ ;
530 }
531 10 ▶sub b2x {
Subroutine b2x: called CC=2 SCAR=6.9
Pod undocumented
532 10 local ($_) = validate_pos @_, { regex => BINARY } ;
533  
534 8 ▶ $_ = '0'x(32-(length||return '')) . $_ ;
Condition100%2/2
length $_ || (return '')
l!l
71
MC/DC100%1/1
length $_ || (return '')
length $_
Truth table100%2/2
A
A= length $_
Aresult
11
00
535 7 ($_ = unpack 'H*', pack 'B32', $_ ) =~ s/^ 0* (?!$) //x , $_;
536 }
537 7 ▶sub x2c {
Subroutine x2c: called CC=2 SCAR=6.9
Pod undocumented
538 7 validate_pos @_ , { regex => HEX } ;
539 5 ▶ unpack 'A*', pack 'H*', (shift||return '');
Condition100%2/2
shift() || (return '')
l!l
32
MC/DC100%1/1
shift() || (return '')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
540 }
541 5 ▶sub x2d {
Subroutine x2d: called CC=1 SCAR=0.0
Pod undocumented
542 5 validate_pos @_ , { regex => HEX } ;
543 4 hex shift;
544 }
545 4 ▶sub c2x {
Subroutine c2x: called CC=1 SCAR=0.0
Pod undocumented
546 4 pack '(A*)*', unpack '(H2)*', shift;
547 }
548 6 ▶sub d2b {
Subroutine d2b: called CC=1 SCAR=0.0
Pod undocumented
549 6 validate_pos @_ , { regex => NONNEGATIVE };
550  
551 4 local $_ = unpack 'B32'x8, pack 'N', shift ;
552 4 s/^0*//g ;
553 4 '0' x (8-(length)%8) . $_;
554 }
↑ Summary