| 1 | package Skilletor::JSON; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: JSON read and written exactly as the reference implementation does | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 248 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 248 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 248 | ▶ | use B (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 248 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 248 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 248 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 248 | ▶ | use Scalar::Util qw( blessed reftype ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 248 | ▶ | use Skilletor::Error::JSON; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 248 | ▶ | use Skilletor::JSON::OrderedHash; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 248 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 248 | our @EXPORT_OK = qw( js_parse js_stringify js_ordered js_sort_keys js_number js_type ); | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 248 | ▶ | use constant CORE_BOOL => defined &builtin::is_bool; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | #### Writing ########################################################### | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 13710 | ▶ | sub js_ordered { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
js_ordered: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 13710 | my ( @pairs ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 13710 | tie my %h, 'Skilletor::JSON::OrderedHash', @pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 13710 | return \%h; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Default Array.prototype.sort: by UTF-16 code units. | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 3920 | ▶ | sub js_sort_keys { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
js_sort_keys: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 3920 | my ( @keys ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 3920 | ▶ | return sort @keys unless grep { /[^\x{0}-\x{FFFF}]/ } @keys; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my %unit = map { $_ => _utf16_units($_) } @keys; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | return sort { $unit{$a} cmp $unit{$b} } @keys; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | ▶ | sub _utf16_units { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_utf16_units: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | my ( $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | return join '', map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | my $c = ord; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | $c > 0xFFFF | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | ? do { my $v = $c - 0x10000; chr( 0xD800 + ( $v >> 10 ) ).chr( 0xDC00 + ( $v & 0x3FF ) ) } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 3981 | ▶ | sub _hash_keys { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hash_keys: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3981 | my ( $h ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3981 | my $tied = tied %$h; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3981 | ▶ | return $tied->keys_in_order if blessed $tied && $tied->isa('Skilletor::JSON::OrderedHash'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($tied) and $tied->isa("Skilletor::JSON::OrderedHash")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3085 | my @keys = keys %$h; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3085 | my @index = sort { $a <=> $b } grep { Skilletor::JSON::OrderedHash->is_index_key($_) } @keys; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3085 | my @rest = js_sort_keys( grep { !Skilletor::JSON::OrderedHash->is_index_key($_) } @keys ); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3085 | return ( @index, @rest ); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 6666 | ▶ | sub js_stringify { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
js_stringify: called CC=6 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 6666 | my ( $value, $indent ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | # A number: that many spaces (at most 10); a string: its first 10 characters. | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 6666 | ▶ | my $gap = !defined $indent ? '' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition33%1/3
ref $indent || _is_number($indent)
MC/DC0%0/2
ref $indent || _is_number($indent)
ref $indent
_is_number($indent)
Truth table33%1/3
A || B
A= ref $indentB= _is_number($indent)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | : ( !ref $indent && !_is_number($indent) ) ? substr( $indent, 0, 10 ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | : $indent > 0 ? ' ' x ( $indent > 10 ? 10 : $indent ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | : ''; | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 6666 | return _write( $value, $gap, '' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 20139 | ▶ | sub _is_number { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_number: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 20139 | my ( $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 20139 | my $flags = B::svref_2object( \$value )->FLAGS; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 20139 | ▶ | return ( $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() ) ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$flags & 12288 and not $flags & 16384
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # typeof, with Array.isArray and null told apart: what a validator asks of a parsed value. | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 14819 | ▶ | sub js_type { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
js_type: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 14819 | my ( $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 14819 | ▶ | return 'null' unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 14488 | ▶ | return 'boolean' if JSON::MaybeXS::is_bool($value); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 14422 | ▶ | if ( ref $value ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 8521 | my $type = reftype $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 8521 | ▶ | return $type eq 'ARRAY' ? 'array' : $type eq 'HASH' ? 'object' : 'other'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 5901 | if (CORE_BOOL) { | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 248 | ▶ | no warnings 'experimental::builtin'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 5901 | ▶ | return 'boolean' if builtin::is_bool($value); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 5901 | ▶ | return _is_number($value) ? 'number' : 'string'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 21757 | ▶ | sub _write { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write: called CC=25 SCAR=40.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 21757 | my ( $value, $gap, $current ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 21757 | ▶ | return 'null' unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 21317 | ▶ | if ( ref $value ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 7400 | ▶ | return $value ? 'true' : 'false' if JSON::MaybeXS::is_bool($value); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ( blessed $value && !( tied( %{ $value } ) && reftype $value eq 'HASH' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
builtin::blessed($value) and not tied %{$value;} && builtin::reftype($value) eq "HASH"
tied %{$value;} && builtin::reftype($value) eq 'HASH'
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'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return _write( $value->TO_JSON, $gap, $current ) if $value->can('TO_JSON'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | croak __PACKAGE__.'->js_stringify cannot write a '.ref($value).' object'; | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 7320 | my $type = reftype $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 7320 | my $inner = $current.$gap; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 7320 | ▶ | if ( $type eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 3339 | ▶ | return '[]' unless @$value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 2975 | my @parts = map { _write( $_, $gap, $inner ) } @$value; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2975 | ▶ | return '['.join( ',', @parts ).']' if $gap eq ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 259 | return "[\n".$inner.join( ",\n".$inner, @parts )."\n".$current.']'; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3981 | ▶ | if ( $type eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3981 | my @keys = _hash_keys($value); | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3981 | ▶ | return '{}' unless @keys; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3720 | ▶ | my $colon = $gap eq '' ? ':' : ': '; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3720 | my @parts = map { _quote($_).$colon._write( $value->{$_}, $gap, $inner ) } @keys; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3720 | ▶ | return '{'.join( ',', @parts ).'}' if $gap eq ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 956 | return "{\n".$inner.join( ",\n".$inner, @parts )."\n".$current.'}'; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( $type eq 'SCALAR' && !blessed $value ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type eq "SCALAR" and not builtin::blessed($value)
MC/DC0%0/2
$type eq "SCALAR" and not builtin::blessed($value)
$type eq "SCALAR"
not builtin::blessed($value)
Truth table0%0/3
A and not(B)
A= $type eq "SCALAR"B= not builtin::blessed($value)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return $$value ? 'true' : 'false' if !defined $$value || $$value eq '1' || $$value eq '0'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
not defined $$value or $$value eq 1 or $$value eq 0
not defined $$value or $$value eq 1
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | croak __PACKAGE__.'->js_stringify cannot write a '.$type.' reference'; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 13917 | if ( CORE_BOOL ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 248 | ▶ | no warnings 'experimental::builtin'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 13917 | ▶ | return $value ? 'true' : 'false' if builtin::is_bool($value); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 13917 | ▶ | return js_number($value) if _is_number($value); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 12391 | return _quote($value); | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 248 | 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 | 21472 | ▶ | sub _quote { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_quote: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 21472 | my ( $s ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 21472 | $s = "$s"; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 21472 | ▶ | $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)
MC/DC100%1/1
$ESCAPE{$1} // sprintf("\\u%04x", ord $1)
$ESCAPE{$1}
Truth table100%2/2
A
A= $ESCAPE{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 21472 | return '"'.$s.'"'; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # Number::toString for a finite double; JSON.stringify writes NaN and the infinities as null. | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1526 | ▶ | sub js_number { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
js_number: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 1526 | my ( $n ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1526 | $n = 0 + $n; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1526 | ▶ | return 'null' if $n != $n || $n == 9**9**9 || $n == -9**9**9; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$n != $n or $n == "Inf" or $n == "-Inf"
$n != $n or $n == "Inf"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1518 | ▶ | return '0' if $n == 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1509 | my $flags = B::svref_2object( \$n )->FLAGS; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1509 | ▶ | if ( ( $flags & B::SVf_IOK() ) && !( $flags & B::SVf_NOK() ) && abs($n) <= 9007199254740992 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$flags & 256 and not $flags & 512 and abs $n <= '9007199254740992'
$flags & 256 and not $flags & 512
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'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return "$n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 1509 | $n = unpack 'd', pack 'd', $n; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1509 | ▶ | my $neg = $n < 0 ? '-' : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 1509 | my $abs = abs $n; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | 1509 | my ( $digits, $exp ); | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1509 | for my $p ( 1 .. 17 ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 6855 | my $s = sprintf '%.*e', $p - 1, $abs; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | 6855 | ▶ | next unless ( 0 + $s ) == $abs || $p == 17; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
0 + $s == $abs or $p == 17
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1509 | ( $digits, $exp ) = $s =~ /\A([0-9.]+)e([-+][0-9]+)\z/; | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1509 | $digits =~ s/\.//; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1509 | ▶ | $digits =~ s/0+\z// if length $digits > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1509 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1509 | my $k = length $digits; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1509 | my $e = $exp + 1; # the decimal point sits after $e digits | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1509 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 1509 | ▶ | if ( $k <= $e && $e <= 21 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$k <= $e and $e <= 21
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1438 | $out = $digits.( '0' x ( $e - $k ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | ▶ | elsif ( 0 < $e && $e <= 21 ) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
0 < $e and $e <= 21
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 36 | $out = substr( $digits, 0, $e ).'.'.substr( $digits, $e ); | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | ▶ | elsif ( -6 < $e && $e <= 0 ) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
-6 < $e and $e <= 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 19 | $out = '0.'.( '0' x -$e ).$digits; | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 16 | my $x = $e - 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 16 | ▶ | my $mant = $k == 1 ? $digits : substr( $digits, 0, 1 ).'.'.substr( $digits, 1 ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 16 | ▶ | $out = $mant.'e'.( $x < 0 ? '-' : '+' ).abs($x); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 1509 | 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 | |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 1628 | ▶ | sub js_parse { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
js_parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 1628 | my ( $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 1628 | ▶ | croak __PACKAGE__.'->js_parse needs a string' unless defined $text; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 1628 | my $p = { s => "$text", i => 0, len => length "$text" }; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1628 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 1628 | my $value = _value($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 1502 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 1502 | ▶ | _fail_at( $p, 'Unexpected non-whitespace character after JSON', undef, 1 ) if $p->{i} < $p->{len}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 1497 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 86347 | ▶ | sub _ws { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ws: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 86347 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 86347 | pos( $p->{s} ) = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | 86347 | ▶ | $p->{i} = pos( $p->{s} ) if $p->{s} =~ /\G[ \t\n\r]+/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 140123 | ▶ | sub _peek { $_[0]{i} < $_[0]{len} ? substr( $_[0]{s}, $_[0]{i}, 1 ) : undef } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_peek: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 10 | ▶ | sub _eos { Skilletor::Error::JSON->throw('Unexpected end of JSON input') } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eos: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # The position as JavaScript counts it, in UTF-16 code units. | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 103 | ▶ | sub _units { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_units: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 103 | my ( $p, $i ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 103 | my $before = substr( $p->{s}, 0, $i ); | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 103 | my $astral = () = $before =~ /[^\x{0}-\x{FFFF}]/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | 103 | return $i + $astral; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 100 | ▶ | sub _fail_at { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fail_at: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 100 | my ( $p, $what, $i, $bare ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 100 | ▶ | $i //= $p->{i}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$i //= $p->{"i"}
MC/DC0%0/2
$i //= $p->{"i"}
$i
$p->{"i"}
Truth table33%1/3
A //= B
A= $iB= $p->{"i"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 100 | my $before = substr( $p->{s}, 0, $i ); | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | 100 | my $line = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | 100 | my $last = -1; | |||||||||||||||||||||||||||||||||||||||||||||
| 222 | 100 | ▶ | while ( $before =~ /\r\n|\r|\n/g ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 6 | $line++; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 6 | $last = pos($before) - 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 100 | my $pos = _units( $p, $i ); | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | 100 | ▶ | my $column = $last < 0 ? $pos + 1 : $pos - _units( $p, $last ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 100 | ▶ | Skilletor::Error::JSON->throw( $what.( $bare ? '' : ' in JSON' ).' at position '.$pos.' (line '.$line.' column '.$column.')' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 21 | ▶ | sub _unexpected_token { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unexpected_token: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 21 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | 21 | my $i = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | 21 | my $tok = substr( $p->{s}, $i, 1 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | 21 | my $len = $p->{len}; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 21 | my $context; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 21 | ▶ | if ( $len <= 20 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 16 | $context = '"'.$p->{s}.'"'; | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 5 | ▶ | my $start = $i - 10 < 0 ? 0 : $i - 10; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 5 | ▶ | my $end = $i + 10 > $len ? $len : $i + 10; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 5 | ▶ | $context = ( $start > 0 ? '...' : '' ).'"'.substr( $p->{s}, $start, $end - $start ).'"'.( $end < $len ? '...' : '' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 21 | Skilletor::Error::JSON->throw( "Unexpected token '".$tok."', ".$context.' is not valid JSON' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 23800 | ▶ | sub _value { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_value: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 23800 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | 23800 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 251 | 23800 | ▶ | _eos() unless defined $c; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 23794 | ▶ | return _string($p) if $c eq '"'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 12612 | ▶ | return _object($p) if $c eq '{'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 5164 | ▶ | return _array($p) if $c eq '['; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 2457 | ▶ | return _number($p) if $c eq '-' || $c =~ /[0-9]/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c eq "-" or $c =~ /[0-9]/
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]/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 1013 | ▶ | return _literal( $p, 'true', JSON::MaybeXS::true() ) if $c eq 't'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 746 | ▶ | return _literal( $p, 'false', JSON::MaybeXS::false() ) if $c eq 'f'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 562 | ▶ | return _literal( $p, 'null', undef ) if $c eq 'n'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 15 | _unexpected_token($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 998 | ▶ | sub _literal { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_literal: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 998 | my ( $p, $word, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 998 | for my $ch ( split //, $word ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 4174 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | 4174 | ▶ | _eos() unless defined $c; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 4172 | ▶ | _unexpected_token($p) unless $c eq $ch; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 4166 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 990 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 1444 | ▶ | sub _number { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_number: called CC=17 SCAR=28.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 1444 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 1444 | my $start = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1444 | my $s = $p->{s}; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 1444 | ▶ | if ( _peek($p) eq '-' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 15 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 15 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | 15 | ▶ | _fail_at( $p, 'No number after minus sign' ) unless defined $c && $c =~ /[0-9]/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $c and $c =~ /[0-9]/
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]/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 1441 | my $int_start = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | 1441 | ▶ | if ( _peek($p) eq '0' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 32 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 32 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 286 | 32 | ▶ | _fail_at( $p, 'Unexpected number' ) if defined $c && $c =~ /[0-9]/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $c and $c =~ /[0-9]/
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]/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 1409 | pos($s) = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 1409 | $s =~ /\G[0-9]+/gc; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | 1409 | $p->{i} = pos($s); | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1437 | my $simple = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 294 | 1437 | ▶ | if ( ( _peek($p) // '' ) eq '.' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 39 | $simple = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | 39 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | 39 | pos($s) = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | 39 | ▶ | _fail_at( $p, 'Unterminated fractional number' ) unless $s =~ /\G[0-9]+/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 37 | $p->{i} = pos($s); | |||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 1435 | ▶ | if ( ( _peek($p) // '' ) =~ /\A[eE]\z/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 17 | $simple = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 303 | 17 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | 17 | ▶ | $p->{i}++ if ( _peek($p) // '' ) =~ /\A[-+]\z/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 17 | pos($s) = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 17 | ▶ | _fail_at( $p, 'Exponent part is missing a number' ) unless $s =~ /\G[0-9]+/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 15 | $p->{i} = pos($s); | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 1433 | my $text = substr( $s, $start, $p->{i} - $start ); | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | 1433 | my $n = 0 + $text; | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | # A JavaScript number is a double: an integer beyond 2**53 loses its low digits there. | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 1433 | ▶ | $n = unpack 'd', pack 'd', $n unless $simple && $p->{i} - $int_start <= 15; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$simple and $p->{"i"} - $int_start <= 15
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 1433 | return $n; | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 248 | my %UNESCAPE = ( '"' => '"', '\\' => '\\', '/' => '/', b => "\b", f => "\f", n => "\n", r => "\r", t => "\t" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 29126 | ▶ | sub _string { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_string: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 29126 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | 29126 | $p->{i}++; # the opening quote | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | 29126 | my $s = $p->{s}; | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | 29126 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | 29126 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||
| 324 | 37150 | pos($s) = $p->{i}; | |||||||||||||||||||||||||||||||||||||||||||||
| 325 | 37150 | ▶ | if ( $s =~ /\G([^"\\\x{0}-\x{1F}]+)/gc ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 35736 | $out .= $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | 35736 | $p->{i} = pos($s); | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 37150 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 330 | 37150 | ▶ | _fail_at( $p, 'Unterminated string' ) unless defined $c; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 37149 | ▶ | if ( $c eq '"' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 29117 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 333 | 29117 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 8032 | ▶ | _fail_at( $p, 'Bad control character in string literal' ) if ord($c) < 0x20; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # a backslash | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 8030 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | 8030 | my $e = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | 8030 | ▶ | _eos() unless defined $e; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 8028 | ▶ | if ( exists $UNESCAPE{$e} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 8002 | $out .= $UNESCAPE{$e}; | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | 8002 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 343 | 8002 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 26 | ▶ | _fail_at( $p, 'Bad escaped character' ) unless $e eq 'u'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 25 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 347 | 25 | my $code = _hex4($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 348 | 22 | ▶ | if ( $code >= 0xD800 && $code <= 0xDBFF && substr( $s, $p->{i}, 2 ) eq '\\u' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$code >= 55296 and $code <= 56319 and substr($s, $p->{"i"}, 2) eq "\\u"
$code >= 55296 and $code <= 56319
MC/DC100%3/3
$code >= 55296 and $code <= 56319 and substr($s, $p->{"i"}, 2) eq "\\u"
$code >= 55296
$code <= 56319
substr($s, $p->{"i"}, 2) eq "\\u"
Truth table100%4/4
A and B and C
A= $code >= 55296B= $code <= 56319C= substr($s, $p->{"i"}, 2) eq "\\u"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$low >= 56320 and $low <= 57343
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 2 | $out .= chr( 0x10000 + ( ( $code - 0xD800 ) << 10 ) + ( $low - 0xDC00 ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | $p->{i} = $save; | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 248 | ▶ | { no warnings qw( surrogate utf8 ); $out .= chr $code; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 27 | ▶ | sub _hex4 { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hex4: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 27 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 364 | 27 | my $v = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 365 | 27 | for ( 1 .. 4 ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | 103 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 367 | 103 | ▶ | _fail_at( $p, 'Bad Unicode escape' ) unless defined $c && $c =~ /\A[0-9a-fA-F]\z/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $c and $c =~ /\A[0-9a-fA-F]\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 100 | $v = $v * 16 + hex $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 369 | 100 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 24 | return $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 7448 | ▶ | sub _object { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_object: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 7448 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 376 | 7448 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 377 | 7448 | my $obj = js_ordered(); | |||||||||||||||||||||||||||||||||||||||||||||
| 378 | 7448 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | 7448 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | 7448 | ▶ | if ( defined $c && $c eq '}' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $c and $c eq "}"
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 "}"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 255 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | 255 | return $obj; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 7193 | ▶ | _fail_at( $p, "Expected property name or '}'" ) unless defined $c && $c eq '"'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $c and $c eq "\""
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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 7139 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||
| 386 | 17944 | my $key = _string($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 387 | 17944 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 388 | 17944 | ▶ | _fail_at( $p, "Expected ':' after property name" ) unless ( _peek($p) // '' ) eq ':'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 17937 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 390 | 17937 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 391 | 17937 | $obj->{$key} = _value($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 392 | 17929 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 393 | 17929 | ▶ | my $n = _peek($p) // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 17929 | ▶ | if ( $n eq '}' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 7115 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 396 | 7115 | return $obj; | |||||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 10814 | ▶ | _fail_at( $p, "Expected ',' or '}' after property value" ) unless $n eq ','; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 10808 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | 10808 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | 10808 | ▶ | _fail_at( $p, 'Expected double-quoted property name' ) unless ( _peek($p) // '' ) eq '"'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 2707 | ▶ | sub _array { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_array: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 2707 | my ( $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | 2707 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 408 | 2707 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||
| 409 | 2707 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 410 | 2707 | my $c = _peek($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 411 | 2707 | ▶ | if ( defined $c && $c eq ']' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $c and $c eq "]"
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 "]"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 1011 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 413 | 1011 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 1696 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||
| 416 | 4235 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 417 | 4235 | push @out, _value($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | 4209 | _ws($p); | |||||||||||||||||||||||||||||||||||||||||||||
| 419 | 4209 | ▶ | my $n = _peek($p) // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_peek($p) // ""
MC/DC100%1/1
_peek($p) // ""
_peek($p)
Truth table100%2/2
A
A= _peek($p)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 4209 | ▶ | if ( $n eq ']' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 1663 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | 1663 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 2546 | ▶ | _fail_at( $p, "Expected ',' or ']' after array element" ) unless $n eq ','; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 2539 | $p->{i}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 248 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||