LIVR/Rules/Types.pm

filter: Statementstmt 96.0% 172 / 179 Branchbran 90.1% 128 / 142 Conditioncond 93.2% 55 / 59 MC/DCmcdc 91.4% 32 / 35 Subroutinesub 100% 29 / 29 Podpod 0.0% 0 / 14 totaltotal 90.8% 416 / 458 CC 98 SCAR 19.6
CC
98
Cov
93%
__ANON__
13.0
__ANON__
10.0
is_number
9.5
CRAP 7.1
« LIVR/Rules/Special.pm ↑ Summary LIVR/Rules/Conditional.pm »
1 package Validator::LIVR::Rules::Types;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.22';
7  
8 # Perl 5.36 and later tell numbers from strings with builtin::created_as_number
9 2 ▶BEGIN { warnings->unimport('experimental::builtin') if $] >= 5.036 }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.036" 2 0 2
10 2 ▶use constant HAS_CREATED_AS_NUMBER => defined &builtin::created_as_number;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # LIVR 2.5. Rules of this module check their arguments when the validator is built: a wrong
13 # argument dies, as an unknown rule does. Booleans of JSON decoders are objects, such as
14 # JSON::PP::Boolean: primitive values with the string form "true" or "false"
15  
16 # JSON::PP::true and JSON::PP::false, which JSON encoders write as true and false. JSON::PP takes
17 # milliseconds to load, so without it the objects are made as JSON::PP makes them
18 2 my ( $TRUE, $FALSE );
19  
20 2 ▶sub _init_booleans {
Subroutine _init_booleans: called CC=2 SCAR=7.5
21 2 require JSON::PP::Boolean;
22  
23 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶ if ( defined $JSON::PP::true ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $JSON::PP::true) { } 2 0 2
25 2 ( $TRUE, $FALSE ) = ( $JSON::PP::true, $JSON::PP::false );
26 } else {
27 0 $TRUE = bless \( my $true = 1 ), 'JSON::PP::Boolean';
28 0 $FALSE = bless \( my $false = 0 ), 'JSON::PP::Boolean';
29 }
30 }
31  
32 61 ▶sub boolean {
Subroutine boolean: called CC=3 SCAR=11.0
Pod undocumented
33 61 ▶ die "Rule [boolean] takes no arguments\n" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 6 55 61
34 55 ▶ _init_booleans() unless $TRUE;
Branch: 1 branch
BranchTrueFalseTotal
unless $TRUE 2 53 55
35  
36 54 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
37 54 my ( $value, undef, $output_ref ) = @_;
38 54 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 9 45 54
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
5445
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
39  
40 45 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 8 37 45
41 8 ▶ return 'FORMAT_ERROR' if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 4 4 8
42 4 ▶ $$output_ref = $value ? $TRUE : $FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 4 0 4
43 4 return;
44 }
45  
46 # A number 1.0 is "1", but the string "1.0" is not a boolean
47 37 my $string = string_form($value);
48 37 ▶ if ( $string eq '1' || $string eq 'true' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($string eq '1' or $string eq 'true') { } 11 26 37
elsif ($string eq '0' or $string eq 'false') { } 10 16 26
Condition100%3/3
$string eq 1 or $string eq "true"
l!l&&r!l&&!r
8326
MC/DC100%2/2
$string eq 1 or $string eq "true"
$string eq 1 $string eq "true"
Truth table100%3/3
A or B
A= $string eq 1B= $string eq "true"
ABresult
1X1
011
000
49 11 $$output_ref = $TRUE;
50 ▶ } elsif ( $string eq '0' || $string eq 'false' ) {
Condition100%3/3
$string eq 0 or $string eq "false"
l!l&&r!l&&!r
8216
MC/DC100%2/2
$string eq 0 or $string eq "false"
$string eq 0 $string eq "false"
Truth table100%3/3
A or B
A= $string eq 0B= $string eq "false"
ABresult
1X1
011
000
51 10 $$output_ref = $FALSE;
52 } else {
53 16 return 'NOT_BOOLEAN';
54 }
55  
56 21 return;
57 55 };
58 }
59  
60 47 ▶sub not_one_of {
Subroutine not_one_of: called CC=4 SCAR=13.9
Pod undocumented
61 47 my @values = @_;
62 47 pop @values; # rule_builders
63  
64 47 ▶ die "Rule [not_one_of] takes one or more values\n" if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1 46 47
65  
66 # By the string forms, so 1 equals "1" and true equals "true"
67 46 my %is_forbidden;
68 46 foreach my $value (@values) {
69 67 my $string = primitive_string($value);
70 67 ▶ die "Rule [not_one_of] takes strings, numbers or booleans\n" if !defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 4 63 67
71  
72 63 $is_forbidden{$string} = 1;
73 }
74  
75 42 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
76 42 my $value = shift;
77 42 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 7 35 42
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
3435
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
78  
79 35 my $string;
80 35 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 6 29 35
81 6 ▶ return 'FORMAT_ERROR' if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 4 2 6
82 2 ▶ $string = $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 0 2
83 } else {
84 29 $string = string_form($value);
85 }
86  
87 31 ▶ return 'NOT_ALLOWED_VALUE' if $is_forbidden{$string};
Branch: 1 branch
BranchTrueFalseTotal
if $is_forbidden{$string} 17 14 31
88 14 return;
89 42 };
90 }
91  
92 57 ▶sub max_decimals {
Subroutine max_decimals: called CC=3 SCAR=11.0
Pod undocumented
93 # Arguments after the first one are ignored. The last one is the rule builders
94 57 my ($max_decimals) = @_;
95 57 ▶ die "Rule [max_decimals] takes a non-negative integer\n" if @_ < 2 || !is_length($max_decimals);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 or not is_length($max_decimals) 4 53 57
Condition100%3/3
@_ < 2 or not is_length($max_decimals)
l!l&&r!l&&!r
1353
MC/DC100%2/2
@_ < 2 or not is_length($max_decimals)
@_ < 2 not is_length($max_decimals)
Truth table100%3/3
A or not(B)
A= @_ < 2B= not is_length($max_decimals)
ABresult
1X1
011
000
96 53 require B if !HAS_CREATED_AS_NUMBER;
97  
98 55 ▶ return sub {
Subroutine __ANON__: called CC=13 SCAR=25.6
99 55 my ( $value, undef, $output_ref ) = @_;
100 55 ▶ return if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 51 55
101  
102 51 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 5 46 51
103 5 ▶ return ref($value) =~ /Boolean/ ? 'NOT_NUMBER' : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 1 4 5
104 }
105  
106 # Before $value is used as a string: Perl before 5.36 marks a stringified number as a string
107 46 my $is_number = is_number($value);
108 46 ▶ return if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 2 44 46
109  
110 # A number in its string form, so 1e-7 has 7 decimals. A string as it is written: "1e-7"
111 # is not a number
112 44 ▶ my $string = $is_number ? number_string($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$is_number ? : 21 23 44
113 44 ▶ return 'NOT_NUMBER' unless $string =~ /\A-?[0-9]+(?:\.([0-9]+))?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\A-?[0-9]+(?:\.([0-9]+))?\z/ 6 38 44
114  
115 # Trailing zeros do not count: "1.230" has 2 decimals, as the number 1.230
116 38 my $decimals = 0;
117 38 ▶ if ( defined $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 29 9 38
118 29 my $fraction = $1;
119 29 $decimals = length($fraction);
120 29 ▶ $decimals-- while $decimals && substr( $fraction, $decimals - 1, 1 ) eq '0';
Branch: 1 branch
BranchTrueFalseTotal
$decimals and substr($fraction, $decimals - 1, 1) eq 0 and do { --$decimals } 26 29 55
Condition100%3/3
$decimals and substr($fraction, $decimals - 1, 1) eq 0
!ll&&!rl&&r
22726
MC/DC100%2/2
$decimals and substr($fraction, $decimals - 1, 1) eq 0
$decimals substr($fraction, $decimals - 1, 1) eq 0
Truth table100%3/3
A and B
A= $decimalsB= substr($fraction, $decimals - 1, 1) eq 0
ABresult
0X0
100
111
121 }
122  
123 38 ▶ return 'TOO_MANY_DECIMALS' if $decimals > $max_decimals;
Branch: 1 branch
BranchTrueFalseTotal
if $decimals > $max_decimals 11 27 38
124  
125 # The output is a number: a string too large for a double, such as "1" with 400 zeros,
126 # is not one
127 27 my $number = $value + 0;
128 27 ▶ return 'NOT_NUMBER' if $number - $number != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $number - $number != 0 1 26 27
129  
130 # A number stays as it is: -0 + 0 is 0
131 26 ▶ $$output_ref = $is_number ? $value : $number;
Branch: 1 branch
BranchTrueFalseTotal
$is_number ? : 14 12 26
132 26 return;
133 53 };
134 }
135  
136 51 ▶sub digits {
Subroutine digits: called CC=2 SCAR=6.9
Pod undocumented
137 51 ▶ die "Rule [digits] takes no arguments\n" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 50 51
138  
139 50 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
140 50 my ( $value, undef, $output_ref ) = @_;
141 50 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 6 44 50
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
4244
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
142  
143 44 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 5 39 44
144 5 ▶ return ref($value) =~ /Boolean/ ? 'NOT_DIGITS' : 'FORMAT_ERROR';
Branch: 1 branch
BranchTrueFalseTotal
ref($value) =~ /Boolean/ ? : 1 4 5
145 }
146  
147 # A string, with the leading zeros: "007" stays "007", 7 becomes "7"
148 39 my $string = string_form($value);
149 39 ▶ return 'NOT_DIGITS' unless $string =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\A[0-9]+\z/ 22 17 39
150  
151 17 $$output_ref = $string;
152 17 return;
153 50 };
154 }
155  
156 43 ▶sub starts_with {
Subroutine starts_with: called CC=1 SCAR=0.0
Pod undocumented
157 43 my @prefixes = _strings( 'starts_with', @_ );
158  
159 39 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
160 39 my ( $value, undef, $output_ref ) = @_;
161 39 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 7 32 39
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
4332
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
162  
163 32 my $string;
164 32 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 5 27 32
165 5 ▶ return 'FORMAT_ERROR' if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 4 1 5
166 1 ▶ $string = $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 0 1
167 } else {
168 27 $string = string_form($value);
169 }
170  
171 # rindex() from 0 checks only the start
172 28 foreach my $prefix (@prefixes) {
173 34 ▶ if ( rindex( $string, $prefix, 0 ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($string, $prefix, 0) == 0) 17 17 34
174 17 $$output_ref = $string;
175 17 return;
176 }
177 }
178  
179 11 return 'WRONG_FORMAT';
180 38 };
181 }
182  
183 31 ▶sub ends_with {
Subroutine ends_with: called CC=1 SCAR=0.0
Pod undocumented
184 31 my @suffixes = _strings( 'ends_with', @_ );
185  
186 29 ▶ return sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
187 29 my ( $value, undef, $output_ref ) = @_;
188 29 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 6 23 29
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
3323
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
189  
190 23 my $string;
191 23 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 5 18 23
192 5 ▶ return 'FORMAT_ERROR' if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 4 1 5
193 1 ▶ $string = $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 0 1
194 } else {
195 18 $string = string_form($value);
196 }
197  
198 19 foreach my $suffix (@suffixes) {
199 21 ▶ if ( length($string) >= length($suffix) && substr( $string, -length($suffix) ) eq $suffix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string >= length $suffix and substr($string, -length($suffix)) eq $suffix) 9 12 21
Condition100%3/3
length $string >= length $suffix and substr($string, -length($suffix)) eq $suffix
!ll&&!rl&&r
1119
MC/DC100%2/2
length $string >= length $suffix and substr($string, -length($suffix)) eq $suffix
length $string >= length $suffix substr($string, -length($suffix)) eq $suffix
Truth table100%3/3
A and B
A= length $string >= length $suffixB= substr($string, -length($suffix)) eq $suffix
ABresult
0X0
100
111
200 9 $$output_ref = $string;
201 9 return;
202 }
203 }
204  
205 10 return 'WRONG_FORMAT';
206 29 };
207 }
208  
209 31 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod undocumented
210 31 my @substrings = _strings( 'contains', @_ );
211  
212 29 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
213 29 my ( $value, undef, $output_ref ) = @_;
214 29 ▶ return if !defined($value) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 6 23 29
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
3323
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
215  
216 23 my $string;
217 23 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 5 18 23
218 5 ▶ return 'FORMAT_ERROR' if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 4 1 5
219 1 ▶ $string = $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 0 1
220 } else {
221 18 $string = string_form($value);
222 }
223  
224 19 foreach my $substring (@substrings) {
225 22 ▶ if ( index( $string, $substring ) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($string, $substring) != -1) 12 10 22
226 12 $$output_ref = $string;
227 12 return;
228 }
229 }
230  
231 7 return 'WRONG_FORMAT';
232 28 };
233 }
234  
235 # Any value passes and stays in the output: objects, lists and empty values too
236 32 ▶sub any {
Subroutine any: called CC=2 SCAR=6.9
Pod undocumented
237 32 ▶ die "Rule [any] takes no arguments\n" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 31 32
238 31 ▶ return sub { return };
Subroutine __ANON__: called CC=1 SCAR=0.0
239 }
240  
241 # The arguments of starts_with, ends_with and contains: one or more non-empty strings. A number
242 # is its string form
243 105 ▶sub _strings {
Subroutine _strings: called CC=5 SCAR=16.1
244 105 my ( $rule_name, @strings ) = @_;
245 105 pop @strings; # rule_builders
246  
247 105 ▶ if ( !@strings || grep { !defined($_) || ref($_) || $_ eq '' } @strings ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @strings or grep {not defined $_ or ref $_ or $_ eq "";} @strings) 10 95 105
Condition100%9/9
not @strings or grep {not defined $_ or ref $_ or $_ eq "";} @strings
l!l&&r!l&&!r
2895
!defined($_) || ref $_ || $_ eq ''
l!l&&r!l&&!r
62118
not defined $_ or ref $_
l!l&&r!l&&!r
42120
MC/DC100%2/2
not @strings or grep {not defined $_ or ref $_ or $_ eq "";} @strings
not @strings grep {not defined $_ or ref $_ or $_ eq "";} @strings
MC/DC100%3/3
!defined($_) || ref $_ || $_ eq ''
not defined $_ ref $_ $_ eq ''
Truth table100%3/3
not(A) or B
A= not @stringsB= grep {not defined $_ or ref $_ or $_ eq "";} @strings
ABresult
1X1
011
000
Truth table100%4/4
not(A) or B || C
A= not defined $_B= ref $_C= $_ eq ''
ABCresult
1XX1
01X1
0011
0000
248 10 die "Rule [$rule_name] takes one or more non-empty strings\n";
249 }
250  
251 95 return map { string_form($_) } @strings;
252 }
253  
254 # Helpers for the other LIVR 2.5 modules
255  
256 # The string form of a primitive value: "true" or "false" for a boolean, undef for undef,
257 # an object or a list
258 67 ▶sub primitive_string {
Subroutine primitive_string: called CC=5 SCAR=16.1
Pod undocumented
259 67 my $value = shift;
260 67 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 65 67
261  
262 65 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 4 61 65
263 4 ▶ return undef if ref($value) !~ /Boolean/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($value) =~ /Boolean/ 2 2 4
264 2 ▶ return $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 0 2
265 }
266  
267 61 return string_form($value);
268 }
269  
270 # The string form of a value that is not a reference (LIVR 2.5, section 2.2): a string as it is,
271 # a number as the shortest decimal that reads back as the same number, without an exponent:
272 # 1e15 is "1000000000000000", not "1e+15", and 0.1 + 0.2 is "0.30000000000000004", not "0.3".
273 # Perl before 5.36 cannot tell a number from a string once it is used as a string, so there
274 # numbers keep the string form of Perl
275 1286 ▶sub string_form {
Subroutine string_form: called CC=3 SCAR=13.3
Pod undocumented
276 1286 ▶ return number_string( $_[0] ) if HAS_CREATED_AS_NUMBER && builtin::created_as_number( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if &builtin::created_as_number($_[0]) 199 1087 1286
277  
278 # The number -0 is "0". Perl writes it as "-0", and JSON::PP 4 decodes -0 as it on Perl 5.10.
279 # A string "-0" has no number flags, unless it was used as a number
280 1087 my $string = "$_[0]";
281 1087 return $string if HAS_CREATED_AS_NUMBER || $string ne '-0';
282  
283 0 require B;
284 0 ▶ return B::svref_2object( \$_[0] )->FLAGS & ( B::SVp_IOK() | B::SVp_NOK() ) ? '0' : $string;
Branch: 1 branch
BranchTrueFalseTotal
B::svref_2object(\$_[0])->FLAGS & 12288 ? : 0 0 0
285 }
286  
287 220 ▶sub number_string {
Subroutine number_string: called CC=9 SCAR=22.2
Pod undocumented
288 220 my $number = shift;
289 220 my $string = "$number";
290 220 ▶ return '0' if $string eq '-0';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "-0" 0 220 220
291  
292 # Perl writes 15 digits, and an exponent from 1e15 on. Integers below that are exact
293 220 ▶ return $string if $string !~ /[.eEnN]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /[.eEnN]/ 168 52 220
294  
295 # Compared as a copy: JSON encoders write a string that was used as a number as a number
296 52 foreach my $precision ( 16, 17 ) {
297 54 ▶ last if do { my $copy = $string; $copy == $number };
Branch: 1 branch
BranchTrueFalseTotal
if do { my $copy = $string; $copy == $number } 50 4 54
298 4 $string = sprintf "%.${precision}g", $number;
299 }
300  
301 # 1.5e+20 is "150000000000000000000", 1.5e-07 is "0.00000015"
302 52 ▶ if ( $string =~ /\A(-?)([0-9])(?:\.([0-9]+))?e([-+][0-9]+)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\A(-?)([0-9])(?:\.([0-9]+))?e([-+][0-9]+)\z/) 5 47 52
303 5 ▶ my ( $sign, $digits, $point ) = ( $1, $2 . ( $3 // '' ), $4 + 1 );
Condition50%1/2
$3 // ""
l!l
05
MC/DC0%0/1
$3 // ""
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
304  
305 5 ▶ if ( $point <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($point <= 0) { } 3 2 5
elsif ($point >= length $digits) { } 2 0 2
306 3 $string = $sign . '0.' . ( '0' x -$point ) . $digits;
307 } elsif ( $point >= length($digits) ) {
308 2 $string = $sign . $digits . ( '0' x ( $point - length($digits) ) );
309 } else {
310 0 $string = $sign . substr( $digits, 0, $point ) . '.' . substr( $digits, $point );
311 }
312 }
313  
314 52 return $string;
315 }
316  
317 # A rule set of values, keys or when: a rule name, a hash with a rule or a list of them. null in
318 # place of rules, an empty list and null or a list in a list of rules die. The validator would
319 # die on them too, but with an unclear message
320 486 ▶sub check_rule_set {
Subroutine check_rule_set: called CC=8 SCAR=20.8
Pod undocumented
321 486 my ( $rule_name, $rules ) = @_;
322  
323 486 ▶ die qq{Rule [$rule_name] got null in place of rules, use "any" for no rules\n} if !defined $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 6 480 486
324 480 ▶ return if ref($rules) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules ne "ARRAY" 340 140 480
325  
326 140 ▶ die qq{Rule [$rule_name] got an empty list of rules, use "any" for no rules\n} if !@$rules;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rules 5 135 140
327  
328 135 foreach my $rule (@$rules) {
329 244 ▶ die "Rule [$rule_name] got null in a list of rules\n" if !defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 1 243 244
330 243 ▶ next if ref($rule) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule ne "ARRAY" 238 5 243
331  
332 5 ▶ die "Rule [$rule_name] got " . ( @$rule ? 'a list' : 'an empty list' ) . " inside a list of rules\n";
Branch: 1 branch
BranchTrueFalseTotal
@$rule ? : 1 4 5
333 }
334  
335 129 return;
336 }
337  
338 # A non-negative integer argument, as a length: a number or a string in the grammar of "decimal"
339 # ("2", "2.0" and "02" are 2; " 2", "1e1" and "0x40" are not numbers). Perl cannot tell "4" from 4
340 182 ▶sub is_length {
Subroutine is_length: called CC=5 SCAR=16.1
Pod undocumented
341 182 my $value = shift;
342 182 ▶ return defined($value) && !ref($value) && $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/
Condition100%12/12
defined $value && !ref($value) && $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ && $value == int $value && $value >= 0
!ll&&!rl&&r
74171
defined $value and not ref $value and $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ and $value == int $value
!ll&&!rl&&r
52175
defined $value and not ref $value and $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/
!ll&&!rl&&r
41177
defined $value and not ref $value
!ll&&!rl&&r
31178
MC/DC100%5/5
defined $value && !ref($value) && $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ && $value == int $value && $value >= 0
defined $value not ref $value $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/ $value == int $value $value >= 0
Truth table100%6/6
A and not(B) and C and D && E
A= defined $valueB= not ref $valueC= $value =~ /\A-?[0-9]+(?:\.[0-9]+)?\z/D= $value == int $valueE= $value >= 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
343 && $value == int($value) && $value >= 0;
344 }
345  
346 # A number, not a string, as JSON encoders tell them apart: a decoded JSON number, such as 1.5,
347 # has no string flag, a decoded JSON string has it. Perl before 5.36 sets the flag when a number
348 # is used as a string, so call it before that. There the caller loads B
349 242 ▶sub is_number {
Subroutine is_number: called CC=4 SCAR=22.5
Pod undocumented
350 242 ▶ return builtin::created_as_number( $_[0] ) ? 1 : 0 if HAS_CREATED_AS_NUMBER;
Branch: 1 branch
BranchTrueFalseTotal
&builtin::created_as_number($_[0]) ? : 22 220 242
351  
352 0 my $flags = B::svref_2object( \$_[0] )->FLAGS;
353 0 ▶ return ( $flags & ( B::SVp_IOK() | B::SVp_NOK() ) ) && !( $flags & B::SVf_POK() ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 1024) ? : 0 0 0
Condition0%0/3
$flags & 12288 and not $flags & 1024
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 12288 and not $flags & 1024
$flags & 12288 not $flags & 1024
Truth table0%0/3
A and not(B)
A= $flags & 12288B= not $flags & 1024
ABresult
0X0
100
111
354 }
355  
356 2 1;
357  
358 =head1 NAME
359  
360 Validator::LIVR::Rules::Types - LIVR 2.5 rules of Validator::LIVR for types and strings: boolean, not_one_of, max_decimals, digits, starts_with, ends_with, contains and any
361  
362 =head1 DESCRIPTION
363  
364 This module has the code of these rules, which are documented in
365 L<Validator::LIVR/LIVR 2.5 rules>. Validator::LIVR loads it when a validator
366 uses one of the rules.
367  
368 Each sub is the builder of the rule with the same name. The code is a good
369 example for L<writing your own rules|Validator::LIVR/WRITING RULES IN PERL>.
370  
371 =cut
« LIVR/Rules/Special.pm ↑ Summary LIVR/Rules/Conditional.pm »