Bifcode.pm

filter: Statementstmt 88.6% 187 / 211 Branchbran 82.5% 132 / 160 Conditioncond 71.0% 54 / 76 MC/DCmcdc 41.3% 19 / 46 Subroutinesub 46.5% 60 / 129 Podpod 66.6% 4 / 6 totaltotal 72.6% 456 / 628 CC 109 SCAR 25.0
CC
109
Cov
83%
_decode_bifcode_chunk
45.3
_encode_bifcode
34.2
__ANON__
22.3
CRAP 12.2
↑ Summary Bifcode/V1.pm »
1 package Bifcode;
2 7 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use boolean ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Exporter::Tidy all => [
Subroutine BEGIN: called CC=1 SCAR=0.0
7 qw( encode_bifcode
8 decode_bifcode
9 force_bifcode
10 diff_bifcode)
11 6 ];
12  
13 # ABSTRACT: Serialisation similar to Bencode + undef/UTF8
14  
15 4 our $VERSION = 'v2.0.16';
16 4 our $max_depth;
17 4 our @CARP_NOT = (__PACKAGE__);
18  
19 78 ▶sub _croak {
Subroutine _croak: called CC=6 SCAR=20.5
20 78 require Carp;
21 78 ▶ my $type = shift // Carp::croak('usage: _croak($TYPE, [$msg])');
Condition50%1/2
shift() // Carp::croak("usage: _croak(\$TYPE, [\$msg])")
l!l
760
MC/DC0%0/1
shift() // Carp::croak("usage: _croak(\$TYPE, [\$msg])")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
22 78 my %messages = (
23 Decode => 'garbage at',
24 DecodeBifcodeTerm => 'missing BIFCODE terminator at',
25 DecodeBytes => 'malformed BYTES length at',
26 DecodeBytesTrunc => 'unexpected BYTES end of data at',
27 DecodeBytesTerm => 'missing BYTES termination at',
28 DecodeDepth => 'nesting depth exceeded at',
29 DecodeTrunc => 'unexpected end of data at',
30 DecodeReal => 'malformed REAL data at',
31 DecodeRealTrunc => 'unexpected REAL end of data at',
32 DecodeInteger => 'malformed INTEGER data at',
33 DecodeIntegerTrunc => 'unexpected INTEGER end of data at',
34 DecodeTrailing => 'trailing garbage at',
35 DecodeUTF8 => 'malformed UTF8 string length at',
36 DecodeUTF8Trunc => 'unexpected UTF8 end of data at',
37 DecodeUTF8Term => 'missing UTF8 termination at',
38 DecodeUsage => undef,
39 DiffUsage => 'usage: diff_bifcode($b1, $b2, [$diff_args])',
40 EncodeBytesUndef => 'Bifcode::BYTES ref is undefined',
41 EncodeReal => undef,
42 EncodeRealUndef => 'Bifcode::REAL ref is undefined',
43 EncodeInteger => undef,
44 EncodeIntegerUndef => 'Bifcode::INTEGER ref is undefined',
45 DecodeKeyType => 'dict key is not BYTES or UTF8 at',
46 DecodeKeyDuplicate => 'duplicate dict key at',
47 DecodeKeyOrder => 'dict key not in sort order at',
48 DecodeKeyValue => 'dict key is missing value at',
49 EncodeUTF8Undef => 'Bifcode::UTF8 ref is undefined',
50 EncodeUnhandled => undef,
51 EncodeUsage => 'usage: encode_bifcode($arg)',
52 ForceUsage => 'ref and type must be defined',
53 );
54  
55 78 my $err = 'Bifcode::Error::' . $type;
56 78 ▶ my $msg = shift // $messages{$type}
Condition60%3/5
(shift() // $messages{$type}) // Carp::croak("Bifcode::_croak($type) has no message ")
l!l
760
shift() // $messages{$type}
l!l&&r!l&&!r
12640
MC/DC0%0/2
(shift() // $messages{$type}) // Carp::croak("Bifcode::_croak($type) has no message ")
shift() $messages{$type}
Truth table66%2/3
A // B
A= shift()B= $messages{$type}
ABresult
1X1
011
000
57 // Carp::croak("Bifcode::_croak($type) has no message ");
58 78 my $short = Carp::shortmess('');
59  
60 78 ▶ $msg =~ s! at$!' at input byte '. ( pos() // 0 )!e;
Condition100%2/2
pos $_ // 0
l!l
504
MC/DC100%1/1
pos $_ // 0
pos $_
Truth table100%2/2
A
A= pos $_
Aresult
11
00
61  
62 0 ▶ eval qq[
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine __ANON__: called CC=1 SCAR=1.9
Subroutine __ANON__: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine BEGIN: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
Subroutine __ANON__: not called CC=1 SCAR=1.9
63 package $err;
64 use overload
65 bool => sub { 1 },
66 '""' => sub { \${ \$_[0] } . ' (' . ( ref \$_[0] ) . ')$short' },
67 fallback => 1;
68 1;
69 ];
70  
71 78 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 76 76
72 78 die bless \$msg, $err;
73 }
74  
75 4 my $chunk = qr/ \G (?|
76 (~,)
77 | (f,)
78 | (t,)
79 | (N,)
80 | (-,)
81 | (\+,)
82 | (B|b|u) (?: ( 0 | [1-9] [0-9]* ) \. )?
83 | (i) (?: ( 0 | -? [1-9] [0-9]* ) , )?
84 | (r) (?: ( 0 | -? [1-9] [0-9]* )
85 \. ( 0 | [0-9]* [1-9] )
86 e ( (?: 0 | -? [1-9] ) [0-9]* ) ,
87 )?
88 | (\[)
89 | (\{)
90 ) /x;
91  
92 35 ▶sub _decode_bifcode_key {
Subroutine _decode_bifcode_key: called CC=11 SCAR=25.9
93  
94 34 ▶ unless (m/ \G (b|u) (?: ( 0 | [1-9] [0-9]* ) \. )? /gcx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \G (b|u) (?: ( 0 | [1-9] [0-9]* ) \. )? /cgx) 4 29 33
95 5 ▶ _croak m/ \G \z /xgc ? 'DecodeTrunc' : 'DecodeKeyType';
Branch: 1 branch
BranchTrueFalseTotal
/ \G \z /cgx ? : 0 4 4
96 }
97  
98 30 ▶ if ( $1 eq 'b' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq 'b') { } 1 28 29
elsif ($1 eq 'u') { } 28 0 28
99 2 ▶ my $len = $2 // _croak 'DecodeBytes';
Condition33%1/3
$2 // _croak("DecodeBytes")
l!l&&r!l&&!r
100
MC/DC0%0/2
$2 // _croak("DecodeBytes")
$2 _croak("DecodeBytes")
Truth table33%1/3
A // B
A= $2B= _croak("DecodeBytes")
ABresult
1X1
011
000
100 2 ▶ _croak 'DecodeBytesTrunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 0 1 1
101  
102 2 my $data = substr $_, pos(), $len;
103 2 pos() = pos() + $len;
104  
105 2 ▶ _croak 'DecodeBytesTerm' unless m/ \G : /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G : /cgx 0 1 1
106 2 $data;
107 }
108 elsif ( $1 eq 'u' ) {
109 29 ▶ my $len = $2 // _croak 'DecodeUTF8';
Condition33%1/3
$2 // _croak("DecodeUTF8")
l!l&&r!l&&!r
2800
MC/DC0%0/2
$2 // _croak("DecodeUTF8")
$2 _croak("DecodeUTF8")
Truth table33%1/3
A // B
A= $2B= _croak("DecodeUTF8")
ABresult
1X1
011
000
110 29 ▶ _croak 'DecodeUTF8Trunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 0 28 28
111  
112 29 utf8::decode( my $str = substr $_, pos(), $len );
113 29 pos() = pos() + $len;
114  
115 29 ▶ _croak 'DecodeUTF8Term' unless m/ \G : /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G : /cgx 0 28 28
116 29 $str;
117 }
118 }
119  
120 158 ▶sub _decode_bifcode_chunk {
Subroutine _decode_bifcode_chunk: called CC=44 SCAR=38.1
121 158 ▶ local $max_depth = $max_depth - 1 if defined $max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bifcode::max_depth 33 124 157
122  
123 158 ▶ unless (m/$chunk/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/$chunk/cg) 8 149 157
124 9 ▶ _croak m/ \G \z /xgc ? 'DecodeTrunc' : 'Decode';
Branch: 1 branch
BranchTrueFalseTotal
/ \G \z /cgx ? : 4 4 8
125 }
126  
127 150 ▶ if ( $1 eq '~,' ) {
Branch: 13 branches
BranchTrueFalseTotal
if ($1 eq '~,') { } 8 141 149
elsif ($1 eq 'f,') { } 3 138 141
elsif ($1 eq 't,') { } 3 135 138
elsif ($1 eq 'N,') { } 1 134 135
elsif ($1 eq '-,') { } 1 133 134
elsif ($1 eq '+,') { } 1 132 133
elsif ($1 eq 'b') { } 6 126 132
elsif ($1 eq 'u') { } 40 86 126
elsif ($1 eq 'i') { } 20 66 86
elsif ($1 eq 'r') { } 11 55 66
elsif ($1 eq '[') { } 26 29 55
elsif ($1 eq '{') { } 29 0 29
elsif ($1 eq 'B') { } 0 0 0
128 9 undef;
129 }
130 elsif ( $1 eq 'f,' ) {
131 4 boolean::false;
132 }
133 elsif ( $1 eq 't,' ) {
134 4 boolean::true;
135 }
136 elsif ( $1 eq 'N,' ) {
137 2 require Math::BigInt;
138 2 Math::BigInt->bnan;
139 }
140 elsif ( $1 eq '-,' ) {
141 2 require Math::BigInt;
142 2 Math::BigInt->binf('-');
143 }
144 elsif ( $1 eq '+,' ) {
145 2 require Math::BigInt;
146 2 Math::BigInt->binf('+');
147 }
148 elsif ( $1 eq 'b' ) {
149 7 ▶ my $len = $2 // _croak 'DecodeBytes';
Condition66%2/3
$2 // _croak("DecodeBytes")
l!l&&r!l&&!r
420
MC/DC0%0/2
$2 // _croak("DecodeBytes")
$2 _croak("DecodeBytes")
Truth table33%1/3
A // B
A= $2B= _croak("DecodeBytes")
ABresult
1X1
011
000
150 5 ▶ _croak 'DecodeBytesTrunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 1 3 4
151  
152 4 my $data = substr $_, pos(), $len;
153 4 pos() = pos() + $len;
154  
155 4 ▶ _croak 'DecodeBytesTerm' unless m/ \G , /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G , /cgx 1 2 3
156 3 $data;
157 }
158 elsif ( $1 eq 'u' ) {
159 41 ▶ my $len = $2 // _croak 'DecodeUTF8';
Condition66%2/3
$2 // _croak("DecodeUTF8")
l!l&&r!l&&!r
3550
MC/DC0%0/2
$2 // _croak("DecodeUTF8")
$2 _croak("DecodeUTF8")
Truth table33%1/3
A // B
A= $2B= _croak("DecodeUTF8")
ABresult
1X1
011
000
160 36 ▶ _croak 'DecodeUTF8Trunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 2 33 35
161  
162 34 utf8::decode( my $str = substr $_, pos(), $len );
163 34 pos() = pos() + $len;
164  
165 34 ▶ _croak 'DecodeUTF8Term' unless m/ \G , /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G , /cgx 2 31 33
166 32 $str;
167 }
168 elsif ( $1 eq 'i' ) {
169 21 ▶ if ( not defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $2) 8 12 20
170 9 ▶ _croak 'DecodeIntegerTrunc' if m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \z /cgx 1 7 8
171 8 _croak 'DecodeInteger';
172 }
173 13 0 + $2;
174 }
175 elsif ( $1 eq 'r' ) {
176 12 ▶ if ( !defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $2) 8 3 11
177 9 ▶ _croak 'DecodeRealTrunc' if m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \z /cgx 1 7 8
178 8 _croak 'DecodeReal';
179 }
180 4 ▶ _croak 'DecodeReal'
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq 0 and $3 eq 0 and $4 ne 0 1 2 3
Condition83%5/6
$2 eq 0 and $3 eq 0 and $4 ne 0
!ll&&!rl&&r
111
$2 eq 0 and $3 eq 0
!ll&&!rl&&r
102
MC/DC66%2/3
$2 eq 0 and $3 eq 0 and $4 ne 0
$2 eq 0 $3 eq 0 $4 ne 0
Truth table75%3/4
A and B and C
A= $2 eq 0B= $3 eq 0C= $4 ne 0
ABCresult
0XX0
10X0
1100
1111
181 if $2 eq '0' # mantissa 0.
182 and $3 eq '0' # mantissa 0.0
183 and $4 ne '0'; # sign or exponent 0.0e0
184  
185 3 0.0 + ( $2 . '.' . $3 . 'e' . $4 );
186 }
187 elsif ( $1 eq '[' ) {
188 27 ▶ _croak 'DecodeDepth' if defined $max_depth and $max_depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bifcode::max_depth and $Bifcode::max_depth < 0 4 22 26
Condition100%3/3
defined $Bifcode::max_depth and $Bifcode::max_depth < 0
!ll&&!rl&&r
1484
MC/DC100%2/2
defined $Bifcode::max_depth and $Bifcode::max_depth < 0
defined $Bifcode::max_depth $Bifcode::max_depth < 0
Truth table100%3/3
A and B
A= defined $Bifcode::max_depthB= $Bifcode::max_depth < 0
ABresult
0X0
100
111
189  
190 23 my @list;
191 23 ▶ until (m/ \G \] /xgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \G \] /cgx) 35 16 51
192 36 push @list, _decode_bifcode_chunk();
193 }
194 17 \@list;
195 }
196 elsif ( $1 eq '{' ) {
197 30 ▶ _croak 'DecodeDepth' if defined $max_depth and $max_depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bifcode::max_depth and $Bifcode::max_depth < 0 3 26 29
Condition100%3/3
defined $Bifcode::max_depth and $Bifcode::max_depth < 0
!ll&&!rl&&r
1793
MC/DC100%2/2
defined $Bifcode::max_depth and $Bifcode::max_depth < 0
defined $Bifcode::max_depth $Bifcode::max_depth < 0
Truth table100%3/3
A and B
A= defined $Bifcode::max_depthB= $Bifcode::max_depth < 0
ABresult
0X0
100
111
198  
199 27 my $last_key;
200 my %hash;
201 27 ▶ until (m/ \G \} /xgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \G \} /cgx) 35 13 48
202 36 ▶ _croak 'DecodeTrunc' if m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \z /cgx 2 33 35
203  
204 34 my $key = _decode_bifcode_key();
205  
206 30 ▶ _croak 'DecodeKeyDuplicate' if exists $hash{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{$key} 1 28 29
207 29 ▶ _croak 'DecodeKeyOrder'
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_key and $key lt $last_key 1 27 28
Condition100%3/3
defined $last_key and $key lt $last_key
!ll&&!rl&&r
1981
MC/DC100%2/2
defined $last_key and $key lt $last_key
defined $last_key $key lt $last_key
Truth table100%3/3
A and B
A= defined $last_keyB= $key lt $last_key
ABresult
0X0
100
111
208 if defined $last_key and $key lt $last_key;
209 28 ▶ _croak 'DecodeKeyValue' if m/ \G \} /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \} /cgx 1 26 27
210  
211 27 $last_key = $key;
212 27 $hash{$key} = _decode_bifcode_chunk();
213 }
214 14 \%hash;
215 }
216 elsif ( $1 eq 'B' ) {
217 1 ▶ my $len = $2 // _croak 'DecodeBifcode';
Condition0%0/3
$2 // _croak("DecodeBifcode")
l!l&&r!l&&!r
000
MC/DC0%0/2
$2 // _croak("DecodeBifcode")
$2 _croak("DecodeBifcode")
Truth table0%0/3
A // B
A= $2B= _croak("DecodeBifcode")
ABresult
1X1
011
000
218 1 ▶ _croak 'DecodeBifcodeTrunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 0 0 0
219  
220 1 my $res = _decode_bifcode_chunk();
221 1 ▶ _croak 'DecodeBifcodeTerm' unless m/ \G , /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G , /cgx 0 0 0
222  
223 1 $res;
224 }
225 }
226  
227 101 ▶sub decode_bifcode {
Subroutine decode_bifcode: called CC=5 SCAR=16.1
Pod documented
228 101 local $_ = shift;
229 101 local $max_depth = shift;
230  
231 101 ▶ _croak 'DecodeUsage', 'decode_bifcode: too many arguments' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 99 100
232 100 ▶ _croak 'DecodeUsage', 'decode_bifcode: input undefined'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 2 97 99
233 unless defined $_;
234 98 ▶ _croak 'DecodeUsage', 'decode_bifcode: only accepts bytes'
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($_) 1 96 97
235 if utf8::is_utf8($_);
236  
237 97 my $deserialised_data = _decode_bifcode_chunk();
238 43 ▶ _croak 'DecodeTrailing', " For: $_" if $_ !~ m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ / \G \z /cgx 4 38 42
239 39 return $deserialised_data;
240 }
241  
242 4 my $number_qr = qr/\A ( 0 | -? [1-9] [0-9]* )
243 ( \. ( [0-9]+? ) 0* )?
244 ( e ( -? [0-9]+ ) )? \z/xi;
245  
246 90 ▶sub _encode_bifcode {
Subroutine _encode_bifcode: called CC=34 SCAR=35.3
247 map {
248 90 ▶ if ( !defined $_ ) {
Branch: 12 branches
BranchTrueFalseTotal
if (not defined $_) { } 4 92 96
elsif ((my $ref = ref $_) eq '') { } 48 44 92
elsif ($ref eq 'ARRAY') { } 5 39 44
elsif ($ref eq 'HASH') { } 7 32 39
elsif ($ref eq 'SCALAR' or $ref eq 'Bifcode::BYTES') { } 6 26 32
elsif (&boolean::isBoolean($_)) { } 4 22 26
elsif ($ref eq 'Bifcode::INTEGER') { } 4 18 22
elsif ($ref eq 'Bifcode::REAL') { } 9 9 18
elsif ($ref eq 'Bifcode::UTF8') { } 3 6 9
elsif (eval { do { $_->is_nan } }) { } 1 5 6
elsif (eval { do { $_->is_inf } }) { } 2 3 5
elsif (my $a = eval { do { [$_->is_int] } }) { } 2 1 3
249 5 '~' . ',';
250 }
251 elsif ( ( my $ref = ref $_ ) eq '' ) {
252 49 ▶ if ( $_ =~ $number_qr ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_ =~ /$number_qr/) { } 36 12 48
elsif (utf8::is_utf8($_)) { } 3 9 12
elsif ($_ =~ /^[\x{20}-\x{7E}]*$/) { } 9 0 9
253 37 ▶ if ( defined $3 or defined $5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $3 or defined $5) { } 26 10 36
Condition100%3/3
defined $3 or defined $5
l!l&&r!l&&!r
17910
MC/DC100%2/2
defined $3 or defined $5
defined $3 defined $5
Truth table100%3/3
A or B
A= defined $3B= defined $5
ABresult
1X1
011
000
254  
255 # normalize to BIFCODE_REAL standards
256 27 ▶ my $x = 'r' . ( 0 + $1 ) # remove leading zeros
Condition100%4/4
$3 // 0
l!l
179
$5 // 0
l!l
206
MC/DC100%1/1
$3 // 0
$3
MC/DC100%1/1
$5 // 0
$5
Truth table100%2/2
A
A= $3
Aresult
11
00
Truth table100%2/2
A
A= $5
Aresult
11
00
257 . '.' . ( $3 // 0 ) . 'e' . ( 0 + ( $5 // 0 ) ) . ',';
258 27 $x =~ s/ ([1-9]) (0+ e)/.${1}e/x; # remove trailing zeros
259 27 $x;
260 }
261 else {
262 11 'i' . $_ . ',';
263 }
264 }
265 elsif ( utf8::is_utf8($_) ) {
266 4 utf8::encode( my $str = $_ );
267 4 'u' . length($str) . '.' . $str . ',';
268 }
269 elsif ( $_ =~ m/^[\x{20}-\x{7E}]*$/ ) {
270 10 'u' . length($_) . '.' . $_ . ',';
271 }
272 else {
273 1 'b' . length($_) . '.' . $_ . ',';
274 }
275 }
276 elsif ( $ref eq 'ARRAY' ) {
277 6 '[' . join( '', map _encode_bifcode($_), @$_ ) . ']';
278 }
279 elsif ( $ref eq 'HASH' ) {
280 '{' . join(
281 '',
282 8 do {
283 8 my $k;
284 8 my @k = sort keys %$_;
285  
286 map {
287 15 $k = shift @k;
288  
289 15 ▶ if ( $k =~ m/^[\x{20}-\x{7E}]*$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ /^[\x{20}-\x{7E}]*$/) { } 13 1 14
elsif (utf8::is_utf8($k)) { } 1 0 1
290 14 ( 'u' . length($k) . '.' . $k . ':', $_ );
291 }
292 elsif ( utf8::is_utf8($k) ) {
293 2 utf8::encode($k);
294 2 ( 'u' . length($k) . '.' . $k . ':', $_ );
295 }
296 else {
297 1 ( 'b' . length($k) . '.' . $k . ':', $_ );
298 }
299 8 } _encode_bifcode( @$_{@k} );
300 }
301 ) . '}';
302 }
303 ▶ elsif ( $ref eq 'SCALAR' or $ref eq 'Bifcode::BYTES' ) {
Condition100%3/3
$ref eq "SCALAR" or $ref eq "Bifcode::BYTES"
l!l&&r!l&&!r
2426
MC/DC100%2/2
$ref eq "SCALAR" or $ref eq "Bifcode::BYTES"
$ref eq "SCALAR" $ref eq "Bifcode::BYTES"
Truth table100%3/3
A or B
A= $ref eq "SCALAR"B= $ref eq "Bifcode::BYTES"
ABresult
1X1
011
000
304 7 ▶ $$_ // _croak 'EncodeBytesUndef';
Condition66%2/3
$$_ // _croak("EncodeBytesUndef")
l!l&&r!l&&!r
420
MC/DC0%0/2
$$_ // _croak("EncodeBytesUndef")
$$_ _croak("EncodeBytesUndef")
Truth table66%2/3
A // B
A= $$_B= _croak("EncodeBytesUndef")
ABresult
1X1
011
000
305 5 'b' . length($$_) . '.' . $$_ . ',';
306 }
307 elsif ( boolean::isBoolean($_) ) {
308 5 ▶ ( $_ ? 't' : 'f' ) . ',';
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 2 2 4
309 }
310 elsif ( $ref eq 'Bifcode::INTEGER' ) {
311 5 ▶ $$_ // _croak 'EncodeIntegerUndef';
Condition66%2/3
$$_ // _croak("EncodeIntegerUndef")
l!l&&r!l&&!r
310
MC/DC0%0/2
$$_ // _croak("EncodeIntegerUndef")
$$_ _croak("EncodeIntegerUndef")
Truth table66%2/3
A // B
A= $$_B= _croak("EncodeIntegerUndef")
ABresult
1X1
011
000
312 4 ▶ _croak 'EncodeInteger', 'invalid integer: ' . $$_
Branch: 1 branch
BranchTrueFalseTotal
unless $$_ =~ /\A (?: 0 | -? [1-9] [0-9]* ) \z/x 2 1 3
313 unless $$_ =~ m/\A (?: 0 | -? [1-9] [0-9]* ) \z/x;
314 2 sprintf 'i%s,', $$_;
315 }
316 elsif ( $ref eq 'Bifcode::REAL' ) {
317 10 ▶ $$_ // _croak 'EncodeRealUndef';
Condition66%2/3
$$_ // _croak("EncodeRealUndef")
l!l&&r!l&&!r
810
MC/DC0%0/2
$$_ // _croak("EncodeRealUndef")
$$_ _croak("EncodeRealUndef")
Truth table66%2/3
A // B
A= $$_B= _croak("EncodeRealUndef")
ABresult
1X1
011
000
318 9 ▶ _croak 'EncodeReal', 'invalid real: ' . $$_
Branch: 1 branch
BranchTrueFalseTotal
unless $$_ =~ /$number_qr/ 1 7 8
319 unless $$_ =~ $number_qr;
320  
321 8 ▶ my $x = 'r' . ( 0 + $1 ) # remove leading zeros
Condition100%4/4
$3 // 0
l!l
52
$5 // 0
l!l
34
MC/DC100%1/1
$3 // 0
$3
MC/DC100%1/1
$5 // 0
$5
Truth table100%2/2
A
A= $3
Aresult
11
00
Truth table100%2/2
A
A= $5
Aresult
11
00
322 . '.' . ( $3 // 0 ) . 'e' . ( 0 + ( $5 // 0 ) ) . ',';
323 8 $x =~ s/ ([1-9]) (0+ e)/.${1}e/x; # remove trailing zeros
324 8 $x;
325 }
326 elsif ( $ref eq 'Bifcode::UTF8' ) {
327 4 ▶ my $str = $$_ // _croak 'EncodeUTF8Undef';
Condition66%2/3
$$_ // _croak("EncodeUTF8Undef")
l!l&&r!l&&!r
201
MC/DC0%0/2
$$_ // _croak("EncodeUTF8Undef")
$$_ _croak("EncodeUTF8Undef")
Truth table33%1/3
A // B
A= $$_B= _croak("EncodeUTF8Undef")
ABresult
1X1
011
000
328 3 utf8::encode($str);
329 3 'u' . length($str) . '.' . $str . ',';
330 }
331 7 elsif ( eval { $_->is_nan } ) {
332 2 'N,';
333 }
334 6 elsif ( eval { $_->is_inf } ) {
335 3 ▶ $_->is_pos ? '+,' : '-,';
Branch: 1 branch
BranchTrueFalseTotal
$_->is_pos ? : 1 1 2
336 }
337 4 elsif ( my $a = eval { [ $_->is_int ] } ) {
338 3 ▶ $a->[0]
Branch: 1 branch
BranchTrueFalseTotal
$a->[0] ? : 1 1 2
339 ? 'i' . $_->bdstr() . ','
340 : 'r' . $_->bnstr() . ',';
341 }
342 else {
343 2 _croak 'EncodeUnhandled', 'unhandled data type: ' . $ref;
344 }
345 } @_;
346 }
347  
348 76 ▶sub encode_bifcode {
Subroutine encode_bifcode: called CC=4 SCAR=14.5
Pod documented
349 76 ▶ if ( ( @_ == 2 ) && pop ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2 and pop()) { } 0 75 75
elsif (@_ == 1) { } 72 3 75
Condition33%1/3
@_ == 2 and pop()
!ll&&!rl&&r
7500
MC/DC0%0/2
@_ == 2 and pop()
@_ == 2 pop()
Truth table33%1/3
A and B
A= @_ == 2B= pop()
ABresult
0X0
100
111
350 1 my $b = (&_encode_bifcode)[0];
351 1 'B' . length($b) . '.' . $b . ',';
352 }
353 elsif ( @_ == 1 ) {
354 73 (&_encode_bifcode)[0];
355 }
356 else {
357 4 _croak 'EncodeUsage';
358 }
359 }
360  
361 17 ▶sub force_bifcode {
Subroutine force_bifcode: called CC=3 SCAR=12.0
Pod documented
362 17 my $ref = shift;
363 17 my $type = shift;
364  
365 17 ▶ _croak 'ForceUsage' unless defined $ref and defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref and defined $type 0 16 16
Condition33%1/3
defined $ref and defined $type
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $ref and defined $type
defined $ref defined $type
Truth table33%1/3
A and B
A= defined $refB= defined $type
ABresult
0X0
100
111
366 17 bless \$ref, 'Bifcode::' . uc($type);
367 }
368  
369 11 ▶sub _expand_bifcode {
Subroutine _expand_bifcode: called CC=1 SCAR=0.0
370 11 my $bifcode = shift;
371 11 $bifcode =~ s/ (
372 [\[\]\{\}]
373 | ~,
374 | (B|u|b) [0-9]+ \.
375 | r -? [0-9]+ \. [0-9]+ e -? [0-9]+ ,
376 | i [0-9]+ ,
377 ) /\n$1/gmx;
378 11 $bifcode =~ s/ \A \n //mx;
379 11 $bifcode . "\n";
380 }
381  
382 8 ▶sub diff_bifcode {
Subroutine diff_bifcode: called CC=4 SCAR=13.9
Pod documented
383 8 ▶ _croak 'DiffUsage' unless @_ >= 2 and @_ <= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 2 and @_ <= 3 2 5 7
Condition100%3/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
115
MC/DC100%2/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table100%3/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
384 6 my $b1 = shift;
385 6 my $b2 = shift;
386 6 ▶ my $diff_args = shift || { STYLE => 'Unified' };
Condition50%1/2
shift() || {'STYLE', 'Unified'}
l!l
05
MC/DC0%0/1
shift() || {'STYLE', 'Unified'}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
387  
388 6 require Text::Diff;
389  
390 6 $b1 = _expand_bifcode($b1);
391 6 $b2 = _expand_bifcode($b2);
392 6 return Text::Diff::diff( \$b1, \$b2, $diff_args );
393 }
394  
395 # Looking for something like "B48." that may be truncated at any point
396 4 my $qr_bcbc = qr/
397 ^
398 ( # 1
399 B
400 (?:
401 \Z | (?:
402 ( 0 | [1-9][0-9]* ) # 2
403 ( \Z | (\.) ) # 3
404 )
405 )
406 )
407 /x;
408  
409 1 ▶sub anyevent_read_type {
Subroutine anyevent_read_type: called CC=1 SCAR=4.4
Pod undocumented
410 1 my ( $self, $cb, $maxdepth ) = @_;
411  
412 1 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=31.0
413 1 ▶ return unless defined $_[0]->{rbuf};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"rbuf"} 0 0 0
414  
415 1 $_[0]->{rbuf} =~ s/^[\r\n]*//;
416 1 ▶ return unless length $_[0]->{rbuf};
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0]{"rbuf"} 0 0 0
417  
418 0 $_[0]->{rbuf} =~ $qr_bcbc;
419  
420 0 ▶ if ( length $3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $3) { } 0 0 0
elsif (not length $1) { } 0 0 0
421 $_[0]->unshift_read(
422 chunk => length($1) + $2 + 1,
423 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=17.9
424 0 my $data = eval { decode_bifcode( $_[1], $maxdepth ) };
425 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
426 0 $_[0]->_error( Errno::EBADMSG(), undef, $@ );
427 }
428 else {
429 0 $cb->( $_[0], $data );
430 }
431 0 1;
432 }
433 0 );
434 0 return 1;
435 }
436 elsif ( not length $1 ) {
437 0 $_[0]->_error( Errno::EBADMSG() );
438 0 return;
439 }
440 else {
441 0 return; # not enough data yet
442 }
443 1 };
444 }
445  
446 1 ▶sub anyevent_write_type {
Subroutine anyevent_write_type: called CC=1 SCAR=6.9
Pod undocumented
447 0 encode_bifcode( $_[1], 1 ) . "\n";
448 }
449  
450 4 1;
451  
452 __END__
↑ Summary Bifcode/V1.pm »