BERT/Decoder.pm

filter: Statementstmt 92.2% 130 / 141 Branchbran 79.3% 46 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.0% 24 / 25 Podpod 10.5% 2 / 19 totaltotal 83.1% 202 / 243 CC 32 SCAR 20.3
CC
32
Cov
88%
extract_complex_type
15.8
read_any
13.2
extract_any
3.0
CRAP 7.6
↑ Summary BERT/Encoder.pm »
1 package BERT::Decoder;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use BERT::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use BERT::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 34 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 34 my $class = shift;
13 34 return bless { }, $class;
14 }
15  
16 34 ▶sub decode {
Subroutine decode: called CC=2 SCAR=6.9
Pod documented
17 34 my ($self, $bert) = @_;
18  
19 34 (my $magic, $bert) = unpack('Ca*', $bert);
20  
21 34 ▶ croak sprintf('Bad magic number. Expected %d found %d', MAGIC_NUMBER, $magic)
Branch: 1 branch
BranchTrueFalseTotal
unless 131 == $magic 1 33 34
22 unless MAGIC_NUMBER == $magic;
23  
24 33 return $self->extract_any($bert);
25 }
26  
27 33 ▶sub extract_any {
Subroutine extract_any: called CC=3 SCAR=11.0
Pod undocumented
28 33 my ($self, $bert) = @_;
29  
30 33 (my $value, $bert) = $self->read_any($bert);
31  
32 33 ▶ $value = $self->extract_complex_type($value)
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "BERT::Tuple" 9 24 33
33 if ref $value eq 'BERT::Tuple';
34  
35 33 ▶ return [ $value, $self->extract_any($bert) ] if $bert;
Branch: 1 branch
BranchTrueFalseTotal
if $bert 0 33 33
36 33 return $value;
37 }
38  
39 9 ▶sub extract_complex_type {
Subroutine extract_complex_type: called CC=13 SCAR=27.6
Pod undocumented
40 9 my ($self, $tuple) = @_;
41  
42 9 my @array = @{ $tuple->value };
43 9 ▶ return $tuple unless $array[0] eq 'bert';
Branch: 1 branch
BranchTrueFalseTotal
unless $array[0] eq "bert" 1 8 9
44  
45 8 ▶ if ($array[1] eq 'nil') {
Branch: 6 branches
BranchTrueFalseTotal
if ($array[1] eq 'nil') { } 1 7 8
elsif ($array[1] eq 'true') { } 1 6 7
elsif ($array[1] eq 'false') { } 1 5 6
elsif ($array[1] eq 'dict') { } 3 2 5
elsif ($array[1] eq 'time') { } 1 1 2
elsif ($array[1] eq 'regex') { } 1 0 1
46 1 return undef;
47 } elsif ($array[1] eq 'true') {
48 1 return BERT::Boolean->true;
49 } elsif ($array[1] eq 'false') {
50 1 return BERT::Boolean->false;
51 } elsif ($array[1] eq 'dict') {
52 3 my @dict = map(@{ $_->value }, @{ $array[2] });
53  
54 # Someday I should add an option to allow hashref to be returned instead
55 3 return BERT::Dict->new(\@dict);
56 } elsif ($array[1] eq 'time') {
57 1 my ($megasec, $sec, $microsec) = @array[2, 3, 4];
58 1 return BERT::Time->new($megasec * 1_000_000 + $sec, $microsec);
59 } elsif ($array[1] eq 'regex') {
60 1 my ($source, $options) = @array[2, 3];
61 1 my $opt = '';
62 1 for (@{ $options }) {
63 1 ▶ if ($_ eq 'caseless') { $opt .= 'i' }
Branch: 4 branches
BranchTrueFalseTotal
if ($_ eq 'caseless') { } 1 0 1
elsif ($_ eq 'dotall') { } 0 0 0
elsif ($_ eq 'extended') { } 0 0 0
elsif ($_ eq 'multiline') { } 0 0 0
64 0 elsif ($_ eq 'dotall') { $opt .= 's' }
65 0 elsif ($_ eq 'extended') { $opt .= 'x' }
66 0 elsif ($_ eq 'multiline') { $opt .= 'm' }
67 }
68 1 return eval "qr/$source/$opt";
69 } else {
70 0 croak "Unknown complex type $array[1]";
71 }
72 }
73  
74 80 ▶sub read_any {
Subroutine read_any: called CC=13 SCAR=25.8
Pod undocumented
75 80 my ($self, $bert) = @_;
76 80 my $value;
77  
78 80 (my $type, $bert) = unpack('Ca*', $bert);
79  
80 80 ▶ if (SMALL_INTEGER_EXT == $type) { return $self->read_small_integer($bert) }
Branch: 12 branches
BranchTrueFalseTotal
if (97 == $type) { } 8 72 80
elsif (98 == $type) { } 7 65 72
elsif (99 == $type) { } 2 63 65
elsif (100 == $type) { } 25 38 63
elsif (104 == $type) { } 12 26 38
elsif (105 == $type) { } 0 26 26
elsif (106 == $type) { } 2 24 26
elsif (107 == $type) { } 3 21 24
elsif (108 == $type) { } 7 14 21
elsif (109 == $type) { } 10 4 14
elsif (110 == $type) { } 3 1 4
elsif (111 == $type) { } 1 0 1
81 7 elsif (INTEGER_EXT == $type) { return $self->read_integer($bert) }
82 2 elsif (FLOAT_EXT == $type) { return $self->read_float($bert) }
83 25 elsif (ATOM_EXT == $type) { return $self->read_atom($bert) }
84 12 elsif (SMALL_TUPLE_EXT == $type) { return $self->read_small_tuple($bert) }
85 0 elsif (LARGE_TUPLE_EXT == $type) { return $self->read_large_tuple($bert) }
86 2 elsif (NIL_EXT == $type) { return $self->read_nil($bert) }
87 3 elsif (STRING_EXT == $type) { return $self->read_string($bert) }
88 7 elsif (LIST_EXT == $type) { return $self->read_list($bert) }
89 10 elsif (BINARY_EXT == $type) { return $self->read_binary($bert) }
90 3 elsif (SMALL_BIG_EXT == $type) { return $self->read_small_big($bert) }
91 1 elsif (LARGE_BIG_EXT == $type) { return $self->read_large_big($bert) }
92 0 else { croak "Unknown type $type" }
93 }
94  
95 8 ▶sub read_small_integer {
Subroutine read_small_integer: called CC=1 SCAR=0.0
Pod undocumented
96 8 my ($self, $bert) = @_;
97 8 (my $value, $bert) = unpack('Ca*', $bert);
98 8 return ($value, $bert);
99 }
100  
101 7 ▶sub read_integer {
Subroutine read_integer: called CC=1 SCAR=0.0
Pod undocumented
102 7 my ($self, $bert) = @_;
103  
104 # This should have been unpack('l>a*',...) only and not have extra unpack('l',...)
105 # but I don't want to require perl >= v5.10
106 7 (my $value, $bert) = unpack('Na*', $bert);
107 7 $value = unpack('l', pack('L', $value));
108 7 return ($value, $bert);
109 }
110  
111 2 ▶sub read_float {
Subroutine read_float: called CC=1 SCAR=0.0
Pod undocumented
112 2 my ($self, $bert) = @_;
113 2 (my $value, $bert) = unpack('Z31a*', $bert);
114 2 return ($value, $bert);
115 }
116  
117 25 ▶sub read_atom {
Subroutine read_atom: called CC=1 SCAR=0.0
Pod undocumented
118 25 my ($self, $bert) = @_;
119 25 (my $len, $bert) = unpack('na*', $bert);
120 25 (my $value, $bert) = unpack("a$len a*", $bert);
121 25 $value = BERT::Atom->new($value);
122 25 return ($value, $bert);
123 }
124  
125 12 ▶sub read_small_tuple {
Subroutine read_small_tuple: called CC=1 SCAR=0.0
Pod undocumented
126 12 my ($self, $bert) = @_;
127 12 (my $len, $bert) = unpack('Ca*', $bert);
128 12 (my $value, $bert) = $self->read_array($bert, $len, []);
129 12 $value = BERT::Tuple->new($value);
130 12 return ($value, $bert);
131 }
132  
133 0 ▶sub read_large_tuple {
Subroutine read_large_tuple: not called CC=1 SCAR=6.9
Pod undocumented
134 0 my ($self, $bert) = @_;
135 0 (my $len, $bert) = unpack('Na*', $bert);
136 0 (my $value, $bert) = $self->read_array($bert, $len, []);
137 0 $value = BERT::Tuple->new($value);
138 0 return ($value, $bert);
139 }
140  
141 2 ▶sub read_nil {
Subroutine read_nil: called CC=1 SCAR=0.0
Pod undocumented
142 2 my ($self, $bert) = @_;
143 2 my $value = [];
144 2 return ($value, $bert);
145 }
146  
147 3 ▶sub read_string {
Subroutine read_string: called CC=1 SCAR=0.0
Pod undocumented
148 3 my ($self, $bert) = @_;
149 3 (my $len, $bert) = unpack('na*', $bert);
150 3 my @values = unpack("C$len a*", $bert);
151 3 $bert = pop @values;
152 3 my $value = \@values;
153 3 return ($value, $bert);
154 }
155  
156 7 ▶sub read_list {
Subroutine read_list: called CC=2 SCAR=7.0
Pod undocumented
157 7 my ($self, $bert) = @_;
158 7 (my $len, $bert) = unpack('Na*', $bert);
159 7 (my $value, $bert) = $self->read_array($bert, $len, []);
160 7 (my $type, $bert) = unpack('Ca*', $bert);
161 7 ▶ croak 'Lists with non NIL tails are not supported'
Branch: 1 branch
BranchTrueFalseTotal
unless 106 == $type 0 7 7
162 unless NIL_EXT == $type;
163 7 return ($value, $bert);
164 }
165  
166 10 ▶sub read_binary {
Subroutine read_binary: called CC=1 SCAR=0.0
Pod undocumented
167 10 my ($self, $bert) = @_;
168 10 (my $len, $bert) = unpack('Na*', $bert);
169 10 (my $value, $bert) = unpack("a$len a*", $bert);
170 10 return ($value, $bert);
171 }
172  
173 3 ▶sub read_small_big {
Subroutine read_small_big: called CC=1 SCAR=0.0
Pod undocumented
174 3 my ($self, $bert) = @_;
175 3 (my $len, $bert) = unpack('Ca*', $bert);
176 3 (my $value, $bert) = $self->read_bigint($bert, $len);
177 3 return ($value, $bert);
178 }
179  
180 1 ▶sub read_large_big {
Subroutine read_large_big: called CC=1 SCAR=0.0
Pod undocumented
181 1 my ($self, $bert) = @_;
182 1 (my $len, $bert) = unpack('Na*', $bert);
183 1 (my $value, $bert) = $self->read_bigint($bert, $len);
184 1 return ($value, $bert);
185 }
186  
187 4 ▶sub read_bigint {
Subroutine read_bigint: called CC=3 SCAR=11.0
Pod undocumented
188 4 my $self = shift;
189 4 my ($bert, $len) = @_;
190  
191 4 my($sign, @values) = unpack("CC$len a*", $bert);
192 4 $bert = pop @values;
193  
194 4 require Math::BigInt;
195 4 my $i = Math::BigInt->new(0);
196 4 my $value = 0;
197  
198 4 foreach my $item (@values) {
199 274 $value += $item * 256 ** $i++;
200 }
201  
202 4 ▶ $value->bneg() if $sign != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sign != 0 1 3 4
203  
204 4 return ($value, $bert);
205 }
206  
207 66 ▶sub read_array {
Subroutine read_array: called CC=2 SCAR=6.9
Pod undocumented
208 66 my $self = shift;
209 66 my ($bert, $len, $array) = @_;
210  
211 66 ▶ if ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) { } 47 19 66
212 47 (my $value, $bert) = $self->read_any($bert);
213 47 return $self->read_array($bert, $len - 1, [@{ $array }, $value]);
214 } else {
215 19 return ($array, $bert);
216 }
217 }
218  
219 6 1;
220  
221 __END__
↑ Summary BERT/Encoder.pm »