Smile/PP.pm

filter: Statementstmt 67.7% 392 / 579 Branchbran 54.1% 221 / 408 Conditioncond 43.7% 70 / 160 MC/DCmcdc 26.0% 24 / 92 Subroutinesub 77.2% 34 / 44 Podpod 100% 4 / 4 totaltotal 57.8% 745 / 1287 CC 272 SCAR 42.4
CC
272
Cov
60%
_decode_value
380.0
_encode_value
240.0
_decode_value_smile
99.4
CRAP 69.3
↑ Summary Smile.pm »
1 9 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Smile::PP;
6  
7 9 ▶use Scalar::Util qw( blessed dualvar looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use constant HEADER => "\x3A\x29\x0A\x03";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use constant MAX_SAFE_INT_DOUBLE => 9_007_199_254_740_992;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use constant MIN_SIGNED_64BIT => -9_223_372_036_854_775_808;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use constant MAX_UNSIGNED_64BIT => 18_446_744_073_709_551_615;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 our $AUTHORITY = 'cpan:TOBYINK';
14 9 our $VERSION = '0.001000';
15  
16 9 ▶use Exporter::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 our @ISA = qw( Exporter::Tiny );
18 9 our @EXPORT_OK = qw( encode_smile decode_smile dump_smile load_smile );
19  
20 9 ▶BEGIN {
Subroutine BEGIN: called CC=10 SCAR=23.9
21 9 my @HELPERS = (
22 _is_arrayref => [
23 [ 'Ref::Util::XS', 'is_plain_arrayref' ],
24 [ 'Type::Tiny::XS', 'ArrayRef' ],
25 q{
26 my ( $v ) = @_;
27 ref( $v ) eq 'ARRAY';
28 },
29 ],
30 _is_hashref => [
31 [ 'Ref::Util::XS', 'is_plain_hashref' ],
32 [ 'Type::Tiny::XS', 'HashRef' ],
33 q{
34 my ( $v ) = @_;
35 ref( $v ) eq 'HASH';
36 },
37 ],
38 _is_dual => [
39 [ 'Scalar::Util', 'isdual' ],
40 q{
41 use B ();
42 my $f = B::svref_2object(\$_[0])->FLAGS;
43 my $SVp_POK = eval { B::SVp_POK() } || 0;
44 my $SVp_IOK = eval { B::SVp_IOK() } || 0;
45 my $SVp_NOK = eval { B::SVp_NOK() } || 0;
46 my $pok = $f & ( B::SVf_POK | $SVp_POK );
47 my $niok = $f & ( B::SVf_IOK | B::SVf_NOK | $SVp_IOK | $SVp_NOK );
48 !!( $pok and $niok );
49 },
50 ],
51 _is_bool => [
52 [ 'builtin', 'is_bool' ],
53 q{
54 my $value = shift;
55 return !!0 unless defined $value;
56 return !!0 if ref $value;
57 return !!0 unless _is_dual( $value );
58 return !!1 if $value and "$value" eq '1' and $value + 0 == 1;
59 return !!1 if not $value and "$value" eq q'' and $value + 0 == 0;
60 return !!0;
61 },
62 ],
63 _created_as_number => [
64 [ 'builtin', 'created_as_number' ],
65 q{
66 use B ();
67 my $value = shift;
68 return !!0 unless defined $value;
69 return !!0 if ref $value;
70 return !!0 if utf8::is_utf8( $value );
71 my $b_obj = B::svref_2object(\$value);
72 my $flags = $b_obj->FLAGS;
73 return !!1 if $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and not( $flags & B::SVp_POK() );
74 return !!0;
75 },
76 ],
77 _created_as_string => [
78 [ 'builtin', 'created_as_string' ],
79 q{
80 my $value = shift;
81 defined $value
82 and not ref $value
83 and not _is_bool( $value )
84 and not _created_as_number( $value );
85 },
86 ],
87 );
88  
89 9 ▶ HELPER: while ( @HELPERS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@HELPERS) 54 9 63
90 54 my ( $name, $implementation_list ) = splice @HELPERS, 0, 2;
91 54 IMPLEMENTATION: for my $i ( @$implementation_list ) {
92 54 ▶ if ( ref $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i) { } 54 0 54
93 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 54 my ( $module, $external_function_name ) = @$i;
95 54 ▶ if ( $module eq 'builtin' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq 'builtin') { } 27 27 54
96 27 ▶ $] >= 5.038 or next IMPLEMENTATION;
Branch: 1 branch
BranchTrueFalseTotal
$] >= "5.038" or next IMPLEMENTATION 27 0 27
97 27 require experimental;
98 27 experimental->import( 'builtin' );
99 27 ▶ if ( defined &{"builtin::$external_function_name"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"builtin::$external_function_name";}) 27 0 27
100 27 *$name = \&{"builtin::$external_function_name"};
101 27 next HELPER;
102 }
103 }
104 else {
105 27 ▶ eval "require $module; 1" or next IMPLEMENTATION;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module; 1" or next IMPLEMENTATION 27 0 27
106 27 ▶ my $f = $module->can( $external_function_name ) or next IMPLEMENTATION;
Branch: 1 branch
BranchTrueFalseTotal
my $f = $module->can($external_function_name) or next IMPLEMENTATION 27 0 27
107 27 *$name = $f;
108 27 next HELPER;
109 }
110 }
111 else {
112 0 ▶ next HELPER if eval "sub $name { $i }; 1";
Branch: 1 branch
BranchTrueFalseTotal
if eval "sub $name { $i }; 1" 0 0 0
113 }
114 }
115 }
116 }
117  
118 46 ▶sub _check_opts {
Subroutine _check_opts: called CC=5 SCAR=16.1
119 46 my ( $name, $opts, $allowed ) = @_;
120 46 ▶ return {} if not defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 33 13 46
121 13 ▶ die 'Options must be a hash reference' if not _is_hashref( $opts );
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($opts) 0 13 13
122 13 for my $k ( keys %$opts ) {
123 14 ▶ die "Unknown option for $name: $k" if not $allowed->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed->{$k} 3 11 14
124 }
125 10 return $opts;
126 }
127  
128 0 ▶sub _pack_u32 { pack('N', $_[0]) }
Subroutine _pack_u32: not called CC=1 SCAR=6.9
129 0 ▶sub _unpack_u32 {
Subroutine _unpack_u32: not called CC=2 SCAR=17.9
130 0 my ( $buf, $pos ) = @_;
131 0 ▶ die 'Unexpected EOF' if $$pos + 4 > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + 4 > length $$buf 0 0 0
132 0 my $v = unpack('N', substr($$buf, $$pos, 4));
133 0 $$pos += 4;
134 0 return $v;
135 }
136  
137 0 ▶sub _pack_str {
Subroutine _pack_str: not called CC=3 SCAR=24.8
138 0 my ( $s ) = @_;
139 0 ▶ my $flag = utf8::is_utf8( $s ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($s) ? : 0 0 0
140 0 my $bytes = $s;
141 0 ▶ utf8::encode( $bytes ) if $flag;
Branch: 1 branch
BranchTrueFalseTotal
if $flag 0 0 0
142 0 return chr( $flag ) . _pack_u32( length( $bytes ) ) . $bytes;
143 }
144  
145 0 ▶sub _unpack_str {
Subroutine _unpack_str: not called CC=4 SCAR=30.0
146 0 my ( $buf, $pos ) = @_;
147 0 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 0 0
148 0 my $flag = ord substr( $$buf, $$pos++, 1 );
149 0 my $len = _unpack_u32( $buf, $pos );
150 0 ▶ die 'Unexpected EOF' if $$pos + $len > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $len > length $$buf 0 0 0
151 0 my $s = substr( $$buf, $$pos, $len );
152 0 $$pos += $len;
153 0 ▶ utf8::decode( $s ) if $flag;
Branch: 1 branch
BranchTrueFalseTotal
if $flag 0 0 0
154 0 return $s;
155 }
156  
157 49 ▶sub _maybe_to_json {
Subroutine _maybe_to_json: called CC=4 SCAR=13.9
158 49 my ( $v ) = @_;
159 49 ▶ return $v if not ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 30 19 49
160 19 ▶ return $v if not blessed $v;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($v) 18 1 19
161 1 ▶ return $v if not $v->can( 'TO_JSON' );
Branch: 1 branch
BranchTrueFalseTotal
unless $v->can("TO_JSON") 0 1 1
162 1 return $v->TO_JSON;
163 }
164  
165 0 ▶sub _encode_value {
Subroutine _encode_value: not called CC=15 SCAR=54.8
166 0 my ( $v ) = @_;
167 0 $v = _maybe_to_json( $v );
168  
169 0 ▶ if ( not defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 0 0
170 0 return 'U';
171 }
172  
173 0 ▶ if ( _is_arrayref( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($v)) 0 0 0
174 0 my $out = 'A' . _pack_u32(scalar @$v);
175 0 $out .= _encode_value( $_ ) for @$v;
176 0 return $out;
177 }
178  
179 0 ▶ if ( _is_hashref( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($v)) 0 0 0
180 0 my @keys = keys %$v;
181 0 my $out = 'H' . _pack_u32(scalar @keys);
182 0 for my $k (@keys) {
183 0 $out .= _pack_str( $k );
184 0 $out .= _encode_value( $v->{$k} );
185 }
186 0 return $out;
187 }
188  
189 0 ▶ die 'Unsupported reference in encode_smile' if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 0 0
190  
191 0 ▶ if ( _is_bool( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::is_bool($v)) 0 0 0
192 0 ▶ return 'b' . ($v ? "\x01" : "\x00");
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
193 }
194  
195 0 ▶ if ( _created_as_string( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_created_as_string($v)) 0 0 0
196 0 return 'S' . _pack_str( $v );
197 }
198  
199 0 ▶ if ( _created_as_number( $v ) or looks_like_number( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_created_as_number($v) or &looks_like_number($v)) 0 0 0
Condition0%0/3
&_created_as_number($v) or &looks_like_number($v)
l!l&&r!l&&!r
000
MC/DC0%0/2
&_created_as_number($v) or &looks_like_number($v)
&_created_as_number($v) &looks_like_number($v)
Truth table0%0/3
A or B
A= &_created_as_number($v)B= &looks_like_number($v)
ABresult
1X1
011
000
200 0 ▶ if ( $v =~ /\A-?[0-9]+\z/ and $v >= -9_223_372_036_854_775_808 and $v <= 9_223_372_036_854_775_807 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\A-?[0-9]+\z/ and $v >= '-9223372036854775808' and $v <= '9223372036854775807') 0 0 0
Condition0%0/6
$v =~ /\A-?[0-9]+\z/ and $v >= '-9223372036854775808' and $v <= '9223372036854775807'
!ll&&!rl&&r
000
$v =~ /\A-?[0-9]+\z/ and $v >= '-9223372036854775808'
!ll&&!rl&&r
000
MC/DC0%0/3
$v =~ /\A-?[0-9]+\z/ and $v >= '-9223372036854775808' and $v <= '9223372036854775807'
$v =~ /\A-?[0-9]+\z/ $v >= '-9223372036854775808' $v <= '9223372036854775807'
Truth table0%0/4
A and B and C
A= $v =~ /\A-?[0-9]+\z/B= $v >= '-9223372036854775808'C= $v <= '9223372036854775807'
ABCresult
0XX0
10X0
1100
1111
201 0 return 'I' . pack('q>', 0 + $v);
202 }
203 0 my $n = "$v";
204 0 return 'N' . _pack_str( $n );
205 }
206  
207 0 return 'S' . _pack_str( "$v" );
208 }
209  
210 9 my ( $jppT, $jppF );
211  
212 0 ▶sub _decode_value {
Subroutine _decode_value: not called CC=19 SCAR=59.4
213 0 my ( $buf, $pos, $json_bool, $use_bigint ) = @_;
214 0 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 0 0
215 0 my $tag = substr $$buf, $$pos++, 1;
216  
217 0 ▶ return undef if $tag eq 'U';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "U" 0 0 0
218 0 ▶ if ( $tag eq 'b' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "b") 0 0 0
219 0 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 0 0
220 0 ▶ if ( $json_bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_bool) 0 0 0
221 0 ▶ if ( not defined $jppT ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jppT) 0 0 0
222 0 require JSON::PP;
223 0 $jppT = JSON::PP::true();
224 0 $jppF = JSON::PP::false();
225 }
226 0 ▶ return ord substr( $$buf, $$pos++, 1 ) ? $jppT : $jppF;
Branch: 1 branch
BranchTrueFalseTotal
ord substr($$buf, $$pos++, 1) ? : 0 0 0
227 }
228 0 ▶ return ord substr( $$buf, $$pos++, 1 ) ? !!1 : !!0;
Branch: 1 branch
BranchTrueFalseTotal
ord substr($$buf, $$pos++, 1) ? : 0 0 0
229 }
230 0 ▶ if ( $tag eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "S") 0 0 0
231 0 return _unpack_str( $buf, $pos );
232 }
233 0 ▶ if ( $tag eq 'I' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "I") 0 0 0
234 0 ▶ die 'Unexpected EOF' if $$pos + 8 > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + 8 > length $$buf 0 0 0
235 0 my $v = unpack( 'q>', substr( $$buf, $$pos, 8 ) );
236 0 $$pos += 8;
237 0 return 0 + $v;
238 }
239 0 ▶ if ( $tag eq 'N' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "N") 0 0 0
240 0 my $s = _unpack_str( $buf, $pos );
241  
242 0 ▶ if ( $use_bigint and $s =~ /\A-?[0-9]{19,}\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_bigint and $s =~ /\A-?[0-9]{19,}\z/) 0 0 0
Condition0%0/3
$use_bigint and $s =~ /\A-?[0-9]{19,}\z/
!ll&&!rl&&r
000
MC/DC0%0/2
$use_bigint and $s =~ /\A-?[0-9]{19,}\z/
$use_bigint $s =~ /\A-?[0-9]{19,}\z/
Truth table0%0/3
A and B
A= $use_bigintB= $s =~ /\A-?[0-9]{19,}\z/
ABresult
0X0
100
111
243 0 require Math::BigInt;
244 0 return Math::BigInt->new( $s );
245 }
246  
247 0 return 0 + $s;
248 }
249 0 ▶ if ( $tag eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "A") 0 0 0
250 0 my $n = _unpack_u32( $buf, $pos );
251 0 my @a;
252 0 push @a, _decode_value( $buf, $pos, $json_bool, $use_bigint ) for 1..$n;
253 0 return \@a;
254 }
255 0 ▶ if ( $tag eq 'H' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "H") 0 0 0
256 0 my $n = _unpack_u32( $buf, $pos );
257 0 my %h;
258 0 for (1..$n) {
259 0 my $k = _unpack_str( $buf, $pos );
260 0 $h{$k} = _decode_value( $buf, $pos, $json_bool, $use_bigint );
261 }
262 0 return \%h;
263 }
264  
265 0 die "Unknown type tag in payload";
266 }
267  
268  
269 5133 ▶sub _normalize_smile_float {
Subroutine _normalize_smile_float: called CC=5 SCAR=16.1
270 5133 my ( $v ) = @_;
271 5133 my $text = "$v";
272 5133 my $num = 0 + $v;
273 5133 my $integer_text = sprintf '%.0f', $num;
274  
275 5133 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 0 + $integer_text and abs $num >= '9007199254740992' and $num >= '-9223372036854775808' and $num <= '18446744073709551615') 198 4935 5133
Condition100%9/9
$num == 0 + $integer_text and abs $num >= '9007199254740992' and $num >= '-9223372036854775808' and $num <= '18446744073709551615'
!ll&&!rl&&r
4301634198
$num == 0 + $integer_text and abs $num >= '9007199254740992' and $num >= '-9223372036854775808'
!ll&&!rl&&r
3656645832
$num == 0 + $integer_text and abs $num >= '9007199254740992'
!ll&&!rl&&r
30446121477
MC/DC100%4/4
$num == 0 + $integer_text and abs $num >= '9007199254740992' and $num >= '-9223372036854775808' and $num <= '18446744073709551615'
$num == 0 + $integer_text abs $num >= '9007199254740992' $num >= '-9223372036854775808' $num <= '18446744073709551615'
Truth table100%5/5
A and B and C and D
A= $num == 0 + $integer_textB= abs $num >= '9007199254740992'C= $num >= '-9223372036854775808'D= $num <= '18446744073709551615'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
276 $num == 0 + $integer_text
277 and abs( $num ) >= MAX_SAFE_INT_DOUBLE
278 and $num >= MIN_SIGNED_64BIT
279 and $num <= MAX_UNSIGNED_64BIT
280 ) {
281 198 return dualvar( $num, $integer_text );
282 }
283  
284 4935 return dualvar( $num, $text );
285 }
286  
287 8596 ▶sub _decode_vint {
Subroutine _decode_vint: called CC=3 SCAR=11.0
288 8596 my ( $buf, $pos ) = @_;
289 8596 my $v = 0;
290  
291 8596 while (1) {
292 21929 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 21929 21929
293 21929 my $byte = ord substr( $$buf, $$pos++, 1 );
294 21929 ▶ if ( $byte & 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte & 128) 8596 13333 21929
295 8596 $v = ( $v << 6 ) | ( $byte & 0x3F );
296 8596 last;
297 }
298 13333 $v = ( $v << 7 ) | ( $byte & 0x7F );
299 }
300  
301 8596 return $v;
302 }
303  
304 11865 ▶sub _zigzag_decode {
Subroutine _zigzag_decode: called CC=2 SCAR=6.9
305 11865 my ( $v ) = @_;
306 11865 ▶ return ( $v & 1 ) ? -( ( $v + 1 ) >> 1 ) : ( $v >> 1 );
Branch: 1 branch
BranchTrueFalseTotal
$v & 1 ? : 5671 6194 11865
307 }
308  
309  
310 5133 ▶sub _decode_fixed_7bit_bytes {
Subroutine _decode_fixed_7bit_bytes: called CC=6 SCAR=18.3
311 5133 my ( $buf, $pos, $groups, $bits_needed ) = @_;
312 5133 my $bits = q{};
313  
314 5133 for ( 1 .. $groups ) {
315 51330 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 51330 51330
316 51330 my $byte = ord substr( $$buf, $$pos++, 1 );
317 51330 $bits .= sprintf '%07b', $byte & 0x7F;
318 }
319  
320 5133 ▶ if ( defined $bits_needed and length( $bits ) > $bits_needed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bits_needed and length $bits > $bits_needed) 5133 0 5133
Condition33%1/3
defined $bits_needed and length $bits > $bits_needed
!ll&&!rl&&r
005133
MC/DC0%0/2
defined $bits_needed and length $bits > $bits_needed
defined $bits_needed length $bits > $bits_needed
Truth table33%1/3
A and B
A= defined $bits_neededB= length $bits > $bits_needed
ABresult
0X0
100
111
321 5133 $bits = substr( $bits, -$bits_needed );
322 }
323  
324 5133 my $out = q{};
325 5133 ▶ for ( my $i = 0; $i < length $bits; $i += 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $bits) 41064 5133 46197
326 41064 $out .= chr oct( '0b' . substr( $bits, $i, 8 ) );
327 }
328  
329 5133 return $out;
330 }
331  
332 4 ▶sub _decode_7bit_binary {
Subroutine _decode_7bit_binary: called CC=4 SCAR=13.9
333 4 my ( $buf, $pos, $len ) = @_;
334 4 my $bits = q{};
335  
336 4 ▶ while ( length $bits < $len * 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bits < $len * 8) 60 4 64
337 60 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 60 60
338 60 my $byte = ord substr( $$buf, $$pos++, 1 );
339 60 $bits .= sprintf '%07b', $byte & 0x7F;
340 }
341  
342 4 $bits = substr( $bits, 0, $len * 8 );
343 4 my $out = q{};
344 4 ▶ for ( my $i = 0; $i < length $bits; $i += 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $bits) 52 4 56
345 52 $out .= chr oct( '0b' . substr( $bits, $i, 8 ) );
346 }
347  
348 4 return $out;
349 }
350  
351  
352 2 ▶sub _raw_bytes_to_native_number {
Subroutine _raw_bytes_to_native_number: called CC=2 SCAR=6.9
353 2 my ( $raw ) = @_;
354 2 my $v = 0.0;
355  
356 2 for my $byte ( unpack 'C*', $raw ) {
357 26 $v = ( $v * 256 ) + $byte;
358 }
359  
360 2 return 0 + $v;
361 }
362  
363 472 ▶sub _decode_long_text {
Subroutine _decode_long_text: called CC=4 SCAR=13.9
364 472 my ( $buf, $pos, $utf8 ) = @_;
365 472 my $s = q{};
366  
367 472 while (1) {
368 178546 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 178546 178546
369 178546 my $byte = ord substr( $$buf, $$pos++, 1 );
370 178546 ▶ last if $byte == 0xFC;
Branch: 1 branch
BranchTrueFalseTotal
if $byte == 252 472 178074 178546
371 178074 $s .= chr $byte;
372 }
373  
374 472 ▶ utf8::decode( $s ) if $utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 18 454 472
375 472 return $s;
376 }
377  
378 4046 ▶sub _shared_push {
Subroutine _shared_push: called CC=3 SCAR=11.0
379 4046 my ( $ary, $v ) = @_;
380 4046 ▶ return if not defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 4046 4046
381  
382 4046 ▶ if ( @$ary >= 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ary >= 1024) 1 4045 4046
383 1 @$ary = ();
384 }
385  
386 4046 push @$ary, $v;
387 }
388  
389 53161 ▶sub _decode_value_smile {
Subroutine _decode_value_smile: called CC=53 SCAR=46.0
390 53161 my ( $buf, $pos, $ctx, $json_bool, $use_bigint ) = @_;
391 53161 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 53161 53161
392 53161 my $t = ord substr( $$buf, $$pos++, 1 );
393  
394 53161 ▶ if ( $t <= 0x1F ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t <= 31) 11414 41747 53161
395 11414 ▶ die 'Invalid shared value reference' if $t == 0 or not defined $ctx->{shared_values}[ $t - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
if $t == 0 or not defined $ctx->{"shared_values"}[$t - 1] 0 11414 11414
Condition33%1/3
$t == 0 or not defined $ctx->{"shared_values"}[$t - 1]
l!l&&r!l&&!r
0011414
MC/DC0%0/2
$t == 0 or not defined $ctx->{"shared_values"}[$t - 1]
$t == 0 not defined $ctx->{"shared_values"}[$t - 1]
Truth table33%1/3
A or not(B)
A= $t == 0B= not defined $ctx->{"shared_values"}[$t - 1]
ABresult
1X1
011
000
396 11414 return $ctx->{shared_values}[ $t - 1 ];
397 }
398 41747 ▶ if ( $t == 0x20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 32) 3 41744 41747
399 3 ▶ _shared_push( $ctx->{shared_values}, q{} ) if $ctx->{enable_shared_values};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_values"} 3 0 3
400 3 return q{};
401 }
402 41744 ▶ if ( $t == 0x21 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 33) 6 41738 41744
403 6 return undef;
404 }
405 41738 ▶ if ( $t == 0x22 or $t == 0x23 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 34 or $t == 35) 5 41733 41738
Condition100%3/3
$t == 34 or $t == 35
l!l&&r!l&&!r
2341733
MC/DC100%2/2
$t == 34 or $t == 35
$t == 34 $t == 35
Truth table100%3/3
A or B
A= $t == 34B= $t == 35
ABresult
1X1
011
000
406 5 my $is_true = $t == 0x23;
407 5 ▶ if ( $json_bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_bool) 5 0 5
408 5 ▶ if ( not defined $jppT ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $jppT) 1 4 5
409 1 require JSON::PP;
410 1 $jppT = JSON::PP::true();
411 1 $jppF = JSON::PP::false();
412 }
413 5 ▶ return $is_true ? $jppT : $jppF;
Branch: 1 branch
BranchTrueFalseTotal
$is_true ? : 3 2 5
414 }
415 0 ▶ return $is_true ? !!1 : !!0;
Branch: 1 branch
BranchTrueFalseTotal
$is_true ? : 0 0 0
416 }
417 41733 ▶ if ( $t == 0x24 or $t == 0x25 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 36 or $t == 37) 8592 33141 41733
Condition66%2/3
$t == 36 or $t == 37
l!l&&r!l&&!r
8592033141
MC/DC50%1/2
$t == 36 or $t == 37
$t == 36 $t == 37
Truth table66%2/3
A or B
A= $t == 36B= $t == 37
ABresult
1X1
011
000
418 8592 my $v = _zigzag_decode( _decode_vint( $buf, $pos ) );
419 8592 return $v;
420 }
421 33141 ▶ if ( $t == 0x26 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 38) 4 33137 33141
422 4 my $len = _decode_vint( $buf, $pos );
423 4 my $raw = _decode_7bit_binary( $buf, $pos, $len );
424 4 my $hex = unpack 'H*', $raw;
425  
426 4 ▶ if ( $use_bigint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_bigint) 2 2 4
427 2 require Math::BigInt;
428 2 ▶ return Math::BigInt->from_hex( '0x' . ( $hex or '0' ) );
Condition50%1/2
$hex || '0'
l!l
20
MC/DC0%0/1
$hex || '0'
$hex
Truth table50%1/2
A
A= $hex
Aresult
11
00
429 }
430  
431 2 return _raw_bytes_to_native_number( $raw );
432 }
433 33137 ▶ if ( $t == 0x2A ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 42) 0 33137 33137
434 0 my $scale = _zigzag_decode( _decode_vint( $buf, $pos ) );
435 0 my $len = _decode_vint( $buf, $pos );
436 0 my $raw = _decode_7bit_binary( $buf, $pos, $len );
437 0 require Math::BigInt;
438 0 require Math::BigFloat;
439 0 my $hex = unpack 'H*', $raw;
440 0 ▶ my $unscaled = Math::BigInt->from_hex( '0x' . ( $hex or '0' ) );
Condition0%0/2
$hex || '0'
l!l
00
MC/DC0%0/1
$hex || '0'
$hex
Truth table0%0/2
A
A= $hex
Aresult
11
00
441 0 my $v = Math::BigFloat->new( $unscaled );
442 0 ▶ if ( $scale > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scale > 0) { } 0 0 0
elsif ($scale < 0) { } 0 0 0
443 0 $v->bdiv( Math::BigFloat->new( 10 )->bpow( $scale ) );
444 }
445 elsif ( $scale < 0 ) {
446 0 $v->bmul( Math::BigFloat->new( 10 )->bpow( -$scale ) );
447 }
448 0 ▶ return $use_bigint ? $v : 0 + $v;
Branch: 1 branch
BranchTrueFalseTotal
$use_bigint ? : 0 0 0
449 }
450 33137 ▶ if ( $t == 0x28 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 40) 0 33137 33137
451 0 my $raw = _decode_fixed_7bit_bytes( $buf, $pos, 5, 32 );
452 0 my $v = unpack( 'f>', $raw );
453 0 return _normalize_smile_float( $v );
454 }
455 33137 ▶ if ( $t == 0x29 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 41) 5133 28004 33137
456 5133 my $raw = _decode_fixed_7bit_bytes( $buf, $pos, 10, 64 );
457 5133 my $v = unpack( 'd>', $raw );
458 5133 return _normalize_smile_float( $v );
459 }
460 28004 ▶ if ( $t >= 0x40 and $t <= 0x7F ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 64 and $t <= 127) 3316 24688 28004
Condition66%2/3
$t >= 64 and $t <= 127
!ll&&!rl&&r
0246883316
MC/DC50%1/2
$t >= 64 and $t <= 127
$t >= 64 $t <= 127
Truth table66%2/3
A and B
A= $t >= 64B= $t <= 127
ABresult
0X0
100
111
461 3316 ▶ my $len = ( $t <= 0x5F ) ? ( ( $t & 0x1F ) + 1 ) : ( ( $t & 0x1F ) + 33 );
Branch: 1 branch
BranchTrueFalseTotal
$t <= 95 ? : 3046 270 3316
462 3316 ▶ die 'Unexpected EOF' if $$pos + $len > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $len > length $$buf 0 3316 3316
463 3316 my $s = substr( $$buf, $$pos, $len );
464 3316 $$pos += $len;
465 3316 ▶ _shared_push( $ctx->{shared_values}, $s ) if $ctx->{enable_shared_values};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_values"} 3314 2 3316
466 3316 return $s;
467 }
468 24688 ▶ if ( $t >= 0x80 and $t <= 0xBF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 128 and $t <= 191) 16 24672 24688
Condition66%2/3
$t >= 128 and $t <= 191
!ll&&!rl&&r
02467216
MC/DC50%1/2
$t >= 128 and $t <= 191
$t >= 128 $t <= 191
Truth table66%2/3
A and B
A= $t >= 128B= $t <= 191
ABresult
0X0
100
111
469 16 ▶ my $len = ( $t <= 0x9F ) ? ( ( $t & 0x1F ) + 2 ) : ( ( $t & 0x1F ) + 34 );
Branch: 1 branch
BranchTrueFalseTotal
$t <= 159 ? : 14 2 16
470 16 ▶ die 'Unexpected EOF' if $$pos + $len > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $len > length $$buf 0 16 16
471 16 my $s = substr( $$buf, $$pos, $len );
472 16 $$pos += $len;
473 16 utf8::decode( $s );
474 16 ▶ _shared_push( $ctx->{shared_values}, $s ) if $ctx->{enable_shared_values};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_values"} 13 3 16
475 16 return $s;
476 }
477 24672 ▶ if ( $t >= 0xC0 and $t <= 0xDF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 192 and $t <= 223) 3273 21399 24672
Condition66%2/3
$t >= 192 and $t <= 223
!ll&&!rl&&r
0213993273
MC/DC50%1/2
$t >= 192 and $t <= 223
$t >= 192 $t <= 223
Truth table66%2/3
A and B
A= $t >= 192B= $t <= 223
ABresult
0X0
100
111
478 3273 return _zigzag_decode( $t & 0x1F );
479 }
480 21399 ▶ if ( $t >= 0xE0 and $t <= 0xE3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 224 and $t <= 227) 454 20945 21399
Condition66%2/3
$t >= 224 and $t <= 227
!ll&&!rl&&r
020945454
MC/DC50%1/2
$t >= 224 and $t <= 227
$t >= 224 $t <= 227
Truth table66%2/3
A and B
A= $t >= 224B= $t <= 227
ABresult
0X0
100
111
481 454 return _decode_long_text( $buf, $pos, 0 );
482 }
483 20945 ▶ if ( $t >= 0xE4 and $t <= 0xE7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 228 and $t <= 231) 18 20927 20945
Condition66%2/3
$t >= 228 and $t <= 231
!ll&&!rl&&r
02092718
MC/DC50%1/2
$t >= 228 and $t <= 231
$t >= 228 $t <= 231
Truth table66%2/3
A and B
A= $t >= 228B= $t <= 231
ABresult
0X0
100
111
484 18 return _decode_long_text( $buf, $pos, 1 );
485 }
486 20927 ▶ if ( $t == 0xE8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 232) 0 20927 20927
487 0 my $len = _decode_vint( $buf, $pos );
488 0 return _decode_7bit_binary( $buf, $pos, $len );
489 }
490 20927 ▶ if ( $t >= 0xEC and $t <= 0xEF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 236 and $t <= 239) 9606 11321 20927
Condition66%2/3
$t >= 236 and $t <= 239
!ll&&!rl&&r
0113219606
MC/DC50%1/2
$t >= 236 and $t <= 239
$t >= 236 $t <= 239
Truth table66%2/3
A and B
A= $t >= 236B= $t <= 239
ABresult
0X0
100
111
491 9606 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 9606 9606
492 9606 my $lsb = ord substr( $$buf, $$pos++, 1 );
493 9606 my $idx = ( ( $t & 0x03 ) << 8 ) | $lsb;
494 9606 ▶ die 'Invalid shared value reference' if $idx < 31 or not defined $ctx->{shared_values}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 31 or not defined $ctx->{"shared_values"}[$idx] 0 9606 9606
Condition33%1/3
$idx < 31 or not defined $ctx->{"shared_values"}[$idx]
l!l&&r!l&&!r
009606
MC/DC0%0/2
$idx < 31 or not defined $ctx->{"shared_values"}[$idx]
$idx < 31 not defined $ctx->{"shared_values"}[$idx]
Truth table33%1/3
A or not(B)
A= $idx < 31B= not defined $ctx->{"shared_values"}[$idx]
ABresult
1X1
011
000
495 9606 return $ctx->{shared_values}[$idx];
496 }
497 11321 ▶ if ( $t == 0xF8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 248) 734 10587 11321
498 734 my @a;
499 734 while (1) {
500 20260 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 20260 20260
501 20260 ▶ last if ord substr( $$buf, $$pos, 1 ) == 0xF9;
Branch: 1 branch
BranchTrueFalseTotal
if ord substr($$buf, $$pos, 1) == 249 734 19526 20260
502 19526 push @a, _decode_value_smile( $buf, $pos, $ctx, $json_bool, $use_bigint );
503 }
504 734 $$pos++;
505 734 return \@a;
506 }
507 10587 ▶ if ( $t == 0xFA ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 250) 10587 0 10587
508 10587 my %h;
509 10587 while (1) {
510 44192 my $k = _decode_key_smile( $buf, $pos, $ctx );
511 44192 ▶ last if not defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 10587 33605 44192
512 33605 $h{$k} = _decode_value_smile( $buf, $pos, $ctx, $json_bool, $use_bigint );
513 }
514 10587 return \%h;
515 }
516  
517 0 die "Unknown type tag in payload";
518 }
519  
520 44192 ▶sub _decode_key_smile {
Subroutine _decode_key_smile: called CC=23 SCAR=38.2
521 44192 my ( $buf, $pos, $ctx ) = @_;
522 44192 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 44192 44192
523 44192 my $t = ord substr( $$buf, $$pos++, 1 );
524  
525 44192 ▶ return undef if $t == 0xFB;
Branch: 1 branch
BranchTrueFalseTotal
if $t == 251 10587 33605 44192
526  
527 33605 ▶ if ( $t == 0x20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 32) 0 33605 33605
528 0 ▶ _shared_push( $ctx->{shared_names}, q{} ) if $ctx->{enable_shared_names};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_names"} 0 0 0
529 0 return q{};
530 }
531 33605 ▶ if ( $t >= 0x30 and $t <= 0x33 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 48 and $t <= 51) 9884 23721 33605
Condition66%2/3
$t >= 48 and $t <= 51
!ll&&!rl&&r
0237219884
MC/DC50%1/2
$t >= 48 and $t <= 51
$t >= 48 $t <= 51
Truth table66%2/3
A and B
A= $t >= 48B= $t <= 51
ABresult
0X0
100
111
532 9884 ▶ die 'Unexpected EOF' if $$pos >= length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= length $$buf 0 9884 9884
533 9884 my $lsb = ord substr( $$buf, $$pos++, 1 );
534 9884 my $idx = ( ( $t & 0x03 ) << 8 ) | $lsb;
535 9884 ▶ die 'Invalid shared name reference' if $idx < 64 or not defined $ctx->{shared_names}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 64 or not defined $ctx->{"shared_names"}[$idx] 0 9884 9884
Condition33%1/3
$idx < 64 or not defined $ctx->{"shared_names"}[$idx]
l!l&&r!l&&!r
009884
MC/DC0%0/2
$idx < 64 or not defined $ctx->{"shared_names"}[$idx]
$idx < 64 not defined $ctx->{"shared_names"}[$idx]
Truth table33%1/3
A or not(B)
A= $idx < 64B= not defined $ctx->{"shared_names"}[$idx]
ABresult
1X1
011
000
536 9884 return $ctx->{shared_names}[$idx];
537 }
538 23721 ▶ if ( $t >= 0x40 and $t <= 0x7F ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 64 and $t <= 127) 23005 716 23721
Condition66%2/3
$t >= 64 and $t <= 127
!ll&&!rl&&r
071623005
MC/DC50%1/2
$t >= 64 and $t <= 127
$t >= 64 $t <= 127
Truth table66%2/3
A and B
A= $t >= 64B= $t <= 127
ABresult
0X0
100
111
539 23005 my $idx = $t & 0x3F;
540 23005 ▶ die 'Invalid shared name reference' if not defined $ctx->{shared_names}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ctx->{"shared_names"}[$idx] 0 23005 23005
541 23005 return $ctx->{shared_names}[$idx];
542 }
543 716 ▶ if ( $t >= 0x80 and $t <= 0xBF ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 128 and $t <= 191) 715 1 716
Condition66%2/3
$t >= 128 and $t <= 191
!ll&&!rl&&r
01715
MC/DC50%1/2
$t >= 128 and $t <= 191
$t >= 128 $t <= 191
Truth table66%2/3
A and B
A= $t >= 128B= $t <= 191
ABresult
0X0
100
111
544 715 my $len = ( $t & 0x3F ) + 1;
545 715 ▶ die 'Unexpected EOF' if $$pos + $len > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $len > length $$buf 0 715 715
546 715 my $k = substr( $$buf, $$pos, $len );
547 715 $$pos += $len;
548 715 ▶ _shared_push( $ctx->{shared_names}, $k ) if $ctx->{enable_shared_names};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_names"} 715 0 715
549 715 return $k;
550 }
551 1 ▶ if ( $t >= 0xC0 and $t <= 0xF7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= 192 and $t <= 247) 1 0 1
Condition33%1/3
$t >= 192 and $t <= 247
!ll&&!rl&&r
001
MC/DC0%0/2
$t >= 192 and $t <= 247
$t >= 192 $t <= 247
Truth table33%1/3
A and B
A= $t >= 192B= $t <= 247
ABresult
0X0
100
111
552 1 my $len = ( $t & 0x3F ) + 2;
553 1 ▶ die 'Unexpected EOF' if $$pos + $len > length $$buf;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $len > length $$buf 0 1 1
554 1 my $k = substr( $$buf, $$pos, $len );
555 1 $$pos += $len;
556 1 utf8::decode( $k );
557 1 ▶ _shared_push( $ctx->{shared_names}, $k ) if $ctx->{enable_shared_names};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_names"} 1 0 1
558 1 return $k;
559 }
560 0 ▶ if ( $t == 0x34 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == 52) 0 0 0
561 0 my $k = _decode_long_text( $buf, $pos, 1 );
562 0 ▶ _shared_push( $ctx->{shared_names}, $k ) if $ctx->{enable_shared_names};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"enable_shared_names"} 0 0 0
563 0 return $k;
564 }
565  
566 0 die 'Invalid object key token';
567 }
568  
569 0 ▶sub _contains_shared_name_pattern {
Subroutine _contains_shared_name_pattern: not called CC=6 SCAR=37.4
570 0 my ( $v ) = @_;
571 0 ▶ return 0 if not _is_arrayref( $v );
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_arrayref($v) 0 0 0
572 0 my %seen;
573 0 for my $e ( @$v ) {
574 0 ▶ next if not _is_hashref( $e );
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($e) 0 0 0
575 0 for my $k ( keys %$e ) {
576 0 ▶ return 1 if ++$seen{$k} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen{$k} > 1 0 0 0
577 }
578 }
579 0 return 0;
580 }
581  
582 0 ▶sub _contains_shared_short_string_value {
Subroutine _contains_shared_short_string_value: not called CC=8 SCAR=42.8
583 0 my ( $v ) = @_;
584 0 ▶ return 0 if not _is_arrayref( $v );
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_arrayref($v) 0 0 0
585 0 my %seen;
586 0 for my $e ( @$v ) {
587 0 ▶ next if ref $e;
Branch: 1 branch
BranchTrueFalseTotal
if ref $e 0 0 0
588 0 ▶ next if not defined( $e ) or utf8::is_utf8( $e ) or length( $e ) > 64;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $e or utf8::is_utf8($e) or length $e > 64 0 0 0
Condition0%0/6
not defined $e or utf8::is_utf8($e) or length $e > 64
l!l&&r!l&&!r
000
not defined $e or utf8::is_utf8($e)
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $e or utf8::is_utf8($e) or length $e > 64
not defined $e utf8::is_utf8($e) length $e > 64
Truth table0%0/4
not(A) or B or C
A= not defined $eB= utf8::is_utf8($e)C= length $e > 64
ABCresult
1XX1
01X1
0011
0000
589 0 ▶ return 1 if ++$seen{$e} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen{$e} > 1 0 0 0
590 }
591 0 return 0;
592 }
593  
594 0 ▶sub _encode_vint {
Subroutine _encode_vint: not called CC=2 SCAR=17.9
595 0 my ( $v ) = @_;
596 0 my @groups;
597  
598 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$v > 0 and do { do { unshift @groups, $v & 127; $v >>= 7 } } 0 0 0
599 0 unshift @groups, $v & 0x7F;
600 0 $v >>= 7;
601 } while ( $v > 0 );
602  
603 0 $groups[-1] |= 0x80;
604 0 return pack 'C*', @groups;
605 }
606  
607 15 ▶sub _zigzag_encode {
Subroutine _zigzag_encode: called CC=2 SCAR=7.2
608 15 my ( $v ) = @_;
609 15 ▶ return ( $v < 0 ) ? ( ( -$v ) * 2 - 1 ) : ( $v * 2 );
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 0 15 15
610 }
611  
612 0 ▶sub _encode_7bit_binary {
Subroutine _encode_7bit_binary: not called CC=2 SCAR=17.9
613 0 my ( $raw ) = @_;
614 0 my $bits = join q{}, map { sprintf '%08b', $_ } unpack 'C*', $raw;
615 0 my $pad = ( 7 - ( length( $bits ) % 7 ) ) % 7;
616 0 $bits .= '0' x $pad;
617  
618 0 my @bytes;
619 0 ▶ for ( my $i = 0; $i < length $bits; $i += 7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $bits) 0 0 0
620 0 push @bytes, oct '0b' . substr( $bits, $i, 7 );
621 }
622  
623 0 return pack 'C*', @bytes;
624 }
625  
626 9 ▶sub _encode_short_or_long_text {
Subroutine _encode_short_or_long_text: called CC=14 SCAR=34.9
627 9 my ( $bytes, $is_utf8 ) = @_;
628 9 my $len = length $bytes;
629  
630 9 ▶ if ( not $is_utf8 and $len >= 1 and $len <= 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_utf8 and $len >= 1 and $len <= 32) 8 1 9
Condition66%4/6
not $is_utf8 and $len >= 1 and $len <= 32
!ll&&!rl&&r
108
not $is_utf8 and $len >= 1
!ll&&!rl&&r
018
MC/DC33%1/3
not $is_utf8 and $len >= 1 and $len <= 32
not $is_utf8 $len >= 1 $len <= 32
Truth table50%2/4
not(A) and B and C
A= not $is_utf8B= $len >= 1C= $len <= 32
ABCresult
0XX0
10X0
1100
1111
631 8 return chr( 0x40 + $len - 1 ) . $bytes;
632 }
633 1 ▶ if ( not $is_utf8 and $len >= 33 and $len <= 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_utf8 and $len >= 33 and $len <= 64) 0 1 1
Condition33%2/6
not $is_utf8 and $len >= 33 and $len <= 64
!ll&&!rl&&r
100
not $is_utf8 and $len >= 33
!ll&&!rl&&r
010
MC/DC0%0/3
not $is_utf8 and $len >= 33 and $len <= 64
not $is_utf8 $len >= 33 $len <= 64
Truth table25%1/4
not(A) and B and C
A= not $is_utf8B= $len >= 33C= $len <= 64
ABCresult
0XX0
10X0
1100
1111
634 0 return chr( 0x60 + $len - 33 ) . $bytes;
635 }
636 1 ▶ if ( $is_utf8 and $len >= 2 and $len <= 33 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_utf8 and $len >= 2 and $len <= 33) 0 1 1
Condition33%2/6
$is_utf8 and $len >= 2 and $len <= 33
!ll&&!rl&&r
100
$is_utf8 and $len >= 2
!ll&&!rl&&r
100
MC/DC0%0/3
$is_utf8 and $len >= 2 and $len <= 33
$is_utf8 $len >= 2 $len <= 33
Truth table25%1/4
A and B and C
A= $is_utf8B= $len >= 2C= $len <= 33
ABCresult
0XX0
10X0
1100
1111
637 0 return chr( 0x80 + $len - 2 ) . $bytes;
638 }
639 1 ▶ if ( $is_utf8 and $len >= 34 and $len <= 65 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_utf8 and $len >= 34 and $len <= 65) 0 1 1
Condition33%2/6
$is_utf8 and $len >= 34 and $len <= 65
!ll&&!rl&&r
100
$is_utf8 and $len >= 34
!ll&&!rl&&r
100
MC/DC0%0/3
$is_utf8 and $len >= 34 and $len <= 65
$is_utf8 $len >= 34 $len <= 65
Truth table25%1/4
A and B and C
A= $is_utf8B= $len >= 34C= $len <= 65
ABCresult
0XX0
10X0
1100
1111
640 0 return chr( 0xA0 + $len - 34 ) . $bytes;
641 }
642  
643 1 ▶ my $marker = $is_utf8 ? 0xE4 : 0xE0;
Branch: 1 branch
BranchTrueFalseTotal
$is_utf8 ? : 0 1 1
644 1 return chr( $marker ) . $bytes . "\xFC";
645 }
646  
647 23 ▶sub _encode_key_smile {
Subroutine _encode_key_smile: called CC=15 SCAR=36.1
648 23 my ( $key, $ctx ) = @_;
649  
650 23 ▶ if ( defined $ctx->{name_to_idx}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx->{"name_to_idx"}{$key}) 1 22 23
651 1 my $idx = $ctx->{name_to_idx}{$key};
652 1 ▶ if ( $idx <= 63 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= 63) 1 0 1
653 1 return chr( 0x40 + $idx );
654 }
655 0 ▶ if ( $idx <= 1023 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= 1023) 0 0 0
656 0 my $msb = ( $idx >> 8 ) & 0x03;
657 0 my $lsb = $idx & 0xFF;
658 0 return chr( 0x30 + $msb ) . chr( $lsb );
659 }
660 }
661  
662 22 ▶ if ( $ctx->{enable_shared_names} and @{$ctx->{shared_names}} < 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"enable_shared_names"} and @{$ctx->{"shared_names"};} < 1024) 19 3 22
Condition66%2/3
$ctx->{"enable_shared_names"} and @{$ctx->{"shared_names"};} < 1024
!ll&&!rl&&r
3019
MC/DC50%1/2
$ctx->{"enable_shared_names"} and @{$ctx->{"shared_names"};} < 1024
$ctx->{"enable_shared_names"} @{$ctx->{"shared_names"};} < 1024
Truth table66%2/3
A and B
A= $ctx->{"enable_shared_names"}B= @{$ctx->{"shared_names"};} < 1024
ABresult
0X0
100
111
663 19 ▶ if ( @{$ctx->{shared_names}} >= 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ctx->{"shared_names"};} >= 1024) 0 19 19
664 0 $ctx->{shared_names} = [];
665 0 $ctx->{name_to_idx} = {};
666 }
667  
668 19 my $idx = scalar @{$ctx->{shared_names}};
669 19 $ctx->{name_to_idx}{$key} = $idx;
670 19 push @{$ctx->{shared_names}}, $key;
671 }
672  
673 22 ▶ return "\x20" if $key eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" 0 22 22
674  
675 22 my $bytes = $key;
676 22 my $is_utf8 = utf8::is_utf8 $bytes;
677 22 ▶ utf8::encode( $bytes ) if $is_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $is_utf8 0 22 22
678 22 my $len = length $bytes;
679  
680 22 ▶ if ( not $is_utf8 and $len >= 1 and $len <= 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_utf8 and $len >= 1 and $len <= 64) 22 0 22
Condition33%2/6
not $is_utf8 and $len >= 1 and $len <= 64
!ll&&!rl&&r
0022
not $is_utf8 and $len >= 1
!ll&&!rl&&r
0022
MC/DC0%0/3
not $is_utf8 and $len >= 1 and $len <= 64
not $is_utf8 $len >= 1 $len <= 64
Truth table25%1/4
not(A) and B and C
A= not $is_utf8B= $len >= 1C= $len <= 64
ABCresult
0XX0
10X0
1100
1111
681 22 return chr( 0x80 + $len - 1 ) . $bytes;
682 }
683 0 ▶ if ( $is_utf8 and $len >= 2 and $len <= 65 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_utf8 and $len >= 2 and $len <= 65) 0 0 0
Condition0%0/6
$is_utf8 and $len >= 2 and $len <= 65
!ll&&!rl&&r
000
$is_utf8 and $len >= 2
!ll&&!rl&&r
000
MC/DC0%0/3
$is_utf8 and $len >= 2 and $len <= 65
$is_utf8 $len >= 2 $len <= 65
Truth table0%0/4
A and B and C
A= $is_utf8B= $len >= 2C= $len <= 65
ABCresult
0XX0
10X0
1100
1111
684 0 return chr( 0xC0 + $len - 2 ) . $bytes;
685 }
686  
687 0 return "\x34" . $bytes . "\xFC";
688 }
689  
690 49 ▶sub _encode_value_smile {
Subroutine _encode_value_smile: called CC=23 SCAR=34.8
691 49 my ( $v, $ctx ) = @_;
692 49 $v = _maybe_to_json( $v );
693  
694 49 ▶ if ( not defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 1 48 49
695 1 return "\x21";
696 }
697  
698 48 ▶ if ( _is_arrayref( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($v)) 6 42 48
699 6 my $out = "\xF8";
700 6 $out .= _encode_value_smile( $_, $ctx ) for @$v;
701 6 $out .= "\xF9";
702 6 return $out;
703 }
704  
705 42 ▶ if ( _is_hashref( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($v)) 13 29 42
706 13 my @keys = keys %$v;
707 13 ▶ @keys = sort @keys if $ctx->{canonical};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"canonical"} 1 12 13
708  
709 13 my $out = "\xFA";
710 13 for my $k ( @keys ) {
711 23 $out .= _encode_key_smile( $k, $ctx );
712 23 $out .= _encode_value_smile( $v->{$k}, $ctx );
713 }
714 13 $out .= "\xFB";
715 13 return $out;
716 }
717  
718 29 ▶ die 'Unsupported reference in encode_smile' if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 29 29
719  
720 29 ▶ if ( _is_bool( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::is_bool($v)) 0 29 29
721 0 ▶ return $v ? "\x23" : "\x22";
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 0 0 0
722 }
723  
724 29 ▶ if ( _created_as_number( $v ) or looks_like_number( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_created_as_number($v) or &looks_like_number($v)) 15 14 29
Condition66%2/3
&_created_as_number($v) or &looks_like_number($v)
l!l&&r!l&&!r
15014
MC/DC50%1/2
&_created_as_number($v) or &looks_like_number($v)
&_created_as_number($v) &looks_like_number($v)
Truth table66%2/3
A or B
A= &_created_as_number($v)B= &looks_like_number($v)
ABresult
1X1
011
000
725 15 ▶ if ( $v =~ /\A-?[0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /\A-?[0-9]+\z/) 15 0 15
726 15 my $n = 0 + $v;
727 15 ▶ if ( $n >= -16 and $n <= 15 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= -16 and $n <= 15) 15 0 15
Condition33%1/3
$n >= -16 and $n <= 15
!ll&&!rl&&r
0015
MC/DC0%0/2
$n >= -16 and $n <= 15
$n >= -16 $n <= 15
Truth table33%1/3
A and B
A= $n >= -16B= $n <= 15
ABresult
0X0
100
111
728 15 return chr( 0xC0 + _zigzag_encode( $n ) );
729 }
730 0 return "\x24" . _encode_vint( _zigzag_encode( $n ) );
731 }
732  
733 0 my $raw = pack 'd>', 0 + $v;
734 0 return "\x29" . _encode_7bit_binary( $raw );
735 }
736  
737 14 my $text = "$v";
738 14 ▶ my $cache_key = utf8::is_utf8( $text ) ? "u\0$text" : "b\0$text";
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($text) ? : 0 14 14
739  
740 14 ▶ if ( $ctx->{enable_shared_values} and defined $ctx->{value_to_idx}{$cache_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"enable_shared_values"} and defined $ctx->{"value_to_idx"}{$cache_key}) 5 9 14
Condition100%3/3
$ctx->{"enable_shared_values"} and defined $ctx->{"value_to_idx"}{$cache_key}
!ll&&!rl&&r
275
MC/DC100%2/2
$ctx->{"enable_shared_values"} and defined $ctx->{"value_to_idx"}{$cache_key}
$ctx->{"enable_shared_values"} defined $ctx->{"value_to_idx"}{$cache_key}
Truth table100%3/3
A and B
A= $ctx->{"enable_shared_values"}B= defined $ctx->{"value_to_idx"}{$cache_key}
ABresult
0X0
100
111
741 5 my $idx = $ctx->{value_to_idx}{$cache_key};
742 5 ▶ if ( $idx <= 30 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= 30) 5 0 5
743 5 return chr( $idx + 1 );
744 }
745 0 ▶ if ( $idx <= 1023 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= 1023) 0 0 0
746 0 my $msb = ( $idx >> 8 ) & 0x03;
747 0 my $lsb = $idx & 0xFF;
748 0 return chr( 0xEC + $msb ) . chr( $lsb );
749 }
750 }
751  
752 9 ▶ if ( $ctx->{enable_shared_values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"enable_shared_values"}) 7 2 9
753 7 ▶ if ( @{$ctx->{shared_values}} >= 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$ctx->{"shared_values"};} >= 1024) 0 7 7
754 0 $ctx->{shared_values} = [];
755 0 $ctx->{value_to_idx} = {};
756 }
757  
758 7 my $idx = scalar @{$ctx->{shared_values}};
759 7 $ctx->{value_to_idx}{$cache_key} = $idx;
760 7 push @{$ctx->{shared_values}}, $text;
761 }
762  
763 9 my $bytes = $text;
764 9 my $is_utf8 = utf8::is_utf8 $bytes;
765 9 ▶ utf8::encode( $bytes ) if $is_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $is_utf8 0 9 9
766 9 return _encode_short_or_long_text( $bytes, $is_utf8 );
767 }
768  
769 14 ▶sub encode_smile {
Subroutine encode_smile: called CC=7 SCAR=19.5
Pod documented
770 14 my ( $data, $opts ) = @_;
771 14 ▶ die 'encode_smile expects at most 2 arguments' if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 14 14
772 14 $opts = _check_opts( 'encode_smile', $opts, { write_header => 1, shared_values => 1, shared_names => 1, canonical => 1 } );
773  
774 12 ▶ my $write_header = exists $opts->{write_header} ? !!$opts->{write_header} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'write_header'} ? : 3 9 12
775 12 ▶ my $shared_values = exists $opts->{shared_values} ? !!$opts->{shared_values} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'shared_values'} ? : 2 10 12
776 12 ▶ my $shared_names = exists $opts->{shared_names} ? !!$opts->{shared_names} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'shared_names'} ? : 1 11 12
777 12 ▶ my $canonical = exists $opts->{canonical} ? !!$opts->{canonical} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'canonical'} ? : 1 11 12
778  
779 12 my %ctx = (
780 shared_names => [],
781 name_to_idx => {},
782 shared_values => [],
783 value_to_idx => {},
784 enable_shared_names => !!$shared_names,
785 enable_shared_values => !!$shared_values,
786 canonical => $canonical,
787 );
788  
789 12 my $payload = _encode_value_smile( $data, \%ctx );
790 12 ▶ my $out = $write_header
Branch: 1 branch
BranchTrueFalseTotal
$write_header ? : 9 3 12
791 ? substr( HEADER, 0, 3 ) . chr( $shared_names | ( $shared_values << 1 ) )
792 : '';
793 12 $out .= $payload;
794 12 return $out;
795 }
796  
797 32 ▶sub decode_smile {
Subroutine decode_smile: called CC=22 SCAR=40.3
Pod documented
798 32 my ( $bytes, $opts ) = @_;
799 32 ▶ die 'decode_smile expects at most 2 arguments' if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 32 32
800 32 $opts = _check_opts( 'decode_smile', $opts, { use_bigint => 1, require_header => 1, json_bool => 1 } );
801 31 ▶ my $use_bigint = exists $opts->{use_bigint} ? !!$opts->{use_bigint} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'use_bigint'} ? : 2 29 31
802 31 ▶ my $require_header = exists $opts->{require_header} ? !!$opts->{require_header} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'require_header'} ? : 2 29 31
803 31 ▶ my $json_bool = exists $opts->{json_bool} ? !!$opts->{json_bool} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'json_bool'} ? : 0 31 31
804  
805 31 my $pos = 0;
806 31 my $has_header = 0;
807 31 ▶ if ( substr( $bytes, 0, 3 ) eq "\x3A\x29\x0A" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($bytes, 0, 3) eq ":)\n") { } 27 4 31
elsif ($require_header) { } 1 3 4
808 27 $pos = 4;
809 27 $has_header = 1;
810 }
811 elsif ( $require_header ) {
812 1 die 'Smile header required';
813 }
814  
815 30 ▶ if ( $pos < length $bytes and substr( $bytes, $pos, 1 ) eq "\x7A" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $bytes and substr($bytes, $pos, 1) eq "z") 0 30 30
Condition33%1/3
$pos < length $bytes and substr($bytes, $pos, 1) eq "z"
!ll&&!rl&&r
0300
MC/DC0%0/2
$pos < length $bytes and substr($bytes, $pos, 1) eq "z"
$pos < length $bytes substr($bytes, $pos, 1) eq "z"
Truth table33%1/3
A and B
A= $pos < length $bytesB= substr($bytes, $pos, 1) eq "z"
ABresult
0X0
100
111
816 0 $pos++;
817 0 ▶ die 'Unexpected EOF' if $pos >= length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= length $bytes 0 0 0
818 0 my $flags = ord substr( $bytes, $pos++, 1 );
819 0 ▶ $pos++ if $flags & 0x01;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 1 0 0 0
820 0 ▶ $pos++ if $flags & 0x02;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 0 0 0
821  
822 0 my $len = _unpack_u32( \$bytes, \$pos );
823 0 ▶ die 'Unexpected EOF' if $pos + $len > length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $len > length $bytes 0 0 0
824 0 my $payload = substr( $bytes, $pos, $len );
825 0 $pos += $len;
826  
827 0 my $inner = 0;
828 0 my $value = _decode_value( \$payload, \$inner, $json_bool, $use_bigint );
829 0 return $value;
830 }
831  
832 30 ▶ my $flags = $has_header ? ord substr( $bytes, 3, 1 ) : 0x01;
Branch: 1 branch
BranchTrueFalseTotal
$has_header ? : 27 3 30
833 30 ▶ my %ctx = (
Branch: 2 branches
BranchTrueFalseTotal
$has_header ? : 27 3 30
$has_header ? : 27 3 30
834 shared_names => [],
835 shared_values => [],
836 enable_shared_names => $has_header ? !!($flags & 0x01) : !!1,
837 enable_shared_values => $has_header ? !!($flags & 0x02) : !!0,
838 );
839  
840 30 my $value = _decode_value_smile( \$bytes, \$pos, \%ctx, $json_bool, $use_bigint );
841 30 ▶ while ( $pos < length $bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $bytes) 2 30 32
842 2 my $rest = ord substr( $bytes, $pos, 1 );
843 2 ▶ last if $rest == 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $rest == 255 0 2 2
844 2 ▶ if ( $rest == 0x0A or $rest == 0x0D or $rest == 0x09 or $rest == 0x20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest == 10 or $rest == 13 or $rest == 9 or $rest == 32) 2 0 2
Condition33%3/9
$rest == 10 or $rest == 13 or $rest == 9 or $rest == 32
l!l&&r!l&&!r
200
$rest == 10 or $rest == 13 or $rest == 9
l!l&&r!l&&!r
200
$rest == 10 or $rest == 13
l!l&&r!l&&!r
200
MC/DC0%0/4
$rest == 10 or $rest == 13 or $rest == 9 or $rest == 32
$rest == 10 $rest == 13 $rest == 9 $rest == 32
Truth table20%1/5
A or B or C or D
A= $rest == 10B= $rest == 13C= $rest == 9D= $rest == 32
ABCDresult
1XXX1
01XX1
001X1
00011
00000
845 2 $pos++;
846 2 next;
847 }
848 0 die 'Invalid payload';
849 }
850 30 return $value;
851 }
852  
853 24 ▶sub _is_pathish_filename {
Subroutine _is_pathish_filename: called CC=4 SCAR=13.9
854 24 my ( $x ) = @_;
855 24 ▶ return 0 if not ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 5 19 24
856 19 ▶ return 1 if eval { $x->isa('Path::Tiny') } or eval { $x->isa('Path::Class::File') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $x->isa("Path::Tiny") } } or eval { do { $x->isa("Path::Class::File") } } 18 1 19
Condition66%2/3
eval { do { $x->isa("Path::Tiny") } } or eval { do { $x->isa("Path::Class::File") } }
l!l&&r!l&&!r
1801
MC/DC50%1/2
eval { do { $x->isa("Path::Tiny") } } or eval { do { $x->isa("Path::Class::File") } }
eval { do { $x->isa("Path::Tiny") } } eval { do { $x->isa("Path::Class::File") } }
Truth table66%2/3
A or B
A= eval { do { $x->isa("Path::Tiny") } }B= eval { do { $x->isa("Path::Class::File") } }
ABresult
1X1
011
000
857 1 return 0;
858 }
859  
860 19 ▶sub _open_for_read {
Subroutine _open_for_read: called CC=7 SCAR=29.1
861 19 my ( $file ) = @_;
862  
863 19 ▶ if ( _is_pathish_filename( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_pathish_filename($file)) 17 2 19
864 17 $file = "$file";
865 }
866  
867 19 ▶ if ( not ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 19 0 19
868 19 ▶ open my $fh, '<:raw', $file or die "open($file) for read: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "open($file) for read: $!" 19 0 19
869 19 return ( $fh, 1 );
870 }
871  
872 0 ▶ if ( eval { $file->can('read') } or eval { $file->can('getline') } or ref( $file ) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $file->can("read") } } or eval { do { $file->can("getline") } } or ref $file eq "GLOB") 0 0 0
Condition0%0/6
eval { do { $file->can("read") } } or eval { do { $file->can("getline") } } or ref $file eq "GLOB"
l!l&&r!l&&!r
000
eval { do { $file->can("read") } } or eval { do { $file->can("getline") } }
l!l&&r!l&&!r
000
MC/DC0%0/3
eval { do { $file->can("read") } } or eval { do { $file->can("getline") } } or ref $file eq "GLOB"
eval { do { $file->can("read") } } eval { do { $file->can("getline") } } ref $file eq "GLOB"
Truth table0%0/4
A or B or C
A= eval { do { $file->can("read") } }B= eval { do { $file->can("getline") } }C= ref $file eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
873 0 binmode( $file, ':raw' );
874 0 return ( $file, 0 );
875 }
876  
877 0 die "Unsupported file argument for read";
878 }
879  
880 5 ▶sub _open_for_write {
Subroutine _open_for_write: called CC=7 SCAR=21.3
881 5 my ( $file ) = @_;
882  
883 5 ▶ if ( _is_pathish_filename( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_pathish_filename($file)) 1 4 5
884 1 $file = "$file";
885 }
886  
887 5 ▶ if ( not ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 4 1 5
888 4 ▶ open my $fh, '>:raw', $file or die "open($file) for write: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $file or die "open($file) for write: $!" 4 0 4
889 4 return ( $fh, 1 );
890 }
891  
892 1 ▶ if ( eval { $file->can('print') } or eval { $file->can('write') } or ref( $file ) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $file->can("print") } } or eval { do { $file->can("write") } } or ref $file eq "GLOB") 1 0 1
Condition33%2/6
eval { do { $file->can("print") } } or eval { do { $file->can("write") } } or ref $file eq "GLOB"
l!l&&r!l&&!r
100
eval { do { $file->can("print") } } or eval { do { $file->can("write") } }
l!l&&r!l&&!r
100
MC/DC0%0/3
eval { do { $file->can("print") } } or eval { do { $file->can("write") } } or ref $file eq "GLOB"
eval { do { $file->can("print") } } eval { do { $file->can("write") } } ref $file eq "GLOB"
Truth table25%1/4
A or B or C
A= eval { do { $file->can("print") } }B= eval { do { $file->can("write") } }C= ref $file eq "GLOB"
ABCresult
1XX1
01X1
0011
0000
893 1 binmode( $file, ':raw' );
894 1 return ( $file, 0 );
895 }
896  
897 0 die "Unsupported file argument for write";
898 }
899  
900 5 ▶sub dump_smile {
Subroutine dump_smile: called CC=10 SCAR=24.0
Pod documented
901 5 my ( $file, $data, $opts ) = @_;
902 5 ▶ die 'dump_smile expects at most 3 arguments' if @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 3 0 5 5
903  
904 5 ▶ if ( @_ >= 3 and defined $opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 3 and defined $opts) 2 3 5
Condition66%2/3
@_ >= 3 and defined $opts
!ll&&!rl&&r
302
MC/DC50%1/2
@_ >= 3 and defined $opts
@_ >= 3 defined $opts
Truth table66%2/3
A and B
A= @_ >= 3B= defined $opts
ABresult
0X0
100
111
905 2 ▶ die 'Options must be a hash reference' if not _is_hashref( $opts );
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($opts) 0 2 2
906 }
907  
908 5 my ( $fh, $close ) = _open_for_write( $file );
909  
910 5 my $bytes = encode_smile( $data, $opts );
911 4 ▶ my $ok = eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { print {$fh;} $bytes or die "write: $!"; 1 } } ? : 4 0 4
912 4 ▶ print {$fh} $bytes or die "write: $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $bytes or die "write: $!" 4 0 4
913 4 1;
914 } ? 1 : 0;
915  
916 4 ▶ if ( $close ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close) 3 1 4
917 3 ▶ close $fh or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or $ok = 0 3 0 3
918 }
919  
920 4 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 4 0 4
921 }
922  
923 19 ▶sub load_smile {
Subroutine load_smile: called CC=8 SCAR=21.6
Pod documented
924 19 my ( $file, $opts ) = @_;
925 19 ▶ die 'load_smile expects at most 2 arguments' if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 19 19
926  
927 19 ▶ if ( @_ >= 2 and defined $opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2 and defined $opts) 1 18 19
Condition66%2/3
@_ >= 2 and defined $opts
!ll&&!rl&&r
1801
MC/DC50%1/2
@_ >= 2 and defined $opts
@_ >= 2 defined $opts
Truth table66%2/3
A and B
A= @_ >= 2B= defined $opts
ABresult
0X0
100
111
928 1 ▶ die 'Options must be a hash reference' if not _is_hashref( $opts );
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($opts) 0 1 1
929 }
930  
931 19 my ( $fh, $close ) = _open_for_read( $file );
932  
933 19 my $buf = do { local $/; <$fh> };
934 19 ▶ die "read failed" if not defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 19 19
935  
936 19 ▶ if ( $close ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close) 19 0 19
937 19 ▶ close $fh or die "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close: $!" 19 0 19
938 }
939  
940 19 return decode_smile( $buf, $opts );
941 }
942  
943 9 1;
944  
945 __END__
↑ Summary Smile.pm »