blib/lib/JKML.pm

filter: Statementstmt 80.5% 153 / 190 Branchbran 59.8% 61 / 102 Conditioncond 37.5% 3 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 88.8% 24 / 27 Podpod 12.5% 1 / 8 totaltotal 71.4% 243 / 340 CC 54 SCAR 24.3
CC
54
Cov
72%
_decode_string
41.4
_decode_value
12.1
_exception
12.0
CRAP 11.4
↑ Summary
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
BranchTrueFalseTotal
unless ($code) 0 1 1
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
BranchTrueFalseTotal
unless defined $REVERSE{$packed} 84 15 99
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
BranchTrueFalseTotal
unless $bytes 0 17 17
84  
85 # Wide characters
86 17 ▶ die 'Wide character in input'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 17 17
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
BranchTrueFalseTotal
unless (pos $_ == length $_) 0 17 17
103 0 ▶ my $got = ref $ref ? lc(ref($ref)) : 'scalar';
Branch: 1 branch
BranchTrueFalseTotal
ref $ref ? : 0 0 0
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
BranchTrueFalseTotal
if (not $res and my $e = $@) 0 17 17
Condition33%1/3
not $res and my $e = $@
!ll&&!rl&&r
1700
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 = $@
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless (/\G\]/cg) 5 0 5
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
BranchTrueFalseTotal
if (/\G,/cg) 4 4 8
135 4 $found_separator++;
136 }
137  
138 # End
139 8 _skip_space();
140 8 ▶ last if m/\G\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\]/cg 5 3 8
141  
142 3 _skip_space();
143 3 ▶ redo if $found_separator;
Branch: 1 branch
BranchTrueFalseTotal
if $found_separator 3 0 3
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
BranchTrueFalseTotal
unless (/\G\}/cg) 4 1 5
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
BranchTrueFalseTotal
if (/\G([A-Za-z][a-zA-Z0-9_]*)/cg) { } 4 0 4
160 4 $1;
161 } else {
162 # Quote
163 0 ▶ m/\G"/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G"/cg or _exception("Expected string while parsing object") 0 0 0
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
BranchTrueFalseTotal
/\G=>/cg or _exception("Expected \"=>\" while parsing object") 4 0 4
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
BranchTrueFalseTotal
if /\G,/cg 2 2 4
182  
183 # End
184 4 _skip_space();
185 4 ▶ last if m/\G\}/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\}/cg 4 0 4
186  
187 0 _skip_space();
188 0 ▶ redo if $found_separator;
Branch: 1 branch
BranchTrueFalseTotal
if $found_separator 0 0 0
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
BranchTrueFalseTotal
unless (/\G$quote/cg) 0 3 3
206 0 ▶ _exception('Unexpected character or invalid escape while parsing string')
Branch: 1 branch
BranchTrueFalseTotal
if /\G[\x00-\x1f\\]/ 0 0 0
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
BranchTrueFalseTotal
if (index($str, "\\u") == -1) 3 0 3
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
BranchTrueFalseTotal
if ($str =~ /\G([^\\]*)\\(?:([^u])|u(.{4}))/cg) 0 0 0
220 0 $buffer .= $1;
221  
222 # Popular character
223 0 ▶ if ($2) { $buffer .= $ESCAPE{$2} }
Branch: 1 branch
BranchTrueFalseTotal
if ($2) { } 0 0 0
224  
225 # Escaped
226 else {
227 0 my $ord = hex $3;
228  
229 # Surrogate pair
230 0 ▶ if (($ord & 0xf800) == 0xd800) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ord & 63488) == 55296) 0 0 0
231  
232 # High surrogate
233 0 ▶ ($ord & 0xfc00) == 0xd800
Branch: 1 branch
BranchTrueFalseTotal
($ord & 64512) == 55296 or pos $_ = $pos + pos($str), _exception("Missing high-surrogate") 0 0 0
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
BranchTrueFalseTotal
$str =~ /\G\\u([Dd][C-Fc-f]..)/cg or pos $_ = $pos + pos($str), _exception("Missing low-surrogate") 0 0 0
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
BranchTrueFalseTotal
if (/\G$IGNORABLE_RE/cg) 121 109 230
255 121 ▶ if (m/\G\x0d?\x0a/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\x0d?\x0a/cg) 33 88 121
256 33 ▶ if (@HERE_QUEUE) {
Branch: 1 branch
BranchTrueFalseTotal
if (@JKML::HERE_QUEUE) 6 27 33
257 6 my ($v, $terminator) = @{pop @HERE_QUEUE};
258 6 ▶ if (m/\G(.*?)^([ \t]*)$terminator$/gcsm) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G(.*?)^([ \t]*)$terminator$/cgms) { } 6 0 6
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
BranchTrueFalseTotal
if (/\G([a-zA-Z][a-zA-Z0-9_]*)\(/cg) 1 29 30
280 1 my $func = $1;
281 1 _decode_value(\my $v);
282 1 ▶ m/\G\)/gc or _exception("Missing ) after funcall");
Branch: 1 branch
BranchTrueFalseTotal
/\G\)/cg or _exception("Missing ) after funcall") 1 0 1
283 1 return $$r = $SELF->call($func, $v);
284 }
285  
286 # heredoc
287 29 ▶ if (m/\G<<-([A-Za-z.]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G<<-([A-Za-z.]+)/cg) 6 23 29
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
BranchTrueFalseTotal
if /\Gr('''|""""|'|")(.*?)\1/cg 6 17 23
294  
295 # String
296 17 ▶ return $$r = _decode_string($1) if m/\G(["'])/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G(["'])/cg 3 14 17
297  
298 # Array
299 14 ▶ return $$r = _decode_array() if m/\G\[/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\[/cg 5 9 14
300  
301 # Object
302 9 ▶ return $$r = _decode_object() if m/\G\{/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\{/cg 5 4 9
303  
304 # Number
305 4 ▶ return $$r = 0 + $1
Branch: 1 branch
BranchTrueFalseTotal
if /\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/cg 1 3 4
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
BranchTrueFalseTotal
if /\Gtrue/cg 2 1 3
310  
311 # False
312 1 ▶ return $$r = $FALSE if m/\Gfalse/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\Gfalse/cg 0 1 1
313  
314 # Null
315 1 ▶ return $$r = undef if m/\Gnull/gc; ## no critic (return)
Branch: 1 branch
BranchTrueFalseTotal
if /\Gnull/cg 1 0 1
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
BranchTrueFalseTotal
if (/\G\z/cg) { } 0 0 0
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 || ''
l!l
00
MC/DC0%0/1
pop @lines || ''
pop @lines
Truth table0%0/2
A
A= pop @lines
Aresult
11
00
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
BranchTrueFalseTotal
if (my $ref = ref $value) 5 4 9
367  
368 # Array
369 5 ▶ return _encode_array($value) if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 1 4 5
370  
371 # Object
372 4 ▶ return _encode_object($value) if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 2 2 4
373  
374 # True or false
375 2 ▶ return Types::Serialiser::is_true($value) ? 'true' : 'false' if Types::Serialiser::is_bool($ref);
Branch: 2 branches
BranchTrueFalseTotal
if &Types::Serialiser::is_bool($ref) 0 2 2
&Types::Serialiser::is_true($value) ? : 0 0 0
376  
377 # References to scalars (including blessed) will be encoded as Booleans.
378 2 ▶ return $$value ? 'true' : 'false' if $ref =~ /SCALAR/;
Branch: 2 branches
BranchTrueFalseTotal
if $ref =~ /SCALAR/ 2 0 2
$$value ? : 1 1 2
379  
380 }
381  
382 # Null
383 4 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
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
BranchTrueFalseTotal
if $flags & 12288 and $value * 0 == 0 2 2 4
Condition66%2/3
$flags & 12288 and $value * 0 == 0
!ll&&!rl&&r
202
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
ABresult
0X0
100
111
388  
389 # String
390 2 return _encode_string($value);
391 }
392  
393 3 1;
394 __END__
↑ Summary