Bifcode/V1.pm

filter: Statementstmt 83.7% 165 / 197 Branchbran 98.0% 98 / 100 Conditioncond 79.6% 51 / 64 MC/DCmcdc 50.0% 19 / 38 Subroutinesub 45.8% 55 / 120 Podpod 100% 4 / 4 totaltotal 74.9% 392 / 523 CC 75 SCAR 22.0
CC
75
Cov
87%
_decode_bifcode_chunk
38.0
_encode_bifcode
24.1
_croak
7.2
CRAP 9.0
« Bifcode.pm ↑ Summary
1 package Bifcode::V1;
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 Carp (qw/croak shortmess/);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Exporter::Tidy all => [
Subroutine BEGIN: called CC=1 SCAR=0.0
8 qw( encode_bifcode
9 decode_bifcode
10 force_bifcode
11 diff_bifcode)
12 6 ];
13  
14 # ABSTRACT: Serialisation similar to Bencode + undef/UTF8
15  
16 5 our $VERSION = 'v2.0.16';
17 5 our $max_depth;
18 5 our @CARP_NOT = (__PACKAGE__);
19  
20 73 ▶sub _croak {
Subroutine _croak: called CC=5 SCAR=19.7
21 73 ▶ my $type = shift // croak 'usage: _error($TYPE, [$msg])';
Condition50%1/2
shift() // croak("usage: _error(\$TYPE, [\$msg])")
l!l
710
MC/DC0%0/1
shift() // croak("usage: _error(\$TYPE, [\$msg])")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
22 73 my %messages = (
23 Decode => 'garbage at',
24 DecodeBytes => 'malformed BYTES length at',
25 DecodeBytesTrunc => 'unexpected BYTES end of data at',
26 DecodeBytesTerm => 'missing BYTES termination at',
27 DecodeDepth => 'nesting depth exceeded at',
28 DecodeTrunc => 'unexpected end of data at',
29 DecodeFloat => 'malformed FLOAT data at',
30 DecodeFloatTrunc => 'unexpected FLOAT end of data at',
31 DecodeInteger => 'malformed INTEGER data at',
32 DecodeIntegerTrunc => 'unexpected INTEGER end of data at',
33 DecodeTrailing => 'trailing garbage at',
34 DecodeUTF8 => 'malformed UTF8 string length at',
35 DecodeUTF8Trunc => 'unexpected UTF8 end of data at',
36 DecodeUTF8Term => 'missing UTF8 termination at',
37 DecodeUsage => undef,
38 DiffUsage => 'usage: diff_bifcode($b1, $b2, [$diff_args])',
39 EncodeBytesUndef => 'Bifcode::BYTES ref is undefined',
40 EncodeFloat => undef,
41 EncodeFloatUndef => 'Bifcode::FLOAT ref is undefined',
42 EncodeInteger => undef,
43 EncodeIntegerUndef => 'Bifcode::INTEGER ref is undefined',
44 DecodeKeyType => 'dict key is not BYTES or UTF8 at',
45 DecodeKeyDuplicate => 'duplicate dict key at',
46 DecodeKeyOrder => 'dict key not in sort order at',
47 DecodeKeyValue => 'dict key is missing value at',
48 EncodeUTF8Undef => 'Bifcode::UTF8 ref is undefined',
49 EncodeUnhandled => undef,
50 EncodeUsage => 'usage: encode_bifcode($arg)',
51 ForceUsage => 'ref and type must be defined',
52 );
53  
54 73 my $err = 'Bifcode::Error::' . $type;
55 73 ▶ my $msg = shift // $messages{$type} // '(no message)';
Condition60%3/5
(shift() // $messages{$type}) // "(no message)"
l!l
710
shift() // $messages{$type}
l!l&&r!l&&!r
8630
MC/DC0%0/2
(shift() // $messages{$type}) // "(no message)"
shift() $messages{$type}
Truth table66%2/3
A // B
A= shift()B= $messages{$type}
ABresult
1X1
011
000
56 73 my $short = shortmess('');
57  
58 73 ▶ $msg =~ s! at$!' at input byte '. ( pos() // 0 )!e;
Condition100%2/2
pos $_ // 0
l!l
521
MC/DC100%1/1
pos $_ // 0
pos $_
Truth table100%2/2
A
A= pos $_
Aresult
11
00
59  
60 0 ▶ eval 'package ' . $err . qq[;
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine __ANON__: called CC=1 SCAR=1.3
Subroutine __ANON__: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine BEGIN: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
Subroutine __ANON__: not called CC=1 SCAR=1.3
61 use overload
62 bool => sub { 1 },
63 '""' => sub { \${ \$_[0] } . ' (' . ( ref \$_[0] ) . ')$short' },
64 fallback => 1;
65 1; ];
66  
67 73 die bless \$msg, $err;
68 }
69  
70 5 my $match = qr/ \G (?|
71 (~)
72 | (0)
73 | (1)
74 | (B|U) (?: ( 0 | [1-9] [0-9]* ) : )?
75 | (I) (?: ( 0 | -? [1-9] [0-9]* ) , )?
76 | (F) (?: ( 0 | -? [1-9] [0-9]* ) \. ( 0 | [0-9]* [1-9] ) e
77 ( (?: 0 | -? [1-9] ) [0-9]* ) , )?
78 | (\[)
79 | (\{)
80 ) /x;
81  
82 183 ▶sub _decode_bifcode_chunk {
Subroutine _decode_bifcode_chunk: called CC=38 SCAR=36.4
83 183 ▶ local $max_depth = $max_depth - 1 if defined $max_depth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bifcode::V1::max_depth 44 138 182
84  
85 183 ▶ unless (m/$match/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/$match/cg) 5 177 182
86 6 ▶ _croak m/ \G \z /xgc ? 'DecodeTrunc' : 'Decode';
Branch: 1 branch
BranchTrueFalseTotal
/ \G \z /cgx ? : 4 1 5
87 }
88  
89 178 ▶ if ( $1 eq '~' ) {
Branch: 9 branches
BranchTrueFalseTotal
if ($1 eq '~') { } 8 169 177
elsif ($1 eq '0') { } 3 166 169
elsif ($1 eq '1') { } 3 163 166
elsif ($1 eq 'B') { } 7 156 163
elsif ($1 eq 'U') { } 68 88 156
elsif ($1 eq 'I') { } 24 64 88
elsif ($1 eq 'F') { } 11 53 64
elsif ($1 eq '[') { } 26 27 53
elsif ($1 eq '{') { } 27 0 27
90 9 return undef;
91 }
92 elsif ( $1 eq '0' ) {
93 4 return boolean::false;
94 }
95 elsif ( $1 eq '1' ) {
96 4 return boolean::true;
97 }
98 elsif ( $1 eq 'B' ) {
99 8 ▶ my $len = $2 // _croak 'DecodeBytes';
Condition66%2/3
$2 // _croak("DecodeBytes")
l!l&&r!l&&!r
520
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 6 ▶ _croak 'DecodeBytesTrunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 1 4 5
101  
102 5 my $data = substr $_, pos(), $len;
103 5 pos() = pos() + $len;
104  
105 5 ▶ _croak 'DecodeBytesTerm' unless m/ \G , /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G , /cgx 1 3 4
106 4 return $data;
107 }
108 elsif ( $1 eq 'U' ) {
109 69 ▶ my $len = $2 // _croak 'DecodeUTF8';
Condition66%2/3
$2 // _croak("DecodeUTF8")
l!l&&r!l&&!r
6350
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 64 ▶ _croak 'DecodeUTF8Trunc' if $len > length() - pos();
Branch: 1 branch
BranchTrueFalseTotal
if $len > length($_) - pos($_) 2 61 63
111  
112 62 utf8::decode( my $str = substr $_, pos(), $len );
113 62 pos() = pos() + $len;
114  
115 62 ▶ _croak 'DecodeUTF8Term' unless m/ \G , /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G , /cgx 2 59 61
116 60 return $str;
117 }
118 elsif ( $1 eq 'I' ) {
119 25 ▶ return $2 if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 16 8 24
120 9 ▶ _croak 'DecodeIntegerTrunc' if m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \z /cgx 1 7 8
121 8 _croak 'DecodeInteger';
122 }
123 elsif ( $1 eq 'F' ) {
124 12 ▶ if ( !defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $2) 8 3 11
125 9 ▶ _croak 'DecodeFloatTrunc' if m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \z /cgx 1 7 8
126 8 _croak 'DecodeFloat';
127 }
128 4 ▶ _croak 'DecodeFloat'
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
129 if $2 eq '0' # mantissa 0.
130 and $3 eq '0' # mantissa 0.0
131 and $4 ne '0'; # sign or exponent 0.0e0
132  
133 3 return $2 . '.' . $3 . 'e' . $4;
134 }
135 elsif ( $1 eq '[' ) {
136 27 ▶ _croak 'DecodeDepth' if defined $max_depth and $max_depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bifcode::V1::max_depth and $Bifcode::V1::max_depth < 0 4 22 26
Condition100%3/3
defined $Bifcode::V1::max_depth and $Bifcode::V1::max_depth < 0
!ll&&!rl&&r
1484
MC/DC100%2/2
defined $Bifcode::V1::max_depth and $Bifcode::V1::max_depth < 0
defined $Bifcode::V1::max_depth $Bifcode::V1::max_depth < 0
Truth table100%3/3
A and B
A= defined $Bifcode::V1::max_depthB= $Bifcode::V1::max_depth < 0
ABresult
0X0
100
111
137  
138 23 my @list;
139 23 ▶ until (m/ \G \] /xgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \G \] /cgx) 35 16 51
140 36 push @list, _decode_bifcode_chunk();
141 }
142 17 return \@list;
143 }
144 elsif ( $1 eq '{' ) {
145 28 ▶ _croak 'DecodeDepth' if defined $max_depth and $max_depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Bifcode::V1::max_depth and $Bifcode::V1::max_depth < 0 3 24 27
Condition100%3/3
defined $Bifcode::V1::max_depth and $Bifcode::V1::max_depth < 0
!ll&&!rl&&r
1593
MC/DC100%2/2
defined $Bifcode::V1::max_depth and $Bifcode::V1::max_depth < 0
defined $Bifcode::V1::max_depth $Bifcode::V1::max_depth < 0
Truth table100%3/3
A and B
A= defined $Bifcode::V1::max_depthB= $Bifcode::V1::max_depth < 0
ABresult
0X0
100
111
146  
147 25 my $last_key;
148 my %hash;
149 25 ▶ until (m/ \G \} /xgc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/ \G \} /cgx) 33 13 46
150 34 ▶ _croak 'DecodeTrunc' if m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \z /cgx 2 31 33
151 32 ▶ _croak 'DecodeKeyType' unless m/ \G (B|U) /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless / \G (B|U) /cgx 2 29 31
152  
153 30 pos() = pos() - 1;
154 30 my $key = _decode_bifcode_chunk();
155  
156 30 ▶ _croak 'DecodeKeyDuplicate' if exists $hash{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{$key} 1 28 29
157 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
158 if defined $last_key and $key lt $last_key;
159 28 ▶ _croak 'DecodeKeyValue' if m/ \G \} /xgc;
Branch: 1 branch
BranchTrueFalseTotal
if / \G \} /cgx 1 26 27
160  
161 27 $last_key = $key;
162 27 $hash{$key} = _decode_bifcode_chunk();
163 }
164 14 return \%hash;
165 }
166 }
167  
168 97 ▶sub decode_bifcode {
Subroutine decode_bifcode: called CC=5 SCAR=16.1
Pod documented
169 97 local $_ = shift;
170 97 local $max_depth = shift;
171  
172 97 ▶ _croak 'DecodeUsage', 'decode_bifcode: too many arguments' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 95 96
173 96 ▶ _croak 'DecodeUsage', 'decode_bifcode: input undefined'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 2 93 95
174 unless defined $_;
175 94 ▶ _croak 'DecodeUsage', 'decode_bifcode: only accepts bytes'
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($_) 1 92 93
176 if utf8::is_utf8($_);
177  
178 93 my $deserialised_data = _decode_bifcode_chunk();
179 44 ▶ _croak 'DecodeTrailing' if $_ !~ m/ \G \z /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ / \G \z /cgx 4 39 43
180 40 return $deserialised_data;
181 }
182  
183 5 my $number_qr = qr/\A ( 0 | -? [1-9] [0-9]* )
184 ( \. ( [0-9]+? ) 0* )?
185 ( e ( -? [0-9]+ ) )? \z/xi;
186  
187 154 ▶sub _encode_bifcode {
Subroutine _encode_bifcode: called CC=24 SCAR=31.8
188 map {
189 154 ▶ if ( !defined $_ ) {
Branch: 9 branches
BranchTrueFalseTotal
if (not defined $_) { } 8 159 167
elsif ((my $ref = ref $_) eq '') { } 92 67 159
elsif ($ref eq 'ARRAY') { } 10 57 67
elsif ($ref eq 'HASH') { } 12 45 57
elsif ($ref eq 'SCALAR' or $ref eq 'Bifcode::BYTES') { } 10 35 45
elsif (&boolean::isBoolean($_)) { } 8 27 35
elsif ($ref eq 'Bifcode::INTEGER') { } 5 22 27
elsif ($ref eq 'Bifcode::FLOAT') { } 16 6 22
elsif ($ref eq 'Bifcode::UTF8') { } 5 1 6
190 9 '~';
191 }
192 elsif ( ( my $ref = ref $_ ) eq '' ) {
193 93 ▶ if ( $_ =~ $number_qr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$number_qr/) { } 70 22 92
194 71 ▶ if ( defined $3 or defined $5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $3 or defined $5) { } 50 20 70
Condition100%3/3
defined $3 or defined $5
l!l&&r!l&&!r
321820
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
195  
196 # normalize to BIFCODE_FLOAT standards
197 51 ▶ my $x = 'F' . ( 0 + $1 ) # remove leading zeros
Condition100%4/4
$3 // 0
l!l
3218
$5 // 0
l!l
4010
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
198 . '.' . ( $3 // 0 ) . 'e' . ( 0 + ( $5 // 0 ) ) . ',';
199 51 $x =~ s/ ([1-9]) (0+ e)/.${1}e/x; # remove trailing zeros
200 51 $x;
201 }
202 else {
203 21 'I' . $_ . ',';
204 }
205 }
206 else {
207 23 utf8::encode( my $str = $_ );
208 23 'U' . length($str) . ':' . $str . ',';
209 }
210 }
211 elsif ( $ref eq 'ARRAY' ) {
212 11 '[' . join( '', map _encode_bifcode($_), @$_ ) . ']';
213 }
214 elsif ( $ref eq 'HASH' ) {
215 '{' . join(
216 '',
217 13 do {
218 13 my @k = sort keys %$_;
219 map {
220 27 my $k = shift @k;
221  
222 # if ( is valid utf8($k) ) {
223 27 utf8::encode($k);
224 27 ( 'U' . length($k) . ':' . $k . ',', $_ );
225  
226 # }
227 # else {
228 # ('B' . length($k) . ':' . $k .',', $_);
229 # }
230 13 } _encode_bifcode( @$_{@k} );
231 }
232 ) . '}';
233 }
234 ▶ 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
2835
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
235 11 ▶ $$_ // _croak 'EncodeBytesUndef';
Condition66%2/3
$$_ // _croak("EncodeBytesUndef")
l!l&&r!l&&!r
820
MC/DC0%0/2
$$_ // _croak("EncodeBytesUndef")
$$_ _croak("EncodeBytesUndef")
Truth table66%2/3
A // B
A= $$_B= _croak("EncodeBytesUndef")
ABresult
1X1
011
000
236 9 'B' . length($$_) . ':' . $$_ . ',';
237 }
238 elsif ( boolean::isBoolean($_) ) {
239 9 $_;
240 }
241 elsif ( $ref eq 'Bifcode::INTEGER' ) {
242 6 ▶ $$_ // _croak 'EncodeIntegerUndef';
Condition66%2/3
$$_ // _croak("EncodeIntegerUndef")
l!l&&r!l&&!r
410
MC/DC0%0/2
$$_ // _croak("EncodeIntegerUndef")
$$_ _croak("EncodeIntegerUndef")
Truth table66%2/3
A // B
A= $$_B= _croak("EncodeIntegerUndef")
ABresult
1X1
011
000
243 5 ▶ _croak 'EncodeInteger', 'invalid integer: ' . $$_
Branch: 1 branch
BranchTrueFalseTotal
unless $$_ =~ /\A (?: 0 | -? [1-9] [0-9]* ) \z/x 2 2 4
244 unless $$_ =~ m/\A (?: 0 | -? [1-9] [0-9]* ) \z/x;
245 3 sprintf 'I%s,', $$_;
246 }
247 elsif ( $ref eq 'Bifcode::FLOAT' ) {
248 17 ▶ $$_ // _croak 'EncodeFloatUndef';
Condition66%2/3
$$_ // _croak("EncodeFloatUndef")
l!l&&r!l&&!r
1510
MC/DC0%0/2
$$_ // _croak("EncodeFloatUndef")
$$_ _croak("EncodeFloatUndef")
Truth table66%2/3
A // B
A= $$_B= _croak("EncodeFloatUndef")
ABresult
1X1
011
000
249 16 ▶ _croak 'EncodeFloat', 'invalid float: ' . $$_
Branch: 1 branch
BranchTrueFalseTotal
unless $$_ =~ /$number_qr/ 1 14 15
250 unless $$_ =~ $number_qr;
251  
252 15 ▶ my $x = 'F' . ( 0 + $1 ) # remove leading zeros
Condition100%4/4
$3 // 0
l!l
104
$5 // 0
l!l
68
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
253 . '.' . ( $3 // 0 ) . 'e' . ( 0 + ( $5 // 0 ) ) . ',';
254 15 $x =~ s/ ([1-9]) (0+ e)/.${1}e/x; # remove trailing zeros
255 15 $x;
256 }
257 elsif ( $ref eq 'Bifcode::UTF8' ) {
258 6 ▶ my $str = $$_ // _croak 'EncodeUTF8Undef';
Condition66%2/3
$$_ // _croak("EncodeUTF8Undef")
l!l&&r!l&&!r
401
MC/DC0%0/2
$$_ // _croak("EncodeUTF8Undef")
$$_ _croak("EncodeUTF8Undef")
Truth table33%1/3
A // B
A= $$_B= _croak("EncodeUTF8Undef")
ABresult
1X1
011
000
259 5 utf8::encode($str); #, sub { croak 'invalid Bifcode::UTF8' } );
260 5 'U' . length($str) . ':' . $str . ',';
261 }
262 else {
263 2 _croak 'EncodeUnhandled', 'unhandled data type: ' . $ref;
264 }
265 } @_;
266 }
267  
268 125 ▶sub encode_bifcode {
Subroutine encode_bifcode: called CC=2 SCAR=6.9
Pod documented
269 125 ▶ _croak 'EncodeUsage' if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 3 121 124
270 122 (&_encode_bifcode)[0];
271 }
272  
273 17 ▶sub force_bifcode {
Subroutine force_bifcode: called CC=3 SCAR=12.0
Pod documented
274 17 my $ref = shift;
275 17 my $type = shift;
276  
277 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
278 17 bless \$ref, 'Bifcode::' . uc($type);
279 }
280  
281 11 ▶sub _expand_bifcode {
Subroutine _expand_bifcode: called CC=1 SCAR=0.0
282 11 my $bifcode = shift;
283 11 $bifcode =~ s/ (
284 [~\[\]\{\}]
285 | (U|B) [0-9]+ :
286 | F -? [0-9]+ \. [0-9]+ e -? [0-9]+ ,
287 | I [0-9]+ ,
288 ) /\n$1/gmx;
289 11 $bifcode =~ s/ \A \n //mx;
290 11 $bifcode . "\n";
291 }
292  
293 8 ▶sub diff_bifcode {
Subroutine diff_bifcode: called CC=4 SCAR=13.9
Pod documented
294 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
295 6 my $b1 = shift;
296 6 my $b2 = shift;
297 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
298  
299 6 require Text::Diff;
300  
301 6 $b1 = _expand_bifcode($b1);
302 6 $b2 = _expand_bifcode($b2);
303 6 return Text::Diff::diff( \$b1, \$b2, $diff_args );
304 }
305  
306 5 decode_bifcode('I1,');
307  
308 __END__
« Bifcode.pm ↑ Summary