Corvus/Json.pm

filter: Statementstmt 92.6% 213 / 230 Branchbran 76.3% 142 / 186 Conditioncond 53.8% 49 / 91 MC/DCmcdc 34.6% 18 / 52 Subroutinesub 94.5% 35 / 37 Podpod 0.0% 0 / 11 totaltotal 75.2% 457 / 607 CC 126 SCAR 28.1
CC
126
Cov
80%
from_data
37.3
_string
22.5
_value
22.3
CRAP 16.6
« Corvus/Pattern.pm ↑ Summary Corvus/Node.pm »
1 package JSON::Schema::Corvus::Json;
2  
3 # The JSON values the evaluator works on, and a parser that produces them.
4 #
5 # A JSON value is a Perl value of one of these forms, which the parser and from_data produce and nothing else changes:
6 # null undef
7 # boolean $TRUE or $FALSE (JSON::PP::Boolean, so that JSON::PP and other modules read them as booleans)
8 # object an unblessed hash reference
9 # array an unblessed array reference
10 # number a scalar created as a number and never used as a string (an integer from an integer literal, a floating
11 # point number, created as one, from any other, so that writing it again gives 1.0 for 1.0), or a
12 # Math::BigInt for an integer beyond 64 bits
13 # string a scalar created as a string
14 # So a number is told from a string by how the scalar was created (is_number), never by what the text looks like.
15 #
16 # An object's names have no order in a Perl hash. Where the order matters (a schema's keywords, which decide the order
17 # of results, and an instance evaluated with a results collector), the parser records it (ordered mode), and names()
18 # gives the names in that order, or in sorted order for a hash with none recorded (Perl data).
19  
20 4 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Hash::Util::FieldHash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # builtin::created_as_number (5.36 and later) is still experimental, and its warning category is unknown before.
30 4 ▶no if $] >= 5.036, warnings => 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 our $TRUE = $JSON::PP::true;
33 4 our $FALSE = $JSON::PP::false;
34  
35 # The order of an object's names, for objects parsed in ordered mode. Keyed by the hash itself: an entry goes when its
36 # hash does.
37 4 Hash::Util::FieldHash::fieldhash(my %ORDER);
38  
39 # Whether a scalar was created as a number (and has not been used as a string since), as JSON::PP decides it.
40 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.5
41 4 ▶ if (defined &builtin::created_as_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &builtin::created_as_number) { } 4 0 4
42 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 *_created_as_number = \&builtin::created_as_number;
44 }
45 else {
46 0 ▶ *_created_as_number = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
47 0 my $flags = B::svref_2object(\$_[0])->FLAGS;
48 0 ▶ return ($flags & (B::SVp_IOK | B::SVp_NOK)) && !($flags & B::SVp_POK) ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 0 0 0
Condition0%0/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table0%0/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
49 0 };
50 }
51 }
52  
53 1596 ▶sub is_number {
Subroutine is_number: called CC=3 SCAR=11.4
Pod undocumented
54 1596 my $r = ref $_[0];
55 1596 ▶ return $r eq 'Math::BigInt' if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 1596 1596
56 1596 ▶ return defined $_[0] && _created_as_number($_[0]);
Condition66%2/3
defined $_[0] && &_created_as_number($_[0])
!ll&&!rl&&r
1584012
MC/DC50%1/2
defined $_[0] && &_created_as_number($_[0])
defined $_[0] &_created_as_number($_[0])
Truth table66%2/3
A && B
A= defined $_[0]B= &_created_as_number($_[0])
ABresult
0X0
100
111
57 }
58  
59 1261 ▶sub is_string {
Subroutine is_string: called CC=3 SCAR=11.7
Pod undocumented
60 1261 ▶ return !ref $_[0] && defined $_[0] && !_created_as_number($_[0]);
Condition66%4/6
!ref($_[0]) && defined $_[0] && !&_created_as_number($_[0])
!ll&&!rl&&r
10810180
not ref $_[0] and defined $_[0]
!ll&&!rl&&r
01081180
MC/DC33%1/3
!ref($_[0]) && defined $_[0] && !&_created_as_number($_[0])
not ref $_[0] defined $_[0] !&_created_as_number($_[0])
Truth table50%2/4
not(A) and B && not(C)
A= not ref $_[0]B= defined $_[0]C= !&_created_as_number($_[0])
ABCresult
0XX0
10X0
1100
1111
61 }
62  
63 306 ▶sub is_bool { ref $_[0] eq 'JSON::PP::Boolean' }
Subroutine is_bool: called CC=1 SCAR=0.0
Pod undocumented
64  
65 # Whether a number is an integer: a whole number, in every draft. (Draft 4 says an integer has no fraction or exponent
66 # in its text, but Perl's numeric operations change the flags that would tell 1.0 from 1, so a parsed document could
67 # answer differently the second time; the C library, and so JSON::Schema::Corvus::XS, takes 1.0 as an integer in every
68 # draft too.)
69 230 ▶sub is_integer {
Subroutine is_integer: called CC=7 SCAR=25.0
Pod undocumented
70 230 my ($v) = @_;
71 230 ▶ return 1 if ref $v eq 'Math::BigInt';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "Math::BigInt" 0 230 230
72 230 ▶ return '' unless defined $v && !ref $v && _created_as_number($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v and &_created_as_number($v) 0 230 230
Condition33%2/6
defined $v and not ref $v and &_created_as_number($v)
!ll&&!rl&&r
00230
defined $v and not ref $v
!ll&&!rl&&r
00230
MC/DC0%0/3
defined $v and not ref $v and &_created_as_number($v)
defined $v not ref $v &_created_as_number($v)
Truth table25%1/4
A and not(B) and C
A= defined $vB= not ref $vC= &_created_as_number($v)
ABCresult
0XX0
10X0
1100
1111
73 230 ▶ return $v == int($v) && $v - $v == 0 ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$v == int $v && $v - $v == 0 ? : 230 0 230
Condition33%1/3
$v == int $v and $v - $v == 0
!ll&&!rl&&r
00230
MC/DC0%0/2
$v == int $v and $v - $v == 0
$v == int $v $v - $v == 0
Truth table33%1/3
A and B
A= $v == int $vB= $v - $v == 0
ABresult
0X0
100
111
74 }
75  
76 # The JSON type of a value: null, boolean, object, array, number or string.
77 68 ▶sub type_of {
Subroutine type_of: called CC=8 SCAR=23.0
Pod undocumented
78 68 my ($v) = @_;
79 68 ▶ return 'null' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 68 68
80 68 my $r = ref $v;
81 68 ▶ return $r eq 'HASH' ? 'object' : $r eq 'ARRAY' ? 'array' : $r eq 'JSON::PP::Boolean' ? 'boolean'
Branch: 5 branches
BranchTrueFalseTotal
if $r 11 57 68
$r eq 'HASH' ? : 9 2 11
$r eq 'ARRAY' ? : 2 0 2
$r eq 'JSON::PP::Boolean' ? : 0 0 0
$r eq 'Math::BigInt' ? : 0 0 0
82 : $r eq 'Math::BigInt' ? 'number' : Carp::confess("not a JSON value: $r") if $r;
83 57 ▶ return _created_as_number($v) ? 'number' : 'string';
Branch: 1 branch
BranchTrueFalseTotal
&_created_as_number($v) ? : 8 49 57
84 }
85  
86 # An object's names, in the order the text gave them (ordered mode), or sorted.
87 198 ▶sub names {
Subroutine names: called CC=3 SCAR=11.0
Pod undocumented
88 198 my ($hash) = @_;
89 198 my $order = $ORDER{$hash};
90 198 ▶ return $order && @$order == keys %$hash ? @$order : sort keys %$hash;
Branch: 1 branch
BranchTrueFalseTotal
$order && @$order == keys %$hash ? : 182 16 198
Condition66%2/3
$order and @$order == keys %$hash
!ll&&!rl&&r
160182
MC/DC50%1/2
$order and @$order == keys %$hash
$order @$order == keys %$hash
Truth table66%2/3
A and B
A= $orderB= @$order == keys %$hash
ABresult
0X0
100
111
91 }
92  
93 171 ▶sub set_order {
Subroutine set_order: called CC=1 SCAR=0.0
Pod undocumented
94 171 my ($hash, $names) = @_;
95 171 $ORDER{$hash} = $names;
96 171 return $hash;
97 }
98  
99 # --- The parser ---
100  
101 4 my %ESCAPES = ('"' => '"', '\\' => '\\', '/' => '/', b => "\b", f => "\f", n => "\n", r => "\r", t => "\t");
102  
103 # Parses JSON text into a JSON value. The text is characters or UTF-8 bytes (a string with the UTF8 flag off is
104 # decoded as UTF-8). Dies with a JSON::Schema::Corvus::Error::InvalidJson for text that is not JSON, or not UTF-8.
105 # With $ordered, objects keep the order of their names (see names).
106 107 ▶sub decode {
Subroutine decode: called CC=6 SCAR=18.0
Pod undocumented
107 107 my ($text, $ordered) = @_;
108 107 ▶ JSON::Schema::Corvus::Error->throw_usage('the text is undef') unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 106 107
109 106 ▶ if (!utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($text)) 104 2 106
110 104 my $copy = $text;
111 104 ▶ if (!utf8::decode($copy) && $copy =~ /[\x80-\xff]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u) 1 103 104
Condition66%2/3
not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u
!ll&&!rl&&r
10301
MC/DC50%1/2
not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u
not utf8::decode($copy) $copy =~ /[\x80-\xff]/u
Truth table66%2/3
not(A) and B
A= not utf8::decode($copy)B= $copy =~ /[\x80-\xff]/u
ABresult
0X0
100
111
112 1 _invalid_utf8($text);
113 }
114 103 $text = $copy;
115 }
116 105 pos($text) = 0;
117 105 my $value = _value(\$text, $ordered, 0);
118 101 $text =~ /\G[ \t\r\n]*/gc;
119 101 ▶ _fail(\$text, 'unexpected text after the value') if pos($text) < length $text;
Branch: 1 branch
BranchTrueFalseTotal
if pos $text < length $text 0 101 101
120 101 return $value;
121 }
122  
123 1 ▶sub _invalid_utf8 {
Subroutine _invalid_utf8: called CC=2 SCAR=6.9
124 1 my ($bytes) = @_;
125 # The offset of the first byte that does not start or continue a valid UTF-8 sequence.
126 1 my $offset = 0;
127 1 ▶ while ($bytes =~ /\G(?:[\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec\xee\xef][\x80-\xbf]{2}
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes =~ /\G(?:[\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec\xee\xef][\x80-\xbf]{2} |\xed[\x80-\x9f][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})/cgux) 1 1 2
128 |\xed[\x80-\x9f][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})/gcx) {
129 1 $offset = pos $bytes;
130 }
131 1 die JSON::Schema::Corvus::Error::InvalidJson->_new("invalid UTF-8 at byte $offset", $offset);
132 }
133  
134 4 ▶sub _fail {
Subroutine _fail: called CC=2 SCAR=7.0
135 4 my ($text, $what) = @_;
136 4 ▶ my $at = pos($$text) // 0;
Condition50%1/2
pos $$text // 0
l!l
40
MC/DC0%0/1
pos $$text // 0
pos $$text
Truth table50%1/2
A
A= pos $$text
Aresult
11
00
137 # The offset in UTF-8 bytes, as the C library reports it.
138 4 my $prefix = substr($$text, 0, $at);
139 4 utf8::encode($prefix);
140 4 my $offset = length $prefix;
141 4 die JSON::Schema::Corvus::Error::InvalidJson->_new("invalid JSON: $what at byte $offset", $offset);
142 }
143  
144 512 ▶sub _value {
Subroutine _value: called CC=22 SCAR=31.0
145 512 my ($text, $ordered, $depth) = @_;
146 512 ▶ _fail($text, 'the value is nested too deeply') if $depth > 10000;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 10000 0 512 512
147 512 $$text =~ /\G[ \t\r\n]*/gc;
148 512 ▶ if ($$text =~ /\G"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G"/cgu) 229 283 512
149 229 return _string($text);
150 }
151 283 ▶ if ($$text =~ /\G\{/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\{/cgu) 194 89 283
152 194 my %object;
153 my @order;
154 194 $$text =~ /\G[ \t\r\n]*/gc;
155 194 ▶ if ($$text =~ /\G\}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\}/cgu) 8 186 194
156 8 ▶ set_order(\%object, []) if $ordered;
Branch: 1 branch
BranchTrueFalseTotal
if $ordered 5 3 8
157 8 return \%object;
158 }
159 186 while (1) {
160 359 $$text =~ /\G[ \t\r\n]*/gc;
161 359 ▶ $$text =~ /\G"/gc or _fail($text, 'expected a name');
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\G"/cgu or _fail($text, "expected a name") 358 1 359
162 358 my $name = _string($text);
163 358 ▶ $$text =~ /\G[ \t\r\n]*:/gc or _fail($text, 'expected :');
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\G[ \t\r\n]*:/cgu or _fail($text, "expected :") 358 0 358
164 358 ▶ push @order, $name if $ordered && !exists $object{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $ordered and not exists $object{$name} 337 21 358
Condition66%2/3
$ordered and not exists $object{$name}
!ll&&!rl&&r
210337
MC/DC50%1/2
$ordered and not exists $object{$name}
$ordered not exists $object{$name}
Truth table66%2/3
A and not(B)
A= $orderedB= not exists $object{$name}
ABresult
0X0
100
111
165 358 $object{$name} = _value($text, $ordered, $depth + 1);
166 357 $$text =~ /\G[ \t\r\n]*/gc;
167 357 ▶ next if $$text =~ /\G,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G,/cgu 173 184 357
168 184 ▶ last if $$text =~ /\G\}/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G\}/cgu 184 0 184
169 0 _fail($text, 'expected , or }');
170 }
171 184 ▶ set_order(\%object, \@order) if $ordered;
Branch: 1 branch
BranchTrueFalseTotal
if $ordered 166 18 184
172 184 return \%object;
173 }
174 89 ▶ if ($$text =~ /\G\[/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\[/cgu) 21 68 89
175 21 my @array;
176 21 $$text =~ /\G[ \t\r\n]*/gc;
177 21 ▶ return \@array if $$text =~ /\G\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G\]/cgu 1 20 21
178 20 while (1) {
179 49 push @array, _value($text, $ordered, $depth + 1);
180 48 $$text =~ /\G[ \t\r\n]*/gc;
181 48 ▶ next if $$text =~ /\G,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G,/cgu 29 19 48
182 19 ▶ last if $$text =~ /\G\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G\]/cgu 18 1 19
183 1 _fail($text, 'expected , or ]');
184 }
185 18 return \@array;
186 }
187 68 ▶ if ($$text =~ /\G(-?(?:0|[1-9][0-9]*))((?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G(-?(?:0|[1-9][0-9]*))((?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/cgu) 44 24 68
188 44 return _number($1, $2);
189 }
190 24 ▶ return $TRUE if $$text =~ /\Gtrue/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\Gtrue/cgu 18 6 24
191 6 ▶ return $FALSE if $$text =~ /\Gfalse/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\Gfalse/cgu 4 2 6
192 2 ▶ return undef if $$text =~ /\Gnull/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\Gnull/cgu 0 2 2
193 2 ▶ _fail($text, pos($$text) >= length $$text ? 'unexpected end of input' : 'unexpected character');
Branch: 1 branch
BranchTrueFalseTotal
pos $$text >= length $$text ? : 2 0 2
194 }
195  
196 # A number from its integer part and the rest (a fraction and an exponent). An integer literal is an integer (a
197 # Math::BigInt beyond 64 bits); any other is a floating point number, created as one even when it is whole.
198 44 ▶sub _number {
Subroutine _number: called CC=6 SCAR=27.1
199 44 my ($integer, $rest) = @_;
200 44 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 43 1 44
201 # The digits of the largest 64-bit values: shorter integers fit.
202 43 ▶ return 0 + $integer if length($integer) < 19;
Branch: 1 branch
BranchTrueFalseTotal
if length $integer < 19 43 0 43
203 0 my $n = 0 + $integer;
204 0 ▶ return $n if "$n" eq $integer && $n == int($n) && B::svref_2object(\$n)->FLAGS & B::SVf_IOK;
Branch: 1 branch
BranchTrueFalseTotal
if "$n" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256 0 0 0
Condition0%0/6
"$n" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256
!ll&&!rl&&r
000
"$n" eq $integer and $n == int $n
!ll&&!rl&&r
000
MC/DC0%0/3
"$n" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256
"$n" eq $integer $n == int $n B::svref_2object(\$n)->FLAGS & 256
Truth table0%0/4
A and B and C
A= "$n" eq $integerB= $n == int $nC= B::svref_2object(\$n)->FLAGS & 256
ABCresult
0XX0
10X0
1100
1111
205 0 return Math::BigInt->new($integer);
206 }
207 1 return to_double($integer . $rest);
208 }
209  
210 # A decimal number's text as the nearest double, created as a floating point number. Perl reads decimal text correctly
211 # rounded from 5.30; before that, a long mantissa can come back as a neighbouring double, so the C library's strtod
212 # reads it there (Perl keeps the numeric locale of C for it outside 'use locale').
213 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
214 4 ▶ if ($] >= 5.030) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.03) { } 4 0 4
215 4 ▶ *to_double = sub { unpack('d', pack('d', $_[0])) };
Subroutine __ANON__: called CC=1 SCAR=0.0
216 }
217 else {
218 0 require POSIX;
219 0 ▶ *to_double = sub { (POSIX::strtod($_[0]))[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 }
221 }
222  
223 587 ▶sub _string {
Subroutine _string: called CC=10 SCAR=31.1
224 587 my ($text) = @_;
225 587 my $s = '';
226 587 while (1) {
227 608 ▶ if ($$text =~ /\G([^"\\\x00-\x1f]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G([^"\\\x00-\x1f]+)/cgu) 604 4 608
228 604 $s .= $1;
229 }
230 608 ▶ if ($$text =~ /\G"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G"/cgu) 587 21 608
231 587 return $s;
232 }
233 21 ▶ if ($$text =~ /\G\\/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\\/cgu) 21 0 21
234 21 ▶ if ($$text =~ /\G(["\\\/bfnrt])/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$text =~ m[\G(["\\/bfnrt])]cgu) { } 21 0 21
elsif ($$text =~ /\Gu([0-9a-fA-F]{4})/cgu) { } 0 0 0
235 21 $s .= $ESCAPES{$1};
236 }
237 elsif ($$text =~ /\Gu([0-9a-fA-F]{4})/gc) {
238 0 my $unit = hex $1;
239 0 ▶ if ($unit >= 0xd800 && $unit <= 0xdbff && $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu) { } 0 0 0
Condition0%0/6
$unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
!ll&&!rl&&r
000
$unit >= 55296 and $unit <= 56319
!ll&&!rl&&r
000
MC/DC0%0/3
$unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
$unit >= 55296 $unit <= 56319 $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
Truth table0%0/4
A and B and C
A= $unit >= 55296B= $unit <= 56319C= $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
ABCresult
0XX0
10X0
1100
1111
240 0 $s .= chr(0x10000 + (($unit - 0xd800) << 10) + (hex($1) - 0xdc00));
241 }
242 else {
243 # A lone surrogate is kept as its code point, as JavaScript keeps it.
244 4 ▶ no warnings 'surrogate';
Subroutine BEGIN: called CC=1 SCAR=0.0
245 0 $s .= chr $unit;
246 }
247 }
248 else {
249 0 _fail($text, 'invalid escape');
250 }
251 21 next;
252 }
253 0 ▶ _fail($text, pos($$text) >= length $$text ? 'unterminated string' : 'control character in a string');
Branch: 1 branch
BranchTrueFalseTotal
pos $$text >= length $$text ? : 0 0 0
254 }
255 }
256  
257 # --- Perl data ---
258  
259 # Converts Perl data into a JSON value, by the rules JSON::PP follows (see JSON::Schema::Corvus): a copy, in which
260 # every scalar has the form above. Dies for data that has no JSON form.
261 589 ▶sub from_data {
Subroutine from_data: called CC=35 SCAR=36.2
Pod undocumented
262 589 my ($data, $depth) = @_;
263 589 ▶ $depth //= 0;
Condition100%2/2
$depth //= 0
l!l
54742
MC/DC100%1/1
$depth //= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
264 589 ▶ Carp::croak('JSON::Schema::Corvus: the data is nested more than 512 deep (or holds a cycle)') if $depth > 512;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 512 1 588 589
265 588 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 586 588
266 586 ▶ if (my $r = ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = ref $data) 552 34 586
267 552 ▶ return { map { $_ => from_data($data->{$_}, $depth + 1) } keys %$data } if $r eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "HASH" 18 534 552
268 534 ▶ return [ map { from_data($_, $depth + 1) } @$data ] if $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" 518 16 534
269 16 ▶ return $data ? $TRUE : $FALSE if $r eq 'JSON::PP::Boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $r eq "JSON::PP::Boolean" 4 12 16
$data ? : 2 2 4
270 12 ▶ if ($r eq 'SCALAR' && !ref $$data && defined $$data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "SCALAR" and not ref $$data and defined $$data) 2 10 12
Condition66%4/6
$r eq "SCALAR" and not ref $$data and defined $$data
!ll&&!rl&&r
1002
$r eq "SCALAR" and not ref $$data
!ll&&!rl&&r
1002
MC/DC33%1/3
$r eq "SCALAR" and not ref $$data and defined $$data
$r eq "SCALAR" not ref $$data defined $$data
Truth table50%2/4
A and not(B) and C
A= $r eq "SCALAR"B= not ref $$dataC= defined $$data
ABCresult
0XX0
10X0
1100
1111
271 2 ▶ return $TRUE if $$data eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $$data eq 1 1 1 2
272 1 ▶ return $FALSE if $$data eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $$data eq 0 1 0 1
273 }
274 10 ▶ if (Scalar::Util::blessed($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data)) 8 2 10
275 8 ▶ return $data->copy if $data->isa('Math::BigInt') && $data->is_int;
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa("Math::BigInt") and $data->is_int 3 5 8
Condition100%3/3
$data->isa("Math::BigInt") and $data->is_int
!ll&&!rl&&r
413
MC/DC100%2/2
$data->isa("Math::BigInt") and $data->is_int
$data->isa("Math::BigInt") $data->is_int
Truth table100%3/3
A and B
A= $data->isa("Math::BigInt")B= $data->is_int
ABresult
0X0
100
111
276 5 ▶ if ($data->isa('Math::BigInt') || $data->isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa("Math::BigInt") or $data->isa("Math::BigFloat")) 3 2 5
Condition100%3/3
$data->isa("Math::BigInt") or $data->isa("Math::BigFloat")
l!l&&r!l&&!r
122
MC/DC100%2/2
$data->isa("Math::BigInt") or $data->isa("Math::BigFloat")
$data->isa("Math::BigInt") $data->isa("Math::BigFloat")
Truth table100%3/3
A or B
A= $data->isa("Math::BigInt")B= $data->isa("Math::BigFloat")
ABresult
1X1
011
000
277 3 ▶ Carp::croak("JSON::Schema::Corvus: the $r $data has no JSON form") if $data->is_nan || $data->is_inf;
Branch: 1 branch
BranchTrueFalseTotal
if $data->is_nan or $data->is_inf 1 2 3
Condition66%2/3
$data->is_nan or $data->is_inf
l!l&&r!l&&!r
012
MC/DC50%1/2
$data->is_nan or $data->is_inf
$data->is_nan $data->is_inf
Truth table66%2/3
A or B
A= $data->is_nanB= $data->is_inf
ABresult
1X1
011
000
278 2 return _from_decimal("$data");
279 }
280 2 ▶ return $data ? $TRUE : $FALSE if $data->isa('JSON::PP::Boolean');
Branch: 2 branches
BranchTrueFalseTotal
if $data->isa("JSON::PP::Boolean") 0 2 2
$data ? : 0 0 0
281 2 ▶ if (my $to_json = $data->can('TO_JSON')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to_json = $data->can("TO_JSON")) 1 1 2
282 1 my $value = $data->$to_json;
283 1 ▶ Carp::croak("JSON::Schema::Corvus: ${r}::TO_JSON returned the object itself")
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and builtin::refaddr($value) == builtin::refaddr($data) 0 1 1
Condition33%1/3
ref $value and builtin::refaddr($value) == builtin::refaddr($data)
!ll&&!rl&&r
010
MC/DC0%0/2
ref $value and builtin::refaddr($value) == builtin::refaddr($data)
ref $value builtin::refaddr($value) == builtin::refaddr($data)
Truth table33%1/3
A and B
A= ref $valueB= builtin::refaddr($value) == builtin::refaddr($data)
ABresult
0X0
100
111
284 if ref $value && Scalar::Util::refaddr($value) == Scalar::Util::refaddr($data);
285 1 return from_data($value, $depth + 1);
286 }
287 1 Carp::croak("JSON::Schema::Corvus: an object of class $r has no JSON form (it has no TO_JSON method)");
288 }
289 2 Carp::croak('JSON::Schema::Corvus: a reference that is not to a hash or an array has no JSON form '
290 . '(\1 and \0 are true and false)');
291 }
292 34 ▶ if (defined &builtin::is_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &builtin::is_bool) 34 0 34
293 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
294 34 ▶ return $data ? $TRUE : $FALSE if builtin::is_bool($data);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($data) 2 32 34
$data ? : 1 1 2
295 }
296 # A number by JSON::PP's rule: a scalar with a number and no string, by its flags (builtin::created_as_number says
297 # number for a number that has since been used as a string, which JSON::PP writes as a string).
298 32 my $flags = B::svref_2object(\$data)->FLAGS;
299 32 ▶ if (($flags & (B::SVp_IOK | B::SVp_NOK)) && !($flags & B::SVp_POK)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 12288 and not $flags & 16384) 14 18 32
Condition100%3/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
17114
MC/DC100%2/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table100%3/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
300 14 my $copy = $data;
301 14 ▶ Carp::croak('JSON::Schema::Corvus: ' . ($copy != $copy ? 'NaN' : 'an infinite number') . ' has no JSON form')
Branch: 2 branches
BranchTrueFalseTotal
if $copy != $copy or $copy == $copy * 2 and $copy != 0 1 13 14
$copy != $copy ? : 0 1 1
Condition66%4/6
$copy != $copy or $copy == $copy * 2 and $copy != 0
l!l&&r!l&&!r
0113
$copy == $copy * 2 and $copy != 0
!ll&&!rl&&r
1301
MC/DC33%1/3
$copy != $copy or $copy == $copy * 2 and $copy != 0
$copy != $copy $copy == $copy * 2 $copy != 0
Truth table50%2/4
A or B and C
A= $copy != $copyB= $copy == $copy * 2C= $copy != 0
ABCresult
1XX1
0111
00X0
0100
302 if $copy != $copy || ($copy == $copy * 2 && $copy != 0);
303 # A plain copy, which keeps how the number was created (arithmetic would make a whole floating point number an
304 # integer).
305 13 return $copy;
306 }
307 18 return "$data";
308 }
309  
310 # A number from its decimal text (a Math::BigFloat's): an integer when it has no fraction, a floating point number
311 # otherwise.
312 2 ▶sub _from_decimal {
Subroutine _from_decimal: called CC=4 SCAR=14.1
313 2 my ($text) = @_;
314 2 ▶ return Math::BigInt->new($text) if $text =~ /\A-?[0-9]+\z/ && length($text) >= 19;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-?[0-9]+\z/u and length $text >= 19 1 1 2
Condition66%2/3
$text =~ /\A-?[0-9]+\z/u and length $text >= 19
!ll&&!rl&&r
101
MC/DC50%1/2
$text =~ /\A-?[0-9]+\z/u and length $text >= 19
$text =~ /\A-?[0-9]+\z/u length $text >= 19
Truth table66%2/3
A and B
A= $text =~ /\A-?[0-9]+\z/uB= length $text >= 19
ABresult
0X0
100
111
315 1 ▶ return 0 + $text if $text =~ /\A-?[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-?[0-9]+\z/u 0 1 1
316 1 return to_double($text);
317 }
318  
319 # --- Writing JSON text ---
320  
321 # The JSON text of a value: names in the order names() gives (canonical for Perl data), UTF-8 characters (not bytes).
322 38 ▶sub encode {
Subroutine encode: called CC=10 SCAR=23.0
Pod undocumented
323 38 my ($v) = @_;
324 38 ▶ return 'null' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2 36 38
325 36 my $r = ref $v;
326 36 ▶ if ($r eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "HASH") 8 28 36
327 8 return '{' . join(',', map { _quote($_) . ':' . encode($v->{$_}) } names($v)) . '}';
328 }
329 28 ▶ return '[' . join(',', map { encode($_) } @$v) . ']' if $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" 3 25 28
330 25 ▶ return $v ? 'true' : 'false' if $r eq 'JSON::PP::Boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $r eq "JSON::PP::Boolean" 6 19 25
$v ? : 3 3 6
331 19 ▶ return "$v" if $r eq 'Math::BigInt';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "Math::BigInt" 3 16 19
332 16 ▶ if (_created_as_number($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_created_as_number($v)) 9 7 16
333 9 my $text = number_text($v);
334 # A floating point number that is whole keeps a fraction, so that reading the text again gives a floating point
335 # number (as Python's json writes it).
336 9 ▶ $text .= '.0' if $text =~ /\A-?[0-9]+\z/ && !(B::svref_2object(\$v)->FLAGS & B::SVf_IOK);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256 0 9 9
Condition66%2/3
$text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256
!ll&&!rl&&r
270
MC/DC0%0/2
$text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256
$text =~ /\A-?[0-9]+\z/u not B::svref_2object(\$v)->FLAGS & 256
Truth table66%2/3
A and not(B)
A= $text =~ /\A-?[0-9]+\z/uB= not B::svref_2object(\$v)->FLAGS & 256
ABresult
0X0
100
111
337 9 return $text;
338 }
339 7 return _quote($v);
340 }
341  
342 # The text of a number: the shortest that reads back as the same double (as JavaScript and Python write it).
343 84 ▶sub number_text {
Subroutine number_text: called CC=10 SCAR=25.1
Pod undocumented
344 84 my ($n) = @_;
345 84 ▶ return "$n" if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 84 84
346 84 ▶ return sprintf('%d', $n) if $n == int($n) && abs($n) < 1e15 && B::svref_2object(\$n)->FLAGS & B::SVf_IOK;
Branch: 1 branch
BranchTrueFalseTotal
if $n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256 81 3 84
Condition83%5/6
$n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256
!ll&&!rl&&r
3081
$n == int $n and abs $n < "1e+15"
!ll&&!rl&&r
2181
MC/DC66%2/3
$n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256
$n == int $n abs $n < "1e+15" B::svref_2object(\$n)->FLAGS & 256
Truth table75%3/4
A and B and C
A= $n == int $nB= abs $n < "1e+15"C= B::svref_2object(\$n)->FLAGS & 256
ABCresult
0XX0
10X0
1100
1111
347 3 ▶ return 'null' if $n != $n || ($n == $n * 2 && $n != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n != $n or $n == $n * 2 and $n != 0 0 3 3
Condition33%2/6
$n != $n or $n == $n * 2 and $n != 0
l!l&&r!l&&!r
003
$n == $n * 2 and $n != 0
!ll&&!rl&&r
300
MC/DC0%0/3
$n != $n or $n == $n * 2 and $n != 0
$n != $n $n == $n * 2 $n != 0
Truth table25%1/4
A or B and C
A= $n != $nB= $n == $n * 2C= $n != 0
ABCresult
1XX1
0111
00X0
0100
348 3 for my $precision (15 .. 17) {
349 5 my $text = sprintf("%.${precision}g", $n);
350 5 ▶ return _tidy($text) if to_double($text) == $n;
Branch: 1 branch
BranchTrueFalseTotal
if to_double($text) == $n 3 2 5
351 }
352 0 return _tidy(sprintf('%.17g', $n));
353 }
354  
355 3 ▶sub _tidy {
Subroutine _tidy: called CC=1 SCAR=0.0
356 3 my ($text) = @_;
357 3 $text =~ s/e([+-])0*([0-9])/e$1$2/;
358 3 $text =~ s/e\+/e+/;
359 3 return $text;
360 }
361  
362 20 ▶sub _quote {
Subroutine _quote: called CC=6 SCAR=18.1
363 20 my ($s) = @_;
364 20 ▶ $s =~ s/(["\\\x00-\x1f])/$1 eq '"' ? '\\"' : $1 eq '\\' ? '\\\\' : $1 eq "\n" ? '\\n' : $1 eq "\t" ? '\\t'
Branch: 5 branches
BranchTrueFalseTotal
$1 eq '"' ? : 1 3 4
$1 eq '\\' ? : 1 2 3
$1 eq "\n" ? : 0 2 2
$1 eq "\t" ? : 1 1 2
$1 eq "\r" ? : 0 1 1
365 : $1 eq "\r" ? '\\r' : sprintf('\\u%04x', ord $1)/ge;
366 20 return qq{"$s"};
367 }
368  
369 4 1;
« Corvus/Pattern.pm ↑ Summary Corvus/Node.pm »