lib/Skilletor/JSON.pm

filter: Statementstmt 98.1% 318 / 324 Branchbran 91.1% 175 / 192 Conditioncond 78.0% 78 / 100 MC/DCmcdc 67.8% 40 / 59 Subroutinesub 100% 37 / 37 Podpod 100% 6 / 6 totaltotal 91.0% 654 / 718 CC 136 SCAR 27.1
CC
136
Cov
93%
_write
45.8
js_number
21.1
_number
17.0
CRAP 15.0
« /usr/local/lib/perl5/5.44.0/CPAN/Meta/Validator.pm ↑ Summary lib/Skilletor/URL.pm »
1 package Skilletor::JSON;
2 # ABSTRACT: JSON read and written exactly as the reference implementation does
3  
4 709 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 709 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 709 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 709 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 709 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 709 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 709 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 709 ▶use Skilletor::Error::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 709 ▶use Skilletor::JSON::OrderedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 709 our $VERSION = '0.008000';
16  
17 709 our @EXPORT_OK = qw( js_parse js_stringify js_ordered js_sort_keys js_number js_type );
18  
19 709 ▶use constant CORE_BOOL => defined &builtin::is_bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #### Writing ###########################################################
22  
23 454732 ▶sub js_ordered {
Subroutine js_ordered: called CC=1 SCAR=0.0
Pod documented
24 454732 my ( @pairs ) = @_;
25 454732 tie my %h, 'Skilletor::JSON::OrderedHash', @pairs;
26 454732 return \%h;
27 }
28  
29 # Default Array.prototype.sort: by UTF-16 code units.
30 100542 ▶sub js_sort_keys {
Subroutine js_sort_keys: called CC=2 SCAR=6.9
Pod documented
31 100542 my ( @keys ) = @_;
32 100542 ▶ return sort @keys unless grep { /[^\x{0}-\x{FFFF}]/ } @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/[^\x{0}-\x{FFFF}]/;} @keys 100539 3 100542
33 3 my %unit = map { $_ => _utf16_units($_) } @keys;
34 3 return sort { $unit{$a} cmp $unit{$b} } @keys;
35 }
36  
37 6 ▶sub _utf16_units {
Subroutine _utf16_units: called CC=2 SCAR=6.9
38 6 my ( $s ) = @_;
39 return join '', map {
40 6 my $c = ord;
41 $c > 0xFFFF
42 22 ▶ ? do { my $v = $c - 0x10000; chr( 0xD800 + ( $v >> 10 ) ).chr( 0xDC00 + ( $v & 0x3FF ) ) }
Branch: 1 branch
BranchTrueFalseTotal
$c > 65535 ? : 3 19 22
43 : $_
44 } split //, $s;
45 }
46  
47 # Property order of a plain hash: written as the reference writes a sorted object –
48 # index keys ascending first (JavaScript reorders them), then the rest sorted.
49 100840 ▶sub _hash_keys {
Subroutine _hash_keys: called CC=3 SCAR=11.0
50 100840 my ( $h ) = @_;
51 100840 my $tied = tied %$h;
52 100840 ▶ return $tied->keys_in_order if blessed $tied && $tied->isa('Skilletor::JSON::OrderedHash');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash") 77937 22903 100840
Condition66%2/3
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
!ll&&!rl&&r
22903077937
MC/DC50%1/2
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
builtin::blessed($tied) $tied->isa("Skilletor::JSON::OrderedHash")
Truth table66%2/3
A and B
A= builtin::blessed($tied)B= $tied->isa("Skilletor::JSON::OrderedHash")
ABresult
0X0
100
111
53 22903 my @keys = keys %$h;
54 22903 my @index = sort { $a <=> $b } grep { Skilletor::JSON::OrderedHash->is_index_key($_) } @keys;
55 22903 my @rest = js_sort_keys( grep { !Skilletor::JSON::OrderedHash->is_index_key($_) } @keys );
56 22903 return ( @index, @rest );
57 }
58  
59 126240 ▶sub js_stringify {
Subroutine js_stringify: called CC=6 SCAR=18.5
Pod documented
60 126240 my ( $value, $indent ) = @_;
61 # A number: that many spaces (at most 10); a string: its first 10 characters.
62 126240 ▶ my $gap = !defined $indent ? ''
Branch: 4 branches
BranchTrueFalseTotal
!defined($indent) ? : 100761 25479 126240
!(ref $indent || _is_number($indent)) ? : 723 24756 25479
$indent > 0 ? : 24756 0 24756
$indent > 10 ? : 0 24756 24756
Condition66%2/3
ref $indent || _is_number($indent)
l!l&&r!l&&!r
024756723
MC/DC50%1/2
ref $indent || _is_number($indent)
ref $indent _is_number($indent)
Truth table66%2/3
A || B
A= ref $indentB= _is_number($indent)
ABresult
1X1
011
000
63 : ( !ref $indent && !_is_number($indent) ) ? substr( $indent, 0, 10 )
64 : $indent > 0 ? ' ' x ( $indent > 10 ? 10 : $indent )
65 : '';
66 126240 return _write( $value, $gap, '' );
67 }
68  
69 348829 ▶sub _is_number {
Subroutine _is_number: called CC=3 SCAR=11.0
70 348829 my ( $value ) = @_;
71 348829 my $flags = B::svref_2object( \$value )->FLAGS;
72 348829 ▶ return ( $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 60400 288429 348829
Condition66%2/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
288429060400
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
73 }
74  
75 # typeof, with Array.isArray and null told apart: what a validator asks of a parsed value.
76 384903 ▶sub js_type {
Subroutine js_type: called CC=8 SCAR=20.8
Pod documented
77 384903 my ( $value ) = @_;
78 384903 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 38672 346231 384903
79 346231 ▶ return 'boolean' if JSON::MaybeXS::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 9273 336958 346231
80 336958 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 217689 119269 336958
81 217689 my $type = reftype $value;
82 217689 ▶ return $type eq 'ARRAY' ? 'array' : $type eq 'HASH' ? 'object' : 'other';
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 17347 200342 217689
$type eq 'HASH' ? : 200341 1 200342
83 }
84 119269 if (CORE_BOOL) {
85 709 ▶ no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 119269 ▶ return 'boolean' if builtin::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_bool($value) 0 119269 119269
87 }
88 119269 ▶ return _is_number($value) ? 'number' : 'string';
Branch: 1 branch
BranchTrueFalseTotal
_is_number($value) ? : 11006 108263 119269
89 }
90  
91 348491 ▶sub _write {
Subroutine _write: called CC=25 SCAR=38.3
92 348491 my ( $value, $gap, $current ) = @_;
93 348491 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 7990 340501 348491
94 340501 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 136420 204081 340501
95 136420 ▶ return $value ? 'true' : 'false' if JSON::MaybeXS::is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 3710 132710 136420
$value ? : 3395 315 3710
96 0 ▶ if ( blessed $value && !( tied( %{ $value } ) && reftype $value eq 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH") 0 132710 132710
Condition16%1/6
builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH"
!ll&&!rl&&r
13271000
tied %{$value;} && builtin::reftype($value) eq 'HASH'
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH"
builtin::blessed($value) tied %{$value;} builtin::reftype($value) eq 'HASH'
Truth table25%1/4
A and not(B && C)
A= builtin::blessed($value)B= tied %{$value;}C= builtin::reftype($value) eq 'HASH'
ABCresult
0XX0
1110
10X1
1101
97 0 ▶ return _write( $value->TO_JSON, $gap, $current ) if $value->can('TO_JSON');
Branch: 1 branch
BranchTrueFalseTotal
if $value->can("TO_JSON") 0 0 0
98 0 croak __PACKAGE__.'->js_stringify cannot write a '.ref($value).' object';
99 }
100 132710 my $type = reftype $value;
101 132710 my $inner = $current.$gap;
102 132710 ▶ if ( $type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 31869 100841 132710
103 31869 ▶ return '[]' unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 10607 21262 31869
104 21262 my @parts = map { _write( $_, $gap, $inner ) } @$value;
105 21262 ▶ return '['.join( ',', @parts ).']' if $gap eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $gap eq "" 14532 6730 21262
106 6730 return "[\n".$inner.join( ",\n".$inner, @parts )."\n".$current.']';
107 }
108 100841 ▶ if ( $type eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 100840 1 100841
109 100840 my @keys = _hash_keys($value);
110 100840 ▶ return '{}' unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 9859 90981 100840
111 90981 ▶ my $colon = $gap eq '' ? ':' : ': ';
Branch: 1 branch
BranchTrueFalseTotal
$gap eq '' ? : 24124 66857 90981
112 90981 my @parts = map { _quote($_).$colon._write( $value->{$_}, $gap, $inner ) } @keys;
113 90981 ▶ return '{'.join( ',', @parts ).'}' if $gap eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $gap eq "" 24124 66857 90981
114 66857 return "{\n".$inner.join( ",\n".$inner, @parts )."\n".$current.'}';
115 }
116 1 ▶ if ( $type eq 'SCALAR' && !blessed $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" and not builtin::blessed($value)) 0 1 1
Condition33%1/3
$type eq "SCALAR" and not builtin::blessed($value)
!ll&&!rl&&r
100
MC/DC0%0/2
$type eq "SCALAR" and not builtin::blessed($value)
$type eq "SCALAR" not builtin::blessed($value)
Truth table33%1/3
A and not(B)
A= $type eq "SCALAR"B= not builtin::blessed($value)
ABresult
0X0
100
111
117 0 ▶ return $$value ? 'true' : 'false' if !defined $$value || $$value eq '1' || $$value eq '0';
Branch: 2 branches
BranchTrueFalseTotal
if not defined $$value or $$value eq 1 or $$value eq 0 0 0 0
$$value ? : 0 0 0
Condition0%0/6
not defined $$value or $$value eq 1 or $$value eq 0
l!l&&r!l&&!r
000
not defined $$value or $$value eq 1
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $$value or $$value eq 1 or $$value eq 0
not defined $$value $$value eq 1 $$value eq 0
Truth table0%0/4
not(A) or B or C
A= not defined $$valueB= $$value eq 1C= $$value eq 0
ABCresult
1XX1
01X1
0011
0000
118 }
119 1 croak __PACKAGE__.'->js_stringify cannot write a '.$type.' reference';
120 }
121 204081 if ( CORE_BOOL ) {
122 709 ▶ no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
123 204081 ▶ return $value ? 'true' : 'false' if builtin::is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($value) 0 204081 204081
$value ? : 0 0 0
124 }
125 204081 ▶ return js_number($value) if _is_number($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_number($value) 24638 179443 204081
126 179443 return _quote($value);
127 }
128  
129 709 my %ESCAPE = ( '"' => '\\"', '\\' => '\\\\', "\b" => '\\b', "\f" => '\\f', "\n" => '\\n', "\r" => '\\r', "\t" => '\\t' );
130  
131 # QuoteJSONString: the short escapes, \u00xx for other controls and lone surrogates, nothing
132 # else – no "/" and no non-ASCII escaped.
133 363014 ▶sub _quote {
Subroutine _quote: called CC=2 SCAR=6.9
134 363014 my ( $s ) = @_;
135 363014 $s = "$s";
136 363014 ▶ $s =~ s{([\x{0}-\x{1F}"\\\x{D800}-\x{DFFF}])}{ $ESCAPE{$1} // sprintf( '\\u%04x', ord $1 ) }ge;
Condition100%2/2
$ESCAPE{$1} // sprintf("\\u%04x", ord $1)
l!l
11989873
MC/DC100%1/1
$ESCAPE{$1} // sprintf("\\u%04x", ord $1)
$ESCAPE{$1}
Truth table100%2/2
A
A= $ESCAPE{$1}
Aresult
11
00
137 363014 return '"'.$s.'"';
138 }
139  
140 # Number::toString for a finite double; JSON.stringify writes NaN and the infinities as null.
141 24735 ▶sub js_number {
Subroutine js_number: called CC=21 SCAR=30.5
Pod documented
142 24735 my ( $n ) = @_;
143 24735 $n = 0 + $n;
144 24735 ▶ return 'null' if $n != $n || $n == 9**9**9 || $n == -9**9**9;
Branch: 1 branch
BranchTrueFalseTotal
if $n != $n or $n == "Inf" or $n == "-Inf" 9 24726 24735
Condition100%6/6
$n != $n or $n == "Inf" or $n == "-Inf"
l!l&&r!l&&!r
7224726
$n != $n or $n == "Inf"
l!l&&r!l&&!r
1624728
MC/DC100%3/3
$n != $n or $n == "Inf" or $n == "-Inf"
$n != $n $n == "Inf" $n == "-Inf"
Truth table100%4/4
A or B or C
A= $n != $nB= $n == "Inf"C= $n == "-Inf"
ABCresult
1XX1
01X1
0011
0000
145 24726 ▶ return '0' if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 89 24637 24726
146 24637 my $flags = B::svref_2object( \$n )->FLAGS;
147 24637 ▶ if ( ( $flags & B::SVf_IOK() ) && !( $flags & B::SVf_NOK() ) && abs($n) <= 9007199254740992 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 256 and not $flags & 512 and abs $n <= '9007199254740992') 0 24637 24637
Condition83%5/6
$flags & 256 and not $flags & 512 and abs $n <= '9007199254740992'
!ll&&!rl&&r
2463430
$flags & 256 and not $flags & 512
!ll&&!rl&&r
147244873
MC/DC0%0/3
$flags & 256 and not $flags & 512 and abs $n <= '9007199254740992'
$flags & 256 not $flags & 512 abs $n <= '9007199254740992'
Truth table75%3/4
A and not(B) and C
A= $flags & 256B= not $flags & 512C= abs $n <= '9007199254740992'
ABCresult
0XX0
10X0
1100
1111
148 0 return "$n";
149 }
150 24637 $n = unpack 'd', pack 'd', $n;
151 24637 ▶ my $neg = $n < 0 ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 46 24591 24637
152 24637 my $abs = abs $n;
153 24637 my ( $digits, $exp );
154 24637 for my $p ( 1 .. 17 ) {
155 123851 my $s = sprintf '%.*e', $p - 1, $abs;
156 123851 ▶ next unless ( 0 + $s ) == $abs || $p == 17;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $s == $abs or $p == 17 99214 24637 123851
Condition66%2/3
0 + $s == $abs or $p == 17
l!l&&r!l&&!r
24637099214
MC/DC50%1/2
0 + $s == $abs or $p == 17
0 + $s == $abs $p == 17
Truth table66%2/3
A or B
A= 0 + $s == $absB= $p == 17
ABresult
1X1
011
000
157 24637 ( $digits, $exp ) = $s =~ /\A([0-9.]+)e([-+][0-9]+)\z/;
158 24637 $digits =~ s/\.//;
159 24637 ▶ $digits =~ s/0+\z// if length $digits > 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $digits > 1 12552 12085 24637
160 24637 last;
161 }
162 24637 my $k = length $digits;
163 24637 my $e = $exp + 1; # the decimal point sits after $e digits
164 24637 my $out;
165 24637 ▶ if ( $k <= $e && $e <= 21 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($k <= $e and $e <= 21) { } 24500 137 24637
elsif (0 < $e and $e <= 21) { } 67 70 137
elsif (-6 < $e and $e <= 0) { } 36 34 70
Condition100%3/3
$k <= $e and $e <= 21
!ll&&!rl&&r
1221524500
MC/DC100%2/2
$k <= $e and $e <= 21
$k <= $e $e <= 21
Truth table100%3/3
A and B
A= $k <= $eB= $e <= 21
ABresult
0X0
100
111
166 24500 $out = $digits.( '0' x ( $e - $k ) );
167 }
168 ▶ elsif ( 0 < $e && $e <= 21 ) {
Condition100%3/3
0 < $e and $e <= 21
!ll&&!rl&&r
551567
MC/DC100%2/2
0 < $e and $e <= 21
0 < $e $e <= 21
Truth table100%3/3
A and B
A= 0 < $eB= $e <= 21
ABresult
0X0
100
111
169 67 $out = substr( $digits, 0, $e ).'.'.substr( $digits, $e );
170 }
171 ▶ elsif ( -6 < $e && $e <= 0 ) {
Condition100%3/3
-6 < $e and $e <= 0
!ll&&!rl&&r
191536
MC/DC100%2/2
-6 < $e and $e <= 0
-6 < $e $e <= 0
Truth table100%3/3
A and B
A= -6 < $eB= $e <= 0
ABresult
0X0
100
111
172 36 $out = '0.'.( '0' x -$e ).$digits;
173 }
174 else {
175 34 my $x = $e - 1;
176 34 ▶ my $mant = $k == 1 ? $digits : substr( $digits, 0, 1 ).'.'.substr( $digits, 1 );
Branch: 1 branch
BranchTrueFalseTotal
$k == 1 ? : 21 13 34
177 34 ▶ $out = $mant.'e'.( $x < 0 ? '-' : '+' ).abs($x);
Branch: 1 branch
BranchTrueFalseTotal
$x < 0 ? : 19 15 34
178 }
179 24637 return $neg.$out;
180 }
181  
182 #### Reading ###########################################################
183  
184 # A recursive-descent JSON.parse with V8's error messages. Objects come back as hashes tied
185 # to Skilletor::JSON::OrderedHash, so they keep the property order JavaScript gives them.
186 # Every match runs on $p->{s} itself, never on a copy: a copy of a UTF-8 text has no offset
187 # cache, so each pos() on it scans from the start and a big file parses in quadratic time (k196).
188  
189 58112 ▶sub js_parse {
Subroutine js_parse: called CC=3 SCAR=11.0
Pod documented
190 58112 my ( $text ) = @_;
191 58112 ▶ croak __PACKAGE__.'->js_parse needs a string' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 58112 58112
192 58112 my $p = { s => "$text", i => 0, len => length "$text" };
193 58112 _ws($p);
194 58112 my $value = _value($p);
195 57778 _ws($p);
196 57778 ▶ _fail_at( $p, 'Unexpected non-whitespace character after JSON', undef, 1 ) if $p->{i} < $p->{len};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"i"} < $p->{"len"} 5 57773 57778
197 57773 return $value;
198 }
199  
200 1570005 ▶sub _ws {
Subroutine _ws: called CC=2 SCAR=6.9
201 1570005 my ( $p ) = @_;
202 1570005 pos( $p->{s} ) = $p->{i};
203 1570005 ▶ $p->{i} = pos( $p->{s} ) if $p->{s} =~ /\G[ \t\n\r]+/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"s"} =~ /\G[ \t\n\r]+/cg 884541 685464 1570005
204 }
205  
206 2211670 ▶sub _peek { $_[0]{i} < $_[0]{len} ? substr( $_[0]{s}, $_[0]{i}, 1 ) : undef }
Subroutine _peek: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'i'} < $_[0]{'len'} ? : 2211531 139 2211670
207  
208 10 ▶sub _eos { Skilletor::Error::JSON->throw('Unexpected end of JSON input') }
Subroutine _eos: called CC=1 SCAR=0.0
209  
210 # The position as JavaScript counts it, in UTF-16 code units.
211 310 ▶sub _units {
Subroutine _units: called CC=1 SCAR=0.0
212 310 my ( $p, $i ) = @_;
213 310 my $before = substr( $p->{s}, 0, $i );
214 310 my $astral = () = $before =~ /[^\x{0}-\x{FFFF}]/g;
215 310 return $i + $astral;
216 }
217  
218 307 ▶sub _fail_at {
Subroutine _fail_at: called CC=5 SCAR=16.1
219 307 my ( $p, $what, $i, $bare ) = @_;
220 307 ▶ $i //= $p->{i};
Condition33%1/3
$i //= $p->{"i"}
l!l&&r!l&&!r
03070
MC/DC0%0/2
$i //= $p->{"i"}
$i $p->{"i"}
Truth table33%1/3
A //= B
A= $iB= $p->{"i"}
ABresult
1X1
011
000
221 307 my $before = substr( $p->{s}, 0, $i );
222 307 my $line = 1;
223 307 my $last = -1;
224 307 ▶ while ( $before =~ /\r\n|\r|\n/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ /\r\n|\r|\n/g) 6 307 313
225 6 $line++;
226 6 $last = pos($before) - 1;
227 }
228 307 my $pos = _units( $p, $i );
229 307 ▶ my $column = $last < 0 ? $pos + 1 : $pos - _units( $p, $last );
Branch: 1 branch
BranchTrueFalseTotal
$last < 0 ? : 304 3 307
230 307 ▶ Skilletor::Error::JSON->throw( $what.( $bare ? '' : ' in JSON' ).' at position '.$pos.' (line '.$line.' column '.$column.')' );
Branch: 1 branch
BranchTrueFalseTotal
$bare ? : 5 302 307
231 }
232  
233 22 ▶sub _unexpected_token {
Subroutine _unexpected_token: called CC=6 SCAR=17.9
234 22 my ( $p ) = @_;
235 22 my $i = $p->{i};
236 22 my $tok = substr( $p->{s}, $i, 1 );
237 22 my $len = $p->{len};
238 22 my $context;
239 22 ▶ if ( $len <= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= 20) { } 17 5 22
240 17 $context = '"'.$p->{s}.'"';
241 }
242 else {
243 5 ▶ my $start = $i - 10 < 0 ? 0 : $i - 10;
Branch: 1 branch
BranchTrueFalseTotal
$i - 10 < 0 ? : 2 3 5
244 5 ▶ my $end = $i + 10 > $len ? $len : $i + 10;
Branch: 1 branch
BranchTrueFalseTotal
$i + 10 > $len ? : 2 3 5
245 5 ▶ $context = ( $start > 0 ? '...' : '' ).'"'.substr( $p->{s}, $start, $end - $start ).'"'.( $end < $len ? '...' : '' );
Branch: 2 branches
BranchTrueFalseTotal
$start > 0 ? : 3 2 5
$end < $len ? : 3 2 5
246 }
247 22 Skilletor::Error::JSON->throw( "Unexpected token '".$tok."', ".$context.' is not valid JSON' );
248 }
249  
250 430193 ▶sub _value {
Subroutine _value: called CC=10 SCAR=23.0
251 430193 my ( $p ) = @_;
252 430193 my $c = _peek($p);
253 430193 ▶ _eos() unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 6 430187 430193
254 430187 ▶ return _string($p) if $c eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\"" 193547 236640 430187
255 236640 ▶ return _object($p) if $c eq '{';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "{" 180866 55774 236640
256 55774 ▶ return _array($p) if $c eq '[';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "[" 21757 34017 55774
257 34017 ▶ return _number($p) if $c eq '-' || $c =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "-" or $c =~ /[0-9]/ 22645 11372 34017
Condition100%3/3
$c eq "-" or $c =~ /[0-9]/
l!l&&r!l&&!r
412260411372
MC/DC100%2/2
$c eq "-" or $c =~ /[0-9]/
$c eq "-" $c =~ /[0-9]/
Truth table100%3/3
A or B
A= $c eq "-"B= $c =~ /[0-9]/
ABresult
1X1
011
000
258 11372 ▶ return _literal( $p, 'true', JSON::MaybeXS::true() ) if $c eq 't';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "t" 9585 1787 11372
259 1787 ▶ return _literal( $p, 'false', JSON::MaybeXS::false() ) if $c eq 'f';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "f" 1149 638 1787
260 638 ▶ return _literal( $p, 'null', undef ) if $c eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "n" 623 15 638
261 15 _unexpected_token($p);
262 }
263  
264 11357 ▶sub _literal {
Subroutine _literal: called CC=4 SCAR=13.9
265 11357 my ( $p, $word, $value ) = @_;
266 11357 for my $ch ( split //, $word ) {
267 46573 my $c = _peek($p);
268 46573 ▶ _eos() unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 2 46571 46573
269 46571 ▶ _unexpected_token($p) unless $c eq $ch;
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq $ch 7 46564 46571
270 46564 $p->{i}++;
271 }
272 11348 return $value;
273 }
274  
275 22645 ▶sub _number {
Subroutine _number: called CC=17 SCAR=28.3
276 22645 my ( $p ) = @_;
277 22645 my $start = $p->{i};
278 22645 ▶ if ( _peek($p) eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_peek($p) eq "-") 41 22604 22645
279 41 $p->{i}++;
280 41 my $c = _peek($p);
281 41 ▶ _fail_at( $p, 'No number after minus sign' ) unless defined $c && $c =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c =~ /[0-9]/ 3 38 41
Condition100%3/3
defined $c and $c =~ /[0-9]/
!ll&&!rl&&r
2138
MC/DC100%2/2
defined $c and $c =~ /[0-9]/
defined $c $c =~ /[0-9]/
Truth table100%3/3
A and B
A= defined $cB= $c =~ /[0-9]/
ABresult
0X0
100
111
282 }
283 22642 my $int_start = $p->{i};
284 22642 ▶ if ( _peek($p) eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_peek($p) eq '0') { } 172 22470 22642
285 172 $p->{i}++;
286 172 my $c = _peek($p);
287 172 ▶ _fail_at( $p, 'Unexpected number' ) if defined $c && $c =~ /[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $c and $c =~ /[0-9]/ 4 168 172
Condition100%3/3
defined $c and $c =~ /[0-9]/
!ll&&!rl&&r
21664
MC/DC100%2/2
defined $c and $c =~ /[0-9]/
defined $c $c =~ /[0-9]/
Truth table100%3/3
A and B
A= defined $cB= $c =~ /[0-9]/
ABresult
0X0
100
111
288 }
289 else {
290 22470 pos( $p->{s} ) = $p->{i};
291 22470 $p->{s} =~ /\G[0-9]+/gc;
292 22470 $p->{i} = pos( $p->{s} );
293 }
294 22638 my $simple = 1;
295 22638 ▶ if ( ( _peek($p) // '' ) eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((_peek($p) // "") eq ".") 39 22599 22638
Condition100%2/2
_peek($p) // ""
l!l
2261226
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
296 39 $simple = 0;
297 39 $p->{i}++;
298 39 pos( $p->{s} ) = $p->{i};
299 39 ▶ _fail_at( $p, 'Unterminated fractional number' ) unless $p->{s} =~ /\G[0-9]+/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"s"} =~ /\G[0-9]+/cg 2 37 39
300 37 $p->{i} = pos( $p->{s} );
301 }
302 22636 ▶ if ( ( _peek($p) // '' ) =~ /\A[eE]\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((_peek($p) // "") =~ /\A[eE]\z/) 17 22619 22636
Condition100%2/2
_peek($p) // ""
l!l
2260036
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
303 17 $simple = 0;
304 17 $p->{i}++;
305 17 ▶ $p->{i}++ if ( _peek($p) // '' ) =~ /\A[-+]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if (_peek($p) // "") =~ /\A[-+]\z/ 7 10 17
Condition100%2/2
_peek($p) // ""
l!l
161
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
306 17 pos( $p->{s} ) = $p->{i};
307 17 ▶ _fail_at( $p, 'Exponent part is missing a number' ) unless $p->{s} =~ /\G[0-9]+/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"s"} =~ /\G[0-9]+/cg 2 15 17
308 15 $p->{i} = pos( $p->{s} );
309 }
310 22634 my $text = substr( $p->{s}, $start, $p->{i} - $start );
311 22634 my $n = 0 + $text;
312 # A JavaScript number is a double: an integer beyond 2**53 loses its low digits there.
313 22634 ▶ $n = unpack 'd', pack 'd', $n unless $simple && $p->{i} - $int_start <= 15;
Branch: 1 branch
BranchTrueFalseTotal
unless $simple and $p->{"i"} - $int_start <= 15 55 22579 22634
Condition100%3/3
$simple and $p->{"i"} - $int_start <= 15
!ll&&!rl&&r
49622579
MC/DC100%2/2
$simple and $p->{"i"} - $int_start <= 15
$simple $p->{"i"} - $int_start <= 15
Truth table100%3/3
A and B
A= $simpleB= $p->{"i"} - $int_start <= 15
ABresult
0X0
100
111
314 22634 return $n;
315 }
316  
317 709 my %UNESCAPE = ( '"' => '"', '\\' => '\\', '/' => '/', b => "\b", f => "\f", n => "\n", r => "\r", t => "\t" );
318  
319 536952 ▶sub _string {
Subroutine _string: called CC=13 SCAR=25.7
320 536952 my ( $p ) = @_;
321 536952 $p->{i}++; # the opening quote
322 536952 my $out = '';
323 536952 while (1) {
324 549425 pos( $p->{s} ) = $p->{i};
325 549425 ▶ if ( $p->{s} =~ /\G([^"\\\x{0}-\x{1F}]+)/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"s"} =~ /\G([^"\\\x{0}-\x{1F}]+)/cg) 546412 3013 549425
326 546412 $out .= $1;
327 546412 $p->{i} = pos( $p->{s} );
328 }
329 549425 my $c = _peek($p);
330 549425 ▶ _fail_at( $p, 'Unterminated string' ) unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 1 549424 549425
331 549424 ▶ if ( $c eq '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\"") 536943 12481 549424
332 536943 $p->{i}++;
333 536943 return $out;
334 }
335 12481 ▶ _fail_at( $p, 'Bad control character in string literal' ) if ord($c) < 0x20;
Branch: 1 branch
BranchTrueFalseTotal
if ord $c < 32 2 12479 12481
336 # a backslash
337 12479 $p->{i}++;
338 12479 my $e = _peek($p);
339 12479 ▶ _eos() unless defined $e;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 2 12477 12479
340 12477 ▶ if ( exists $UNESCAPE{$e} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $UNESCAPE{$e}) 12433 44 12477
341 12433 $out .= $UNESCAPE{$e};
342 12433 $p->{i}++;
343 12433 next;
344 }
345 44 ▶ _fail_at( $p, 'Bad escaped character' ) unless $e eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
unless $e eq "u" 1 43 44
346 43 $p->{i}++;
347 43 my $code = _hex4($p);
348 40 ▶ if ( $code >= 0xD800 && $code <= 0xDBFF && substr( $p->{s}, $p->{i}, 2 ) eq '\\u' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 55296 and $code <= 56319 and substr($p->{"s"}, $p->{"i"}, 2) eq "\\u") 2 38 40
Condition100%6/6
$code >= 55296 and $code <= 56319 and substr($p->{"s"}, $p->{"i"}, 2) eq "\\u"
!ll&&!rl&&r
3622
$code >= 55296 and $code <= 56319
!ll&&!rl&&r
3514
MC/DC100%3/3
$code >= 55296 and $code <= 56319 and substr($p->{"s"}, $p->{"i"}, 2) eq "\\u"
$code >= 55296 $code <= 56319 substr($p->{"s"}, $p->{"i"}, 2) eq "\\u"
Truth table100%4/4
A and B and C
A= $code >= 55296B= $code <= 56319C= substr($p->{"s"}, $p->{"i"}, 2) eq "\\u"
ABCresult
0XX0
10X0
1100
1111
349 2 my $save = $p->{i};
350 2 $p->{i} += 2;
351 2 my $low = _hex4($p);
352 2 ▶ if ( $low >= 0xDC00 && $low <= 0xDFFF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low >= 56320 and $low <= 57343) 2 0 2
Condition33%1/3
$low >= 56320 and $low <= 57343
!ll&&!rl&&r
002
MC/DC0%0/2
$low >= 56320 and $low <= 57343
$low >= 56320 $low <= 57343
Truth table33%1/3
A and B
A= $low >= 56320B= $low <= 57343
ABresult
0X0
100
111
353 2 $out .= chr( 0x10000 + ( ( $code - 0xD800 ) << 10 ) + ( $low - 0xDC00 ) );
354 2 next;
355 }
356 0 $p->{i} = $save;
357 }
358 709 ▶ { no warnings qw( surrogate utf8 ); $out .= chr $code; }
Subroutine BEGIN: called CC=1 SCAR=0.0
359 }
360 }
361  
362 45 ▶sub _hex4 {
Subroutine _hex4: called CC=4 SCAR=13.9
363 45 my ( $p ) = @_;
364 45 my $v = 0;
365 45 for ( 1 .. 4 ) {
366 175 my $c = _peek($p);
367 175 ▶ _fail_at( $p, 'Bad Unicode escape' ) unless defined $c && $c =~ /\A[0-9a-fA-F]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c =~ /\A[0-9a-fA-F]\z/ 3 172 175
Condition100%3/3
defined $c and $c =~ /\A[0-9a-fA-F]\z/
!ll&&!rl&&r
12172
MC/DC100%2/2
defined $c and $c =~ /\A[0-9a-fA-F]\z/
defined $c $c =~ /\A[0-9a-fA-F]\z/
Truth table100%3/3
A and B
A= defined $cB= $c =~ /\A[0-9a-fA-F]\z/
ABresult
0X0
100
111
368 172 $v = $v * 16 + hex $c;
369 172 $p->{i}++;
370 }
371 42 return $v;
372 }
373  
374 180866 ▶sub _object {
Subroutine _object: called CC=12 SCAR=24.8
375 180866 my ( $p ) = @_;
376 180866 $p->{i}++;
377 180866 my $obj = js_ordered();
378 180866 _ws($p);
379 180866 my $c = _peek($p);
380 180866 ▶ if ( defined $c && $c eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq "}") 1156 179710 180866
Condition100%3/3
defined $c and $c eq "}"
!ll&&!rl&&r
501796601156
MC/DC100%2/2
defined $c and $c eq "}"
defined $c $c eq "}"
Truth table100%3/3
A and B
A= defined $cB= $c eq "}"
ABresult
0X0
100
111
381 1156 $p->{i}++;
382 1156 return $obj;
383 }
384 179710 ▶ _fail_at( $p, "Expected property name or '}'" ) unless defined $c && $c eq '"';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c eq "\"" 261 179449 179710
Condition100%3/3
defined $c and $c eq "\""
!ll&&!rl&&r
50211179449
MC/DC100%2/2
defined $c and $c eq "\""
defined $c $c eq "\""
Truth table100%3/3
A and B
A= defined $cB= $c eq "\""
ABresult
0X0
100
111
385 179449 while (1) {
386 343405 my $key = _string($p);
387 343405 _ws($p);
388 343405 ▶ _fail_at( $p, "Expected ':' after property name" ) unless ( _peek($p) // '' ) eq ':';
Branch: 1 branch
BranchTrueFalseTotal
unless (_peek($p) // "") eq ":" 7 343398 343405
Condition100%2/2
_peek($p) // ""
l!l
3434023
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
389 343398 $p->{i}++;
390 343398 _ws($p);
391 343398 $obj->{$key} = _value($p);
392 343390 _ws($p);
393 343390 ▶ my $n = _peek($p) // '';
Condition100%2/2
_peek($p) // ""
l!l
3433891
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
394 343390 ▶ if ( $n eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "}") 179425 163965 343390
395 179425 $p->{i}++;
396 179425 return $obj;
397 }
398 163965 ▶ _fail_at( $p, "Expected ',' or '}' after property value" ) unless $n eq ',';
Branch: 1 branch
BranchTrueFalseTotal
unless $n eq "," 6 163959 163965
399 163959 $p->{i}++;
400 163959 _ws($p);
401 163959 ▶ _fail_at( $p, 'Expected double-quoted property name' ) unless ( _peek($p) // '' ) eq '"';
Branch: 1 branch
BranchTrueFalseTotal
unless (_peek($p) // "") eq "\"" 3 163956 163959
Condition100%2/2
_peek($p) // ""
l!l
1639581
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
402 }
403 }
404  
405 21757 ▶sub _array {
Subroutine _array: called CC=6 SCAR=17.9
406 21757 my ( $p ) = @_;
407 21757 $p->{i}++;
408 21757 my @out;
409 21757 _ws($p);
410 21757 my $c = _peek($p);
411 21757 ▶ if ( defined $c && $c eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c eq "]") 1166 20591 21757
Condition100%3/3
defined $c and $c eq "]"
!ll&&!rl&&r
3205881166
MC/DC100%2/2
defined $c and $c eq "]"
defined $c $c eq "]"
Truth table100%3/3
A and B
A= defined $cB= $c eq "]"
ABresult
0X0
100
111
412 1166 $p->{i}++;
413 1166 return \@out;
414 }
415 20591 while (1) {
416 28683 _ws($p);
417 28683 push @out, _value($p);
418 28657 _ws($p);
419 28657 ▶ my $n = _peek($p) // '';
Condition100%2/2
_peek($p) // ""
l!l
286552
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
Aresult
11
00
420 28657 ▶ if ( $n eq ']' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n eq "]") 20558 8099 28657
421 20558 $p->{i}++;
422 20558 return \@out;
423 }
424 8099 ▶ _fail_at( $p, "Expected ',' or ']' after array element" ) unless $n eq ',';
Branch: 1 branch
BranchTrueFalseTotal
unless $n eq "," 7 8092 8099
425 8092 $p->{i}++;
426 }
427 }
428  
429 709 1;
430  
431 __END__
« /usr/local/lib/perl5/5.44.0/CPAN/Meta/Validator.pm ↑ Summary lib/Skilletor/URL.pm »