BERT/Encoder.pm

filter: Statementstmt 96.3% 130 / 135 Branchbran 85.9% 55 / 64 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 25 / 25 Podpod 11.7% 2 / 17 totaltotal 87.1% 223 / 256 CC 39 SCAR 19.0
CC
39
Cov
92%
encode_any
14.0
encode_regex
11.0
encode_integer
8.0
CRAP 6.7
« BERT/Decoder.pm ↑ Summary BERT/Boolean.pm »
1 package BERT::Encoder;
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 # stolen from Regexp::Common :-)
12 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 INT_RE => qr/^(?:(?:[+-]?)(?:[0123456789]+))$/,
14 FLOAT_RE => qr/^(?:(?i)(?:[+-]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))$/,
15 6 };
16  
17 36 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 36 my $class = shift;
19 36 return bless { }, $class;
20 }
21  
22 36 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
23 36 my ($self, $value) = @_;
24 36 return pack('C', MAGIC_NUMBER) . $self->encode_any($value);
25 }
26  
27 98 ▶sub encode_any {
Subroutine encode_any: called CC=14 SCAR=26.4
Pod undocumented
28 98 my ($self, $value) = @_;
29  
30 98 ▶ return $self->encode_nil unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 96 98
31  
32 96 my $type = ref $value;
33 96 ▶ if ($type eq 'ARRAY') { return $self->encode_array($value) }
Branch: 10 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 13 83 96
elsif ($type eq 'HASH') { } 2 81 83
elsif ($type eq 'Regexp') { } 1 80 81
elsif ($type eq 'BERT::Atom') { } 29 51 80
elsif ($type eq 'BERT::Tuple') { } 14 37 51
elsif ($type eq 'BERT::Boolean') { } 2 35 37
elsif ($type eq 'BERT::Dict') { } 2 33 35
elsif ($type eq 'BERT::Time') { } 1 32 33
elsif ($type eq 'Math::BigInt') { } 4 28 32
elsif ($type) { } 1 27 28
34 2 elsif ($type eq 'HASH') { return $self->encode_dict($value) }
35 1 elsif ($type eq 'Regexp') { return $self->encode_regex($value) }
36 29 elsif ($type eq 'BERT::Atom') { return $self->encode_atom($value) }
37 14 elsif ($type eq 'BERT::Tuple') { return $self->encode_tuple($value) }
38 2 elsif ($type eq 'BERT::Boolean') { return $self->encode_boolean($value) }
39 2 elsif ($type eq 'BERT::Dict') { return $self->encode_dict($value) }
40 1 elsif ($type eq 'BERT::Time') { return $self->encode_time($value) }
41 4 elsif ($type eq 'Math::BigInt') { return $self->encode_integer($value) }
42 1 elsif ($type) { croak "Can't encode type $type" }
43  
44 # I didn't use B::svref_2object on this because by only looking at variables
45 # in Perl can actually modify them
46 27 ▶ if ($value =~ INT_RE) { return $self->encode_integer($value) }
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^(?:(?:[+-]?)(?:[0123456789]+))$/) { } 15 12 27
elsif ($value =~ /^(?:(?i)(?:[+-]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[+-]?)(?:[0123456789]+))|))$/) { } 2 10 12
47 2 elsif ($value =~ FLOAT_RE) { return $self->encode_float($value) }
48 10 else { return $self->encode_binary($value) }
49 }
50  
51 2 ▶sub encode_nil {
Subroutine encode_nil: called CC=1 SCAR=0.0
Pod undocumented
52 2 my ($self) = @_;
53 2 my $perl = BERT::Tuple->new([BERT::Atom->new('bert'), BERT::Atom->new('nil')]);
54 2 return $self->encode_any($perl);
55 }
56  
57 10 ▶sub is_erl_string {
Subroutine is_erl_string: called CC=6 SCAR=18.3
Pod undocumented
58 10 my ($self, $value) = @_;
59
60 # Although it works I'm not sure it's the best way to test whether a
61 # scalar is within the byte range
62 10 foreach my $item (@{ $value }) {
63 17 ▶ if ($item =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^\d+$/) { } 9 8 17
64 9 ▶ return 0 if 0 > $item or $item > 255;
Branch: 1 branch
BranchTrueFalseTotal
if 0 > $item or $item > 255 0 9 9
Condition33%1/3
0 > $item or $item > 255
l!l&&r!l&&!r
009
MC/DC0%0/2
0 > $item or $item > 255
0 > $item $item > 255
Truth table33%1/3
A or B
A= 0 > $itemB= $item > 255
ABresult
1X1
011
000
65 } else {
66 8 ▶ return 0 if length $item != 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $item != 1 7 1 8
67 }
68 }
69 3 return 1;
70 }
71  
72 13 ▶sub encode_array {
Subroutine encode_array: called CC=3 SCAR=11.0
Pod undocumented
73 13 my ($self, $value) = @_;
74 13 my @value = @{ $value };
75
76 13 ▶ return pack('C', NIL_EXT) unless @value;
Branch: 1 branch
BranchTrueFalseTotal
unless @value 3 10 13
77 10 ▶ return $self->encode_bytelist(\@value) if $self->is_erl_string(\@value);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_erl_string(\@value) 3 7 10
78  
79 7 my $array = $self->encode_list(\@value, []);
80 7 return pack('CN', LIST_EXT, scalar @{ $array }) . join('', @{ $array }) . pack('C', NIL_EXT);
81 }
82  
83 73 ▶sub encode_list {
Subroutine encode_list: called CC=2 SCAR=6.9
Pod undocumented
84 73 my ($self, $value, $array) = @_;
85  
86 73 ▶ if (@{ $value }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$value;}) { } 52 21 73
87 52 my $head = shift @{ $value };
88 52 return $self->encode_list($value, [@{ $array }, $self->encode_any($head)]);
89 } else {
90 21 return $array;
91 }
92 }
93  
94 4 ▶sub encode_dict {
Subroutine encode_dict: called CC=3 SCAR=11.0
Pod undocumented
95 4 my ($self, $value) = @_;
96  
97 4 my @array;
98 4 ▶ my @value = ref $value eq 'BERT::Dict' ? @{ $value->value } : %{ $value };
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'BERT::Dict' ? : 2 2 4
99 4 ▶ while (my @key_value = splice(@value, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@key_value) = splice(@value, 0, 2)) 3 4 7
100 3 push @array, BERT::Tuple->new(\@key_value);
101 }
102  
103 4 my $perl = BERT::Tuple->new([BERT::Atom->new('bert'), BERT::Atom->new('dict'), \@array]);
104 4 return $self->encode_any($perl);
105 }
106  
107 1 ▶sub encode_regex {
Subroutine encode_regex: called CC=7 SCAR=24.0
Pod undocumented
108 1 my ($self, $value) = @_;
109  
110 1 for ($value) { s/^\(\?//; s/\)$// }
111 1 my ($modifiers, $pattern) = split /:/, $value, 2;
112 1 my ($on, $off) = split /-/, $modifiers;
113  
114 1 my @options;
115 1 for ($on) {
116 1 ▶ if (/i/) { push @options, BERT::Atom->new('caseless') }
Branch: 4 branches
BranchTrueFalseTotal
if (/i/) { } 1 0 1
elsif (/s/) { } 0 0 0
elsif (/x/) { } 0 0 0
elsif (/m/) { } 0 0 0
117 0 elsif (/s/) { push @options, BERT::Atom->new('dotall') }
118 0 elsif (/x/) { push @options, BERT::Atom->new('extended') }
119 0 elsif (/m/) { push @options, BERT::Atom->new('multiline') }
120 }
121  
122 1 my $perl = BERT::Tuple->new([BERT::Atom->new('bert'), BERT::Atom->new('regex'), $pattern, \@options]);
123 1 return $self->encode_any($perl);
124 }
125  
126 29 ▶sub encode_atom {
Subroutine encode_atom: called CC=1 SCAR=0.0
Pod undocumented
127 29 my ($self, $value) = @_;
128 29 return pack('Cna*', ATOM_EXT, length $value, $value);
129 }
130  
131 14 ▶sub encode_tuple {
Subroutine encode_tuple: called CC=2 SCAR=7.6
Pod undocumented
132 14 my ($self, $value) = @_;
133  
134 14 my @array = @{ $value->value };
135 14 ▶ return pack('C*', SMALL_TUPLE_EXT, scalar @array) . join('', @{ $self->encode_list(\@array, []) }) if @array < 256;
Branch: 1 branch
BranchTrueFalseTotal
if @array < 256 14 0 14
136 0 return pack('CN', LARGE_TUPLE_EXT, scalar @array) . join('', @{ $self->encode_list(\@array, []) });
137 }
138  
139 3 ▶sub encode_bytelist {
Subroutine encode_bytelist: called CC=1 SCAR=0.0
Pod undocumented
140 3 my ($self, $value) = @_;
141 3 return pack('CnC*', STRING_EXT, scalar @{ $value }, @{ $value });
142 }
143  
144 2 ▶sub encode_boolean {
Subroutine encode_boolean: called CC=2 SCAR=6.9
Pod undocumented
145 2 my ($self, $value) = @_;
146  
147 2 ▶ my $boolean = $value ? BERT::Atom->new('true') : BERT::Atom->new('false');
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 1 2
148 2 my $perl = BERT::Tuple->new([BERT::Atom->new('bert'), $boolean]);
149 2 return $self->encode_any($perl);
150 }
151  
152 1 ▶sub encode_time {
Subroutine encode_time: called CC=1 SCAR=0.0
Pod undocumented
153 1 my ($self, $value) = @_;
154  
155 6 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 1 my ($seconds, $microseconds) = $value->value;
157 1 my $megaseconds = $seconds / 1_000_000;
158 1 $seconds = $seconds % 1_000_000;
159 1 my $perl = BERT::Tuple->new([BERT::Atom->new('bert'), BERT::Atom->new('time'), $megaseconds, $seconds, $microseconds]);
160 1 return $self->encode_any($perl);
161 }
162  
163 19 ▶sub encode_integer {
Subroutine encode_integer: called CC=8 SCAR=20.8
Pod undocumented
164 19 my ($self, $value) = @_;
165  
166 19 ▶ return pack('C2', SMALL_INTEGER_EXT, $value)
Branch: 1 branch
BranchTrueFalseTotal
if 0 <= $value and $value <= 255 8 11 19
Condition100%3/3
0 <= $value and $value <= 255
!ll&&!rl&&r
388
MC/DC100%2/2
0 <= $value and $value <= 255
0 <= $value $value <= 255
Truth table100%3/3
A and B
A= 0 <= $valueB= $value <= 255
ABresult
0X0
100
111
167 if 0 <= $value and $value <= 255;
168  
169 # I think newer versions of erlang no longer have the 28bit limit,
170 # so maybe I should add an option to extend the limit to max_int
171 11 ▶ return pack('CN', INTEGER_EXT, $value)
Branch: 1 branch
BranchTrueFalseTotal
if -134217728 <= $value and $value <= 134217727 7 4 11
Condition100%3/3
-134217728 <= $value and $value <= 134217727
!ll&&!rl&&r
137
MC/DC100%2/2
-134217728 <= $value and $value <= 134217727
-134217728 <= $value $value <= 134217727
Truth table100%3/3
A and B
A= -134217728 <= $valueB= $value <= 134217727
ABresult
0X0
100
111
172 if ERL_MIN <= $value and $value <= ERL_MAX;
173  
174 4 ▶ my $sign = $value < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value < 0 ? : 1 3 4
175 4 $value = abs($value);
176  
177 4 my @bytes;
178 4 ▶ while ($value > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > 0) 274 4 278
179 274 push @bytes, $value & 0xFF;
180 274 $value >>= 8;
181 }
182  
183 4 ▶ return pack('C*', SMALL_BIG_EXT, scalar @bytes, $sign, @bytes) if @bytes < 256;
Branch: 1 branch
BranchTrueFalseTotal
if @bytes < 256 3 1 4
184 1 return pack('CNC*', LARGE_BIG_EXT, scalar @bytes, $sign, @bytes);
185 }
186  
187 2 ▶sub encode_float {
Subroutine encode_float: called CC=1 SCAR=0.0
Pod undocumented
188 2 my ($self, $value) = @_;
189 2 return pack('CZ31', FLOAT_EXT, sprintf('%.20e', $value));
190 }
191  
192 10 ▶sub encode_binary {
Subroutine encode_binary: called CC=1 SCAR=0.0
Pod undocumented
193 10 my ($self, $value) = @_;
194 10 return pack('CNa*', BINARY_EXT, length $value, $value);
195 }
196  
197 6 1;
198  
199 __END__
« BERT/Decoder.pm ↑ Summary BERT/Boolean.pm »