| 1 | package JKML; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Types::Serialiser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use B (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our @EXPORT = qw(decode_jkml encode_jkml); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = "0.01"; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | our @HERE_QUEUE; | |||||||||||||||||||||||||||
| 17 | 3 | our $SELF; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # JKML is based on JSON::Tiny. | ||||||||||||||||||||||||||||
| 20 | # JSON::Tiny was "Adapted from Mojo::JSON and Mojo::Util". | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Licensed under the Artistic 2.0 license. | ||||||||||||||||||||||||||||
| 23 | # http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 25 | my $class = shift; | |||||||||||||||||||||||||||
| 27 | 25 | bless { | |||||||||||||||||||||||||||
| 28 | functions => { | ||||||||||||||||||||||||||||
| 29 | base64 => \&MIME::Base64::decode_base64, | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | }, $class; | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 1 | my ($self, $name, $vref) = @_; | |||||||||||||||||||||||||||
| 36 | 1 | my $code = $self->{functions}->{$name}; | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | unless ($code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | _exception("Unknown function: $name"); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | $code->($vref); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | my $TRUE = $Types::Serialiser::true; | |||||||||||||||||||||||||||
| 44 | 3 | my $FALSE = $Types::Serialiser::false; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Escaped special character map (with u2028 and u2029) | ||||||||||||||||||||||||||||
| 47 | 3 | my %ESCAPE = ( | |||||||||||||||||||||||||||
| 48 | '"' => '"', | ||||||||||||||||||||||||||||
| 49 | '\\' => '\\', | ||||||||||||||||||||||||||||
| 50 | '/' => '/', | ||||||||||||||||||||||||||||
| 51 | 'b' => "\x08", | ||||||||||||||||||||||||||||
| 52 | 'f' => "\x0c", | ||||||||||||||||||||||||||||
| 53 | 'n' => "\x0a", | ||||||||||||||||||||||||||||
| 54 | 'r' => "\x0d", | ||||||||||||||||||||||||||||
| 55 | 't' => "\x09", | ||||||||||||||||||||||||||||
| 56 | 'u2028' => "\x{2028}", | ||||||||||||||||||||||||||||
| 57 | 'u2029' => "\x{2029}" | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | 3 | my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | for( 0x00 .. 0x1f, 0x7f ) { | |||||||||||||||||||||||||||
| 62 | 99 | my $packed = pack 'C', $_; | |||||||||||||||||||||||||||
| 63 | $REVERSE{$packed} = sprintf '\u%.4X', $_ | ||||||||||||||||||||||||||||
| 64 | 99 | ▶ | if ! defined( $REVERSE{$packed} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | my $WHITESPACE_RE = qr/[\x20\x09]/; | |||||||||||||||||||||||||||
| 68 | 3 | my $COMMENT_RE = qr!#[^\n]*(?:\n|\z)!; | |||||||||||||||||||||||||||
| 69 | 3 | my $IGNORABLE_RE = qr!(?:$WHITESPACE_RE|$COMMENT_RE)*!; | |||||||||||||||||||||||||||
| 70 | 3 | my $LEFTOVER_RE = qr!(?:$WHITESPACE_RE|$COMMENT_RE|[\x0d\x0a])*!; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 17 | ▶ | sub decode_jkml { JKML->new->decode(shift) } | ||||||||||||||||||||||||||
|
Subroutine
decode_jkml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 8 | ▶ | sub encode_jkml { JKML->new->encode(shift) } | ||||||||||||||||||||||||||
|
Subroutine
encode_jkml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 17 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=7 SCAR=22.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 17 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 17 | local $SELF = $self; | |||||||||||||||||||||||||||
| 80 | 17 | local @HERE_QUEUE; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Missing input | ||||||||||||||||||||||||||||
| 83 | 17 | ▶ | die 'Missing or empty input' unless $bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Wide characters | ||||||||||||||||||||||||||||
| 86 | 17 | ▶ | die 'Wide character in input' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | unless utf8::downgrade($bytes, 1); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Object or array | ||||||||||||||||||||||||||||
| 90 | 17 | my $res = eval { | |||||||||||||||||||||||||||
| 91 | 17 | local $_ = $bytes; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Leading whitespace | ||||||||||||||||||||||||||||
| 94 | 17 | _skip_space(); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # value | ||||||||||||||||||||||||||||
| 97 | 17 | my $ref; | |||||||||||||||||||||||||||
| 98 | 17 | _decode_value(\$ref); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Leftover data | ||||||||||||||||||||||||||||
| 101 | 17 | _skip_space(); | |||||||||||||||||||||||||||
| 102 | 17 | ▶ | unless (pos() == length($_)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | my $got = ref $ref ? lc(ref($ref)) : 'scalar'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | _exception("Unexpected data after $got"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 17 | $ref; | |||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Exception | ||||||||||||||||||||||||||||
| 111 | 17 | ▶ | if (!$res && (my $e = $@)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $res and my $e = $@
MC/DC0%0/2
not $res and my $e = $@
not $res
my $e = $@
Truth table33%1/3
not(A) and B
A= not $resB= my $e = $@
| |||||||||||||||||||||||||||||
| 112 | 0 | chomp $e; | |||||||||||||||||||||||||||
| 113 | 0 | die $e; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 17 | return $res; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub false {$FALSE} | ||||||||||||||||||||||||||
|
Subroutine
false: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub true {$TRUE} | ||||||||||||||||||||||||||
|
Subroutine
true: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 5 | ▶ | sub _decode_array { | ||||||||||||||||||||||||||
|
Subroutine
_decode_array: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 124 | 5 | my @array; | |||||||||||||||||||||||||||
| 125 | 5 | _skip_space(); | |||||||||||||||||||||||||||
| 126 | 5 | ▶ | until (m/\G\]/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Value | ||||||||||||||||||||||||||||
| 129 | 8 | _decode_value(\($array[0+@array])); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Separator | ||||||||||||||||||||||||||||
| 132 | 8 | my $found_separator = 0; | |||||||||||||||||||||||||||
| 133 | 8 | _skip_space(); | |||||||||||||||||||||||||||
| 134 | 8 | ▶ | if (m/\G,/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 4 | $found_separator++; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # End | ||||||||||||||||||||||||||||
| 139 | 8 | _skip_space(); | |||||||||||||||||||||||||||
| 140 | 8 | ▶ | last if m/\G\]/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 3 | _skip_space(); | |||||||||||||||||||||||||||
| 143 | 3 | ▶ | redo if $found_separator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Invalid character | ||||||||||||||||||||||||||||
| 146 | 0 | _exception('Expected comma or right square bracket while parsing array'); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 5 | return \@array; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | ▶ | sub _decode_object { | ||||||||||||||||||||||||||
|
Subroutine
_decode_object: called CC=8 SCAR=23.6
| |||||||||||||||||||||||||||||
| 153 | 5 | my %hash; | |||||||||||||||||||||||||||
| 154 | 5 | _skip_space(); | |||||||||||||||||||||||||||
| 155 | 5 | ▶ | until (m/\G\}/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | # Key | ||||||||||||||||||||||||||||
| 157 | 4 | my $key = do { | |||||||||||||||||||||||||||
| 158 | 4 | _skip_space(); | |||||||||||||||||||||||||||
| 159 | 4 | ▶ | if (m/\G([A-Za-z][a-zA-Z0-9_]*)/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 4 | $1; | |||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||
| 162 | # Quote | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | m/\G"/gc | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | or _exception('Expected string while parsing object'); | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | _decode_string(); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # Colon | ||||||||||||||||||||||||||||
| 171 | 4 | _skip_space(); | |||||||||||||||||||||||||||
| 172 | 4 | ▶ | m/\G=>/gc | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | or _exception('Expected "=>" while parsing object'); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # Value | ||||||||||||||||||||||||||||
| 176 | 4 | _decode_value(\$hash{$key}); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Separator | ||||||||||||||||||||||||||||
| 179 | 4 | _skip_space(); | |||||||||||||||||||||||||||
| 180 | 4 | my $found_separator = 0; | |||||||||||||||||||||||||||
| 181 | 4 | ▶ | $found_separator++ if m/\G,/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # End | ||||||||||||||||||||||||||||
| 184 | 4 | _skip_space(); | |||||||||||||||||||||||||||
| 185 | 4 | ▶ | last if m/\G\}/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | _skip_space(); | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | redo if $found_separator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Invalid character | ||||||||||||||||||||||||||||
| 191 | 0 | _exception('Expected comma or right curly bracket while parsing object'); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 5 | return \%hash; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 3 | ▶ | sub _decode_string { | ||||||||||||||||||||||||||
|
Subroutine
_decode_string: called CC=9 SCAR=37.2
| |||||||||||||||||||||||||||||
| 198 | 3 | my $quote = shift; | |||||||||||||||||||||||||||
| 199 | 3 | my $pos = pos; | |||||||||||||||||||||||||||
| 200 | # Extract string with escaped characters | ||||||||||||||||||||||||||||
| 201 | 3 | m!\G((?:(?:[^\x00-\x1f\\${quote}]|\\(?:[${quote}\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc; # segfault on 5.8.x in t/20-mojo-json.t #83 | |||||||||||||||||||||||||||
| 202 | 3 | my $str = $1; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # Invalid character | ||||||||||||||||||||||||||||
| 205 | 3 | ▶ | unless (m/\G${quote}/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | _exception('Unexpected character or invalid escape while parsing string') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | if m/\G[\x00-\x1f\\]/; | ||||||||||||||||||||||||||||
| 208 | 0 | _exception('Unterminated string'); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # Unescape popular characters | ||||||||||||||||||||||||||||
| 212 | 3 | ▶ | if (index($str, '\\u') < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 3 | $str =~ s!\\(["\\/bfnrt])!$ESCAPE{$1}!gs; | |||||||||||||||||||||||||||
| 214 | 3 | return $str; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Unescape everything else | ||||||||||||||||||||||||||||
| 218 | 0 | my $buffer = ''; | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | while ($str =~ m/\G([^\\]*)\\(?:([^u])|u(.{4}))/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | $buffer .= $1; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # Popular character | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if ($2) { $buffer .= $ESCAPE{$2} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # Escaped | ||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||
| 227 | 0 | my $ord = hex $3; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Surrogate pair | ||||||||||||||||||||||||||||
| 230 | 0 | ▶ | if (($ord & 0xf800) == 0xd800) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # High surrogate | ||||||||||||||||||||||||||||
| 233 | 0 | ▶ | ($ord & 0xfc00) == 0xd800 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | or pos($_) = $pos + pos($str), _exception('Missing high-surrogate'); | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # Low surrogate | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | $str =~ m/\G\\u([Dd][C-Fc-f]..)/gc | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | or pos($_) = $pos + pos($str), _exception('Missing low-surrogate'); | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Pair | ||||||||||||||||||||||||||||
| 241 | 0 | $ord = 0x10000 + ($ord - 0xd800) * 0x400 + (hex($1) - 0xdc00); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # Character | ||||||||||||||||||||||||||||
| 245 | 0 | $buffer .= pack 'U', $ord; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # The rest | ||||||||||||||||||||||||||||
| 250 | 0 | return $buffer . substr $str, pos($str), length($str); | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 109 | ▶ | sub _skip_space { | ||||||||||||||||||||||||||
|
Subroutine
_skip_space: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 254 | 109 | ▶ | while (m/\G$IGNORABLE_RE/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 121 | ▶ | if (m/\G\x0d?\x0a/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 33 | ▶ | if (@HERE_QUEUE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 6 | my ($v, $terminator) = @{pop @HERE_QUEUE}; | |||||||||||||||||||||||||||
| 258 | 6 | ▶ | if (m/\G(.*?)^([ \t]*)$terminator$/gcsm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 6 | my $buf = $1; | |||||||||||||||||||||||||||
| 260 | 6 | my $dedent = $2; | |||||||||||||||||||||||||||
| 261 | 6 | $buf =~ s/^$dedent//mg; | |||||||||||||||||||||||||||
| 262 | 6 | $buf =~ s/\n\z//; | |||||||||||||||||||||||||||
| 263 | 6 | $$v = $buf; | |||||||||||||||||||||||||||
| 264 | } else { | ||||||||||||||||||||||||||||
| 265 | 0 | _exception("Unexpected EOF in heredoc: '$terminator'"); | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 30 | ▶ | sub _decode_value { | ||||||||||||||||||||||||||
|
Subroutine
_decode_value: called CC=12 SCAR=25.0
| |||||||||||||||||||||||||||||
| 273 | 30 | my $r = shift; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # Leading whitespace | ||||||||||||||||||||||||||||
| 276 | 30 | _skip_space(); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | # funcall | ||||||||||||||||||||||||||||
| 279 | 30 | ▶ | if (m/\G([a-zA-Z][a-zA-Z0-9_]*)\(/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 1 | my $func = $1; | |||||||||||||||||||||||||||
| 281 | 1 | _decode_value(\my $v); | |||||||||||||||||||||||||||
| 282 | 1 | ▶ | m/\G\)/gc or _exception("Missing ) after funcall"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 1 | return $$r = $SELF->call($func, $v); | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # heredoc | ||||||||||||||||||||||||||||
| 287 | 29 | ▶ | if (m/\G<<-([A-Za-z.]+)/gc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 6 | push @HERE_QUEUE, [$r, $1]; | |||||||||||||||||||||||||||
| 289 | 6 | return; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | # Raw string | ||||||||||||||||||||||||||||
| 293 | 23 | ▶ | return $$r = $2 if m/\Gr('''|""""|'|")(.*?)\1/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # String | ||||||||||||||||||||||||||||
| 296 | 17 | ▶ | return $$r = _decode_string($1) if m/\G(["'])/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # Array | ||||||||||||||||||||||||||||
| 299 | 14 | ▶ | return $$r = _decode_array() if m/\G\[/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # Object | ||||||||||||||||||||||||||||
| 302 | 9 | ▶ | return $$r = _decode_object() if m/\G\{/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # Number | ||||||||||||||||||||||||||||
| 305 | 4 | ▶ | return $$r = 0 + $1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | if m/\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc; | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # True | ||||||||||||||||||||||||||||
| 309 | 3 | ▶ | return $$r = $TRUE if m/\Gtrue/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # False | ||||||||||||||||||||||||||||
| 312 | 1 | ▶ | return $$r = $FALSE if m/\Gfalse/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | # Null | ||||||||||||||||||||||||||||
| 315 | 1 | ▶ | return $$r = undef if m/\Gnull/gc; ## no critic (return) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | # Invalid character | ||||||||||||||||||||||||||||
| 318 | 0 | _exception('Expected string, array, object, number, boolean or null'); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | ▶ | sub _exception { | ||||||||||||||||||||||||||
|
Subroutine
_exception: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # Leading whitespace | ||||||||||||||||||||||||||||
| 324 | 0 | _skip_space(); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # Context | ||||||||||||||||||||||||||||
| 327 | 0 | my $context = 'Malformed JKML: ' . shift; | |||||||||||||||||||||||||||
| 328 | 0 | ▶ | if (m/\G\z/gc) { $context .= ' before end of data' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | else { | ||||||||||||||||||||||||||||
| 330 | 0 | my @lines = split /\n/, substr($_, 0, pos); | |||||||||||||||||||||||||||
| 331 | 0 | ▶ | $context .= ' at line ' . @lines . ', offset ' . length(pop @lines || ''); | ||||||||||||||||||||||||||
|
Condition0%0/2
pop @lines || ''
MC/DC0%0/1
pop @lines || ''
pop @lines
Truth table0%0/2
A
A= pop @lines
| |||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 0 | die "$context\n"; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # ------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 8 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 340 | 8 | my ($self, $ref) = @_; | |||||||||||||||||||||||||||
| 341 | 8 | return Encode::encode 'UTF-8', _encode_value($ref); | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 1 | ▶ | sub _encode_array { | ||||||||||||||||||||||||||
|
Subroutine
_encode_array: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 345 | 1 | my $array = shift; | |||||||||||||||||||||||||||
| 346 | 0 | return '[' . join(',', map { _encode_value($_) } @$array) . ']'; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 2 | ▶ | sub _encode_object { | ||||||||||||||||||||||||||
|
Subroutine
_encode_object: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 350 | 2 | my $object = shift; | |||||||||||||||||||||||||||
| 351 | 2 | my @pairs = map { _encode_string($_) . '=>' . _encode_value($object->{$_}) } | |||||||||||||||||||||||||||
| 352 | keys %$object; | ||||||||||||||||||||||||||||
| 353 | 2 | return '{' . join(',', @pairs) . '}'; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 3 | ▶ | sub _encode_string { | ||||||||||||||||||||||||||
|
Subroutine
_encode_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 357 | 3 | my $str = shift; | |||||||||||||||||||||||||||
| 358 | 3 | $str =~ s!([\x00-\x1f\x7f\x{2028}\x{2029}\\"/\b\f\n\r\t])!$REVERSE{$1}!gs; | |||||||||||||||||||||||||||
| 359 | 3 | return "\"$str\""; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 9 | ▶ | sub _encode_value { | ||||||||||||||||||||||||||
|
Subroutine
_encode_value: called CC=11 SCAR=24.7
| |||||||||||||||||||||||||||||
| 363 | 9 | my $value = shift; | |||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | # Reference | ||||||||||||||||||||||||||||
| 366 | 9 | ▶ | if (my $ref = ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # Array | ||||||||||||||||||||||||||||
| 369 | 5 | ▶ | return _encode_array($value) if $ref eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | # Object | ||||||||||||||||||||||||||||
| 372 | 4 | ▶ | return _encode_object($value) if $ref eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # True or false | ||||||||||||||||||||||||||||
| 375 | 2 | ▶ | return Types::Serialiser::is_true($value) ? 'true' : 'false' if Types::Serialiser::is_bool($ref); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # References to scalars (including blessed) will be encoded as Booleans. | ||||||||||||||||||||||||||||
| 378 | 2 | ▶ | return $$value ? 'true' : 'false' if $ref =~ /SCALAR/; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # Null | ||||||||||||||||||||||||||||
| 383 | 4 | ▶ | return 'null' unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | # Number | ||||||||||||||||||||||||||||
| 386 | 4 | my $flags = B::svref_2object(\$value)->FLAGS; | |||||||||||||||||||||||||||
| 387 | 4 | ▶ | return 0 + $value if $flags & (B::SVp_IOK | B::SVp_NOK) && $value * 0 == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$flags & 12288 and $value * 0 == 0
MC/DC50%1/2
$flags & 12288 and $value * 0 == 0
$flags & 12288
$value * 0 == 0
Truth table66%2/3
A and B
A= $flags & 12288B= $value * 0 == 0
| |||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # String | ||||||||||||||||||||||||||||
| 390 | 2 | return _encode_string($value); | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 3 | 1; | |||||||||||||||||||||||||||
| 394 | __END__ | ||||||||||||||||||||||||||||