BitStream/Base.pm

filter: Statementstmt 81.8% 355 / 434 Branchbran 65.9% 157 / 238 Conditioncond 44.6% 29 / 65 MC/DCmcdc 20.5% 8 / 39 Subroutinesub 89.5% 60 / 67 Podpod 94.8% 37 / 39 totaltotal 73.2% 646 / 882 CC 147 SCAR 25.0
CC
147
Cov
73%
read_open
56.0
BUILD
32.7
error_code
28.6
CRAP 12.2
« BitStream/Code/Baer.pm ↑ Summary BitStream/Code/Omega.pm »
1 package Data::BitStream::Base;
2 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 $Data::BitStream::Base::AUTHORITY = 'cpan:DANAJ';
7 28 $Data::BitStream::Base::VERSION = '0.08';
8 }
9  
10 our $CODEINFO = [ { package => __PACKAGE__,
11 name => 'Unary',
12 universal => 0,
13 params => 0,
14 3 ▶ encodesub => sub {shift->put_unary(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
15 25 ▶ decodesub => sub {shift->get_unary(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
16 },
17 { package => __PACKAGE__,
18 name => 'Unary1',
19 universal => 0,
20 params => 0,
21 3 ▶ encodesub => sub {shift->put_unary1(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
22 25 ▶ decodesub => sub {shift->get_unary1(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
23 },
24 { package => __PACKAGE__,
25 name => 'BinWord',
26 universal => 0,
27 params => 1,
28 1 ▶ encodesub => sub {shift->put_binword(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
29 23 ▶ decodesub => sub {shift->get_binword(@_)},
Subroutine __ANON__: called CC=1 SCAR=0.0
30 },
31 28 ];
32  
33 28 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 28 ▶use MooX::Types::MooseLike::Base qw/Int Bool Str ArrayRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # pos is ignored while writing
37 1648 ▶has 'pos' => (is => 'ro', writer => '_setpos', default => sub{0});
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1648 ▶has 'len' => (is => 'ro', writer => '_setlen', default => sub{0});
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1647 ▶has 'mode' => (is => 'rw', default => sub{'rdwr'});
Subroutine __ANON__: called CC=1 SCAR=0.0
40 has '_code_pos_array' => (is => 'rw',
41 isa => ArrayRef[Int],
42 1648 ▶ default => sub {[]} );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 has '_code_str_array' => (is => 'rw',
44 isa => ArrayRef[Str],
45 1648 ▶ default => sub {[]} );
Subroutine __ANON__: called CC=1 SCAR=0.0
46  
47 28 has 'file' => (is => 'ro', writer => '_setfile');
48 28 has 'fheader' => (is => 'ro', writer => '_setfheader');
49 28 has 'fheaderlines' => (is => 'ro');
50  
51 1648 ▶has 'writing' => (is => 'ro', isa => Bool, writer => '_setwrite', default => sub {1});
Subroutine __ANON__: called CC=1 SCAR=0.0
52  
53 # Useful for testing, but time consuming. Not so bad now that all the test
54 # suites call put_* ~30 times with a list instead of per-value ~30,000 times.
55 # It still makes the test suite take about 20% longer.
56 #
57 # after '_setpos' => sub {
58 # my $self = shift;
59 # my $pos = $self->pos;
60 # my $len = $self->len;
61 # die "position must be >= 0" if $pos < 0;
62 # die "position must be <= length" if $pos > $len;
63 # $pos;
64 # };
65  
66 1648 ▶sub BUILD {
Subroutine BUILD: called CC=15 SCAR=34.9
Pod undocumented
67 1648 my $self = shift;
68  
69 # Looks like some systems aren't setting these correctly via the default.
70 # I cannot reproduce the issue even with the same versions of Perl, Moo,
71 # and Class::XSAccessor. So, we'll set them here.
72 1648 $self->_code_pos_array([]);
73 1648 $self->_code_str_array([]);
74 1648 $self->_setpos(0);
75 1648 $self->_setlen(0);
76  
77 # Change mode to canonical form
78 1648 my $curmode = $self->mode;
79 1648 my $is_writing;
80 0 ▶ if ($curmode eq 'read') { $curmode = 'r'; }
Branch: 7 branches
BranchTrueFalseTotal
if ($curmode eq 'read') { } 0 1648 1648
elsif ($curmode eq 'readonly') { } 0 1648 1648
elsif ($curmode eq 'write') { } 0 1648 1648
elsif ($curmode eq 'writeonly') { } 0 1648 1648
elsif ($curmode eq 'readwrite') { } 0 1648 1648
elsif ($curmode eq 'rdwr') { } 1647 1 1648
elsif ($curmode eq 'append') { } 0 1 1
81 0 elsif ($curmode eq 'readonly') { $curmode = 'ro'; }
82 0 elsif ($curmode eq 'write') { $curmode = 'w'; }
83 0 elsif ($curmode eq 'writeonly') { $curmode = 'wo'; }
84 0 elsif ($curmode eq 'readwrite') { $curmode = 'rw'; }
85 1647 elsif ($curmode eq 'rdwr') { $curmode = 'rw'; }
86 0 elsif ($curmode eq 'append') { $curmode = 'a'; }
87 1648 ▶ die "Unknown mode: $curmode" unless $curmode =~ /^(?:r|ro|w|wo|rw|a)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $curmode =~ /^(?:r|ro|w|wo|rw|a)$/ 1 1647 1648
88 1647 $self->mode( $curmode );
89  
90 # Set writing based on mode
91 0 ▶ if ($curmode =~ /^ro?$/) { $is_writing = 0; }
Branch: 4 branches
BranchTrueFalseTotal
if ($curmode =~ /^ro?$/) { } 0 1647 1647
elsif ($curmode =~ /^wo?$/) { } 0 1647 1647
elsif ($curmode eq 'rw') { } 1647 0 1647
elsif ($curmode eq 'a') { } 0 0 0
92 0 elsif ($curmode =~ /^wo?$/) { $is_writing = 1; }
93 1647 elsif ($curmode eq 'rw') { $is_writing = 1; }
94 0 elsif ($curmode eq 'a') { $is_writing = 0; }
95  
96 1647 ▶ if ($is_writing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_writing) { } 1647 0 1647
97 1647 $self->_setwrite(1);
98 1647 $self->write_open;
99 } else {
100 0 $self->_setwrite(0);
101 0 $self->read_open;
102 }
103  
104 1647 ▶ $self->write_open if $curmode eq 'a';
Branch: 1 branch
BranchTrueFalseTotal
if $curmode eq "a" 0 1647 1647
105 # TODO: writeonly doesn't really work
106 }
107  
108 1648 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=6.9
Pod undocumented
109 1648 my $self = shift;
110 1648 ▶ $self->write_close if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 270 1378 1648
111 }
112  
113 28 my $_host_word_size; # maxbits
114 my $_all_ones; # maxval
115  
116 28 ▶BEGIN {
Subroutine BEGIN: called CC=11 SCAR=32.2
117 28 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 $_host_word_size =
119 ( (defined $Config{'use64bitint'} && $Config{'use64bitint'} eq 'define')
120 || (defined $Config{'use64bitall'} && $Config{'use64bitall'} eq 'define')
121 28 ▶ || (defined $Config{'longsize'} && $Config{'longsize'} >= 8)
Branch: 1 branch
BranchTrueFalseTotal
defined $Data::BitStream::Base::Config{'use64bitint'} && $Data::BitStream::Base::Config{'use64bitint'} eq 'define' || defined $Data::BitStream::Base::Config{'use64bitall'} && $Data::BitStream::Base::Config{'use64bitall'} eq 'define' || defined $Data::BitStream::Base::Config{'longsize'} && $Data::BitStream::Base::Config{'longsize'} >= 8 ? : 28 0 28
Condition20%3/15
defined $Data::BitStream::Base::Config{"use64bitint"} and $Data::BitStream::Base::Config{"use64bitint"} eq "define" or defined $Data::BitStream::Base::Config{"use64bitall"} and $Data::BitStream::Base::Config{"use64bitall"} eq "define" or defined $Data::BitStream::Base::Config{"longsize"} and $Data::BitStream::Base::Config{"longsize"} >= 8
l!l&&r!l&&!r
2800
defined $Data::BitStream::Base::Config{"use64bitint"} and $Data::BitStream::Base::Config{"use64bitint"} eq "define" or defined $Data::BitStream::Base::Config{"use64bitall"} and $Data::BitStream::Base::Config{"use64bitall"} eq "define"
l!l&&r!l&&!r
2800
defined $Data::BitStream::Base::Config{"use64bitint"} and $Data::BitStream::Base::Config{"use64bitint"} eq "define"
!ll&&!rl&&r
0028
defined $Data::BitStream::Base::Config{"use64bitall"} and $Data::BitStream::Base::Config{"use64bitall"} eq "define"
!ll&&!rl&&r
000
defined $Data::BitStream::Base::Config{"longsize"} and $Data::BitStream::Base::Config{"longsize"} >= 8
!ll&&!rl&&r
000
MC/DC0%0/6
defined $Data::BitStream::Base::Config{"use64bitint"} and $Data::BitStream::Base::Config{"use64bitint"} eq "define" or defined $Data::BitStream::Base::Config{"use64bitall"} and $Data::BitStream::Base::Config{"use64bitall"} eq "define" or defined $Data::BitStream::Base::Config{"longsize"} and $Data::BitStream::Base::Config{"longsize"} >= 8
defined $Data::BitStream::Base::Config{"use64bitint"} $Data::BitStream::Base::Config{"use64bitint"} eq "define" defined $Data::BitStream::Base::Config{"use64bitall"} $Data::BitStream::Base::Config{"use64bitall"} eq "define" defined $Data::BitStream::Base::Config{"longsize"} $Data::BitStream::Base::Config{"longsize"} >= 8
Truth table6%1/15
A and B or C and D or E and F
A= defined $Data::BitStream::Base::Config{"use64bitint"}B= $Data::BitStream::Base::Config{"use64bitint"} eq "define"C= defined $Data::BitStream::Base::Config{"use64bitall"}D= $Data::BitStream::Base::Config{"use64bitall"} eq "define"E= defined $Data::BitStream::Base::Config{"longsize"}F= $Data::BitStream::Base::Config{"longsize"} >= 8
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
122 )
123 ? 64
124 : 32;
125 28 ▶ no Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 # Check sanity of ~0
128 28 my $notzero = ~0;
129 28 ▶ if ($_host_word_size == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_host_word_size == 32) { } 0 28 28
130 0 die "Config says 32-bit Perl, but int is $notzero" if ~0 != 0xFFFFFFFF;
131 } else {
132 28 die "Config says 64-bit Perl, but int is $notzero" if ((~0 >> 16) >> 16) != 0xFFFFFFFF;
133 }
134  
135 # 64-bit seems broken in Perl 5.6.2 on the 32-bit system I have (and at
136 # least one CPAN Tester shows the same). Try:
137 # perl -e 'die if 18446744073709550593 == ~0'
138 # That inexplicably dies on 64-bit 5.6.2. It works fine on 5.8.0 and later.
139 #
140 # Direct method, pre-5.8.0 Perls.
141 # $_host_word_size = 32 if $] < 5.008;
142 # Detect the symptoms (should allow 5.6.2 on 64-bit O/S to work fine):
143 28 ▶ if ( ($_host_word_size == 64) && (18446744073709550592 == ~0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if () 0 28 28
Condition50%1/2
$_host_word_size == 64 and
!ll
028
MC/DC0%0/1
$_host_word_size == 64 and
$_host_word_size == 64
Truth table50%1/2
A
A= $_host_word_size == 64
Aresult
00
11
144 0 $_host_word_size = 32;
145 }
146  
147 28 ▶ $_all_ones = ($_host_word_size == 32) ? 0xFFFFFFFF : ~0;
Branch: 1 branch
BranchTrueFalseTotal
$_host_word_size == 32 ? : 0 28 28
148 }
149 # Moo 1.000007 doesn't allow inheritance of 'use constant'.
150 #use constant maxbits => $_host_word_size;
151 #use constant maxval => $_all_ones;
152 # Use a sub with empty prototype (see perlsub documentation)
153 1105989 ▶sub maxbits () { $_host_word_size } ## no critic (ProhibitSubroutinePrototypes)
Subroutine maxbits: called CC=1 SCAR=0.0
Pod documented
154 92500 ▶sub maxval () { $_all_ones } ## no critic (ProhibitSubroutinePrototypes)
Subroutine maxval: called CC=1 SCAR=0.0
Pod documented
155  
156 30205 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=6.9
Pod documented
157 30205 my $self = shift;
158 30205 ▶ $self->error_stream_mode('rewind') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 1 30204 30205
159 30204 $self->_setpos(0);
160 30204 1;
161 }
162 77787 ▶sub skip {
Subroutine skip: called CC=4 SCAR=13.9
Pod documented
163 77787 my $self = shift;
164 77787 ▶ $self->error_stream_mode('skip') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 1 77786 77787
165 77786 my $skip = shift;
166 77786 my $pos = $self->pos;
167 77786 my $len = $self->len;
168 77786 my $newpos = $pos + $skip;
169 77786 ▶ $self->error_off_stream('skip') if $newpos < 0 || $newpos > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $newpos < 0 or $newpos > $len 10 77776 77786
Condition66%2/3
$newpos < 0 or $newpos > $len
l!l&&r!l&&!r
01077776
MC/DC50%1/2
$newpos < 0 or $newpos > $len
$newpos < 0 $newpos > $len
Truth table66%2/3
A or B
A= $newpos < 0B= $newpos > $len
ABresult
1X1
011
000
170 77776 $self->_setpos($newpos);
171 77776 1;
172 }
173 1 ▶sub exhausted {
Subroutine exhausted: called CC=2 SCAR=8.1
Pod documented
174 1 my $self = shift;
175 1 ▶ $self->error_stream_mode('exhausted') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 1 0 1
176 0 $self->pos >= $self->len;
177 }
178 20047 ▶sub erase {
Subroutine erase: called CC=1 SCAR=0.0
Pod documented
179 20047 my $self = shift;
180 20047 $self->_setlen(0);
181 20047 $self->_setpos(0);
182 # Writing state is left unchanged
183 # You want an after method to handle the data
184 }
185 0 ▶sub read_open {
Subroutine read_open: not called CC=7 SCAR=40.3
Pod documented
186 0 my $self = shift;
187 0 ▶ $self->error_stream_mode('read') if $self->mode eq 'wo';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "wo" 0 0 0
188 0 ▶ $self->write_close if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 0 0 0
189 0 my $file = $self->file;
190 0 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 0 0
191 0 ▶ open(my $fp, "<", $file) or die "Cannot open file '$file' for read: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fp, "<", $file or die "Cannot open file '${file}' for read: $!\n" 0 0 0
192 0 my $headerlines = $self->fheaderlines;
193 0 ▶ if (defined $headerlines) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headerlines) 0 0 0
194 # Read in their header
195 0 my $header = '';
196 0 ▶ while ($headerlines-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headerlines-- > 0) 0 0 0
197 0 $header .= <$fp>;
198 }
199 0 $self->_setfheader($header);
200 }
201 0 binmode $fp;
202 # Turn off file linking while calling from_raw
203 0 my $saved_mode = $self->mode;
204 0 $self->_setfile( undef );
205 0 $self->mode( 'rw' );
206 0 my $bits = <$fp>;
207 {
208 0 local $/;
209 0 $self->from_raw( <$fp>, $bits );
210 }
211 0 close $fp;
212 # link us back.
213 0 $self->_setfile( $file );
214 0 $self->mode( $saved_mode );
215 }
216 0 1;
217 }
218 27146 ▶sub write_open {
Subroutine write_open: called CC=3 SCAR=11.0
Pod documented
219 27146 my $self = shift;
220 27146 ▶ $self->error_stream_mode('write') if $self->mode eq 'ro';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "ro" 0 27146 27146
221 27146 ▶ if (!$self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->writing) 25427 1719 27146
222 25427 $self->_setwrite(1);
223 # pos is now ignored
224 }
225 27146 1;
226 }
227 27091 ▶sub write_close {
Subroutine write_close: called CC=6 SCAR=26.2
Pod documented
228 27091 my $self = shift;
229 27091 ▶ if ($self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->writing) 27075 16 27091
230 27075 $self->_setwrite(0);
231 27075 $self->_setpos($self->len);
232  
233 27075 my $file = $self->file;
234 27075 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 27075 27075
235 0 ▶ open(my $fp, ">", $file) or die "Cannot open file $file for write: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fp, ">", $file or die "Cannot open file $file for write: $!\n" 0 0 0
236 0 my $header = $self->fheader;
237 0 ▶ print $fp $header, "\n" if defined $header && length($header) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $header and length $header > 0 0 0 0
Condition0%0/3
defined $header and length $header > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $header and length $header > 0
defined $header length $header > 0
Truth table0%0/3
A and B
A= defined $headerB= length $header > 0
ABresult
0X0
100
111
238 0 binmode $fp;
239 0 print $fp $self->len, "\n";
240 0 print $fp $self->to_raw;
241 0 close $fp;
242 }
243 }
244 27091 1;
245 }
246  
247  
248 ####### Error handling
249 #
250 # This section has two purposes:
251 # 1. enforce a common set of failure messages for all codes.
252 # 2. enable the position to be reset to the start of a code on an error.
253 #
254 # Number 2 is relatively complex since codes can be composed of other codes,
255 # and we want to back up to the start of the outermost code. We set up a stack
256 # of saved positions which can be used when an error occurs.
257 #
258 # If your code methods do not either call other codes or make multiple calls to
259 # read / skip, then there really is no extra effort. If they do, then it is
260 # important to call code_pos_start() before starting, code_pos_set() before
261 # each successive value, and code_pos_end() when done. What you get in return
262 # is not having to worry about how far you've read -- the position will be
263 # restored to the start of the outermost code.
264 #
265 # From the user's point of view, this means if they try to read a complicated
266 # code and it is invalid, the position is left unchanged, instead of some
267 # random distance forward in the stream.
268  
269 106285 ▶sub code_pos_start { # Starting a code
Subroutine code_pos_start: called CC=1 SCAR=0.0
Pod documented
270 106285 my $self = shift;
271 106285 my $name = shift;
272 106285 push @{$self->_code_pos_array}, $self->pos;
273 106285 push @{$self->_code_str_array}, $name;
274 #print STDERR "error stack is ", join(",", @{$self->_code_str_array}), "\n";
275 }
276 183272 ▶sub code_pos_set { # Replace position
Subroutine code_pos_set: called CC=1 SCAR=0.0
Pod documented
277 183272 my $self = shift;
278 183272 $self->_code_pos_array->[-1] = $self->pos;
279 }
280 106161 ▶sub code_pos_end { # Remove position -- we're not in this code any more
Subroutine code_pos_end: called CC=1 SCAR=0.0
Pod documented
281 106161 my $self = shift;
282 106161 pop @{$self->_code_pos_array};
283 106161 pop @{$self->_code_str_array};
284 }
285 185 ▶sub _code_restore_pos { # Returns string of code name
Subroutine _code_restore_pos: called CC=6 SCAR=18.1
286 185 my $self = shift;
287 # Check that we aren't trying to restore a position while writing
288 185 ▶ if ($self->writing and @{$self->_code_pos_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->writing and @{$self->_code_pos_array;}) 0 185 185
Condition66%2/3
$self->writing and @{$self->_code_pos_array;}
!ll&&!rl&&r
121640
MC/DC0%0/2
$self->writing and @{$self->_code_pos_array;}
$self->writing @{$self->_code_pos_array;}
Truth table66%2/3
A and B
A= $self->writingB= @{$self->_code_pos_array;}
ABresult
0X0
100
111
289 0 confess "Found code position while error in writing: " . $self->_code_str_array->[0];
290 }
291 # Put position back to start of topmost code
292 185 ▶ if (@{$self->_code_pos_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_code_pos_array;}) 98 87 185
293 98 $self->_setpos($self->_code_pos_array->[0]);
294 98 @{$self->_code_pos_array} = ();
295 }
296 185 my $codename = '';
297 185 ▶ if (@{$self->_code_str_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_code_str_array;}) 98 87 185
298 98 ▶ $codename = $self->_code_str_array->[0] if defined $self->_code_str_array->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_code_str_array->[0] 98 0 98
299 98 @{$self->_code_str_array} = ();
300 }
301 185 $codename;
302 }
303  
304 # This can be called after any code routines have been used, to verify they
305 # cleaned up after themselves. Failing this usually means someone died inside
306 # an eval, while being called by a code routine. It's also possible a broken
307 # code routine did a code_pos_start then returned without a matching end.
308 119 ▶sub code_pos_is_set {
Subroutine code_pos_is_set: called CC=2 SCAR=9.2
Pod documented
309 119 my $self = shift;
310 119 ▶ return unless @{$self->_code_pos_array}; # return undef if nothing.
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_code_pos_array;} 119 0 119
311  
312 0 my $text = join(",", @{$self->_code_str_array});
313 0 $text;
314 }
315  
316 57 ▶sub error_off_stream {
Subroutine error_off_stream: called CC=4 SCAR=13.9
Pod documented
317 57 my $self = shift;
318 57 my $skipping = shift;
319  
320 # Give the skip error only if we were not reading a code.
321 57 ▶ if ( (defined $skipping) && (@{$self->_code_pos_array} == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $skipping and @{$self->_code_pos_array;} == 0) 1 56 57
Condition100%3/3
defined $skipping and @{$self->_code_pos_array;} == 0
!ll&&!rl&&r
4791
MC/DC100%2/2
defined $skipping and @{$self->_code_pos_array;} == 0
defined $skipping @{$self->_code_pos_array;} == 0
Truth table100%3/3
A and B
A= defined $skippingB= @{$self->_code_pos_array;} == 0
ABresult
0X0
100
111
322 1 croak "skip off end of stream";
323 }
324  
325 56 my $codename = $self->_code_restore_pos();
326 56 ▶ $codename = " ($codename)" if $codename ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $codename ne "" 51 5 56
327 56 croak "read off end of stream$codename";
328 }
329 10 ▶sub error_stream_mode {
Subroutine error_stream_mode: called CC=12 SCAR=25.8
Pod documented
330 10 my $self = shift;
331 10 my $type = shift;
332 10 my $codename = $self->_code_restore_pos();
333 10 ▶ $codename = " ($codename)" if $codename ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $codename ne "" 0 10 10
334  
335 10 ▶ croak "write while stream opened readonly"
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "write" and $self->mode eq "ro" 0 10 10
Condition66%2/3
$type eq "write" and $self->mode eq "ro"
!ll&&!rl&&r
730
MC/DC0%0/2
$type eq "write" and $self->mode eq "ro"
$type eq "write" $self->mode eq "ro"
Truth table66%2/3
A and B
A= $type eq "write"B= $self->mode eq "ro"
ABresult
0X0
100
111
336 if ($type eq 'write') && ($self->mode eq 'ro');
337 10 ▶ croak "read while stream opened writeonly"
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "read" and $self->mode eq "wo" 0 10 10
Condition66%2/3
$type eq "read" and $self->mode eq "wo"
!ll&&!rl&&r
640
MC/DC0%0/2
$type eq "read" and $self->mode eq "wo"
$type eq "read" $self->mode eq "wo"
Truth table66%2/3
A and B
A= $type eq "read"B= $self->mode eq "wo"
ABresult
0X0
100
111
338 if ($type eq 'read') && ($self->mode eq 'wo');
339  
340 10 ▶ if ($self->writing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->writing) { } 7 3 10
341 7 ▶ if ($type eq 'rewind') { croak "rewind while writing$codename"; }
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'rewind') { } 1 6 7
elsif ($type eq 'read') { } 4 2 6
elsif ($type eq 'skip') { } 1 1 2
elsif ($type eq 'exhausted') { } 1 0 1
342 4 elsif ($type eq 'read' ) { croak "read while writing$codename"; }
343 1 elsif ($type eq 'skip' ) { croak "skip while writing$codename"; }
344 1 elsif ($type eq 'exhausted') { croak "exhausted while writing$codename"; }
345 } else {
346 3 ▶ if ($type eq 'write' ) { croak "write while reading$codename"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "write") 3 0 3
347 }
348 0 croak "Mode error$codename: $type";
349 }
350 119 ▶sub error_code {
Subroutine error_code: called CC=15 SCAR=33.5
Pod documented
351 119 my $self = shift;
352 119 my $type = shift;
353 119 my $text = shift;
354 119 ▶ if ($type eq 'zeroval') { # Implied text
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "zeroval") 54 65 119
355 54 $type = 'value';
356 54 $text = 'value must be >= 0';
357 }
358 119 ▶ if ($type eq 'range') { # Range is given the value, the min, and the max
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "range") 0 119 119
359 0 $type = 'value';
360 0 ▶ if (!defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $text) { } 0 0 0
361 0 $text = 'value out of range';
362 } else {
363 0 my $min = shift;
364 0 my $max = shift;
365 0 $text = "value $text out of range";
366 0 ▶ $text .= " $min - $max" if defined $min && defined $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and defined $max 0 0 0
Condition0%0/3
defined $min and defined $max
!ll&&!rl&&r
000
MC/DC0%0/2
defined $min and defined $max
defined $min defined $max
Truth table0%0/3
A and B
A= defined $minB= defined $max
ABresult
0X0
100
111
367 }
368 }
369 119 my $codename = $self->_code_restore_pos();
370 119 my $trailer = '';
371 119 ▶ $trailer .= " ($codename)" if $codename ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $codename ne "" 47 72 119
372 119 ▶ $trailer .= ": $text" if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 78 41 119
373 119 ▶ if ($type eq 'param') { croak "invalid parameters$trailer"; }
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'param') { } 17 102 119
elsif ($type eq 'value') { } 54 48 102
elsif ($type eq 'string') { } 0 48 48
elsif ($type eq 'base') { } 7 41 48
elsif ($type eq 'overflow') { } 40 1 41
elsif ($type eq 'short') { } 1 0 1
elsif ($type eq 'assert') { } 0 0 0
374 54 elsif ($type eq 'value') { croak "invalid value$trailer"; }
375 0 elsif ($type eq 'string') { croak "invalid string$trailer"; }
376 7 elsif ($type eq 'base') { croak "code error: invalid base$trailer";}
377 40 elsif ($type eq 'overflow'){croak "code error: overflow$trailer";}
378 1 elsif ($type eq 'short') { croak "short read$trailer"; }
379 0 elsif ($type eq 'assert') { confess "internal assert$trailer"; }
380 0 else { confess "Unknown error$trailer"; }
381 }
382  
383  
384 ####### Combination functions
385 20044 ▶sub erase_for_write {
Subroutine erase_for_write: called CC=2 SCAR=6.9
Pod documented
386 20044 my $self = shift;
387 20044 $self->erase;
388 20044 ▶ $self->write_open if !$self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->writing 19760 284 20044
389 }
390 29547 ▶sub rewind_for_read {
Subroutine rewind_for_read: called CC=2 SCAR=6.9
Pod documented
391 29547 my $self = shift;
392 29547 ▶ $self->write_close if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 21017 8530 29547
393 29547 $self->rewind;
394 }
395  
396  
397 6 ▶sub readahead {
Subroutine readahead: called CC=1 SCAR=0.0
Pod documented
398 6 my $self = shift;
399 6 my $bits = shift;
400 6 $self->read($bits, 'readahead');
401 }
402 0 ▶sub read { # You need to implement this.
Subroutine read: not called CC=1 SCAR=6.9
Pod documented
403 0 confess "The read method has not been implemented!";
404 }
405 0 ▶sub write { # You need to implement this.
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
406 0 confess "The write method has not been implemented!";
407 }
408  
409  
410 5916 ▶sub put_unary {
Subroutine put_unary: called CC=5 SCAR=17.5
Pod documented
411 5916 my $self = shift;
412  
413 5916 foreach my $val (@_) {
414 6290 ▶ $self->error_code('zeroval') unless defined $val and $val >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val >= 0 0 6290 6290
Condition33%1/3
defined $val and $val >= 0
!ll&&!rl&&r
006290
MC/DC0%0/2
defined $val and $val >= 0
defined $val $val >= 0
Truth table33%1/3
A and B
A= defined $valB= $val >= 0
ABresult
0X0
100
111
415 6290 ▶ warn "Trying to write large unary value ($val)" if $val > 10_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if $val > 10000000 0 6290 6290
416  
417 # Since the write routine is allowed to take any number of bits when
418 # writing 0 and 1, this works, and is very fast.
419 6290 $self->write($val+1, 1);
420  
421 # Alternate implementation, much slower for large values:
422 #
423 # if ($val < maxbits) {
424 # $self->write($val+1, 1);
425 # } else {
426 # my $nbits = $val % maxbits;
427 # my $nwords = ($val-$nbits) / maxbits;
428 # $self->write(maxbits, 0) for (1 .. $nwords);
429 # $self->write($nbits+1, 1);
430 # }
431 }
432 5916 1;
433 }
434 33456 ▶sub get_unary { # You ought to override this.
Subroutine get_unary: called CC=10 SCAR=23.0
Pod documented
435 33456 my $self = shift;
436 33456 ▶ $self->error_stream_mode('read') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 0 33456 33456
437 33456 my $count = shift;
438 33456 ▶ if (!defined $count) { $count = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $count) { } 33443 13 33456
elsif ($count < 0) { } 9 4 13
elsif ($count == 0) { } 2 2 4
439 9 elsif ($count < 0) { $count = ~0; } # Get everything
440 2 elsif ($count == 0) { return; }
441  
442 33454 my @vals;
443 33454 $self->code_pos_start('Unary');
444 33454 ▶ while ($count-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count-- > 0) 33854 33290 67144
445 33854 $self->code_pos_set;
446 33854 my $val = 0;
447  
448 # Simple code:
449 #
450 # my $maxval = $len - $pos - 1; # Maximum unary value in remaining space
451 # $val++ while ( ($val <= $maxval) && ($self->read(1) == 0) );
452 # die "read off end of stream" if $pos >= $len;
453 #
454 # Faster code, looks at 32 bits at a time. Still comparatively slow.
455  
456 33854 my $word = $self->read(maxbits, 'readahead');
457 33854 ▶ last unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 164 33690 33854
458 33690 ▶ while ($word == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word == 0) 8265 33690 41955
459 8265 $val += maxbits;
460 8265 $self->skip(maxbits);
461 8265 $word = $self->read(maxbits, 'readahead');
462 }
463 33690 ▶ while (($word >> (maxbits-1) & 1) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($word >> &maxbits() - 1 & 1) == 0) 207395 33690 241085
464 207395 $val++;
465 207395 $word <<= 1;
466 }
467 33690 my $nbits = $val % maxbits;
468 33690 $self->skip($nbits + 1);
469  
470 33690 push @vals, $val;
471 }
472 33454 $self->code_pos_end;
473 33454 ▶ wantarray ? @vals : $vals[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 33444 33454
474 }
475  
476 # Write unary as 1111.....0 instead of 0000.....1
477 8744 ▶sub put_unary1 {
Subroutine put_unary1: called CC=7 SCAR=19.5
Pod documented
478 8744 my $self = shift;
479  
480 8744 foreach my $val (@_) {
481 9686 ▶ $self->error_code('zeroval') unless defined $val and $val >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val >= 0 2 9684 9686
Condition100%3/3
defined $val and $val >= 0
!ll&&!rl&&r
119684
MC/DC100%2/2
defined $val and $val >= 0
defined $val $val >= 0
Truth table100%3/3
A and B
A= defined $valB= $val >= 0
ABresult
0X0
100
111
482 9684 ▶ warn "Trying to write large unary value ($val)" if $val > 10_000_000;
Branch: 1 branch
BranchTrueFalseTotal
if $val > 10000000 0 9684 9684
483 9684 ▶ if ($val < maxbits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val < &maxbits()) { } 8887 797 9684
484 8887 $self->write($val+1, maxval() << 1);
485 } else {
486 797 my $nbits = $val % maxbits;
487 797 my $nwords = ($val-$nbits) / maxbits();
488 797 $self->write(maxbits, maxval) for (1 .. $nwords);
489 797 $self->write($nbits+1, maxval() << 1);
490 }
491 }
492 8742 1;
493 }
494 8968 ▶sub get_unary1 { # You ought to override this.
Subroutine get_unary1: called CC=10 SCAR=23.2
Pod documented
495 8968 my $self = shift;
496 8968 ▶ $self->error_stream_mode('read') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 0 8968 8968
497 8968 my $count = shift;
498 8968 ▶ if (!defined $count) { $count = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $count) { } 8953 15 8968
elsif ($count < 0) { } 15 0 15
elsif ($count == 0) { } 0 0 0
499 15 elsif ($count < 0) { $count = ~0; } # Get everything
500 0 elsif ($count == 0) { return; }
501  
502 8968 my @vals;
503 8968 $self->code_pos_start('Unary1');
504 8968 ▶ while ($count-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count-- > 0) 9925 8877 18802
505 9925 $self->code_pos_set;
506 9925 my $val = 0;
507  
508 # Simple code:
509 #
510 # my $maxval = $len - $pos - 1; # Maximum unary value in remaining space
511 # $val++ while ( ($val <= $maxval) && ($self->read(1) == 0) );
512 # die "read off end of stream" if $pos >= $len;
513 #
514 # Faster code, looks at 32 bits at a time. Still comparatively slow.
515  
516 9925 my $word = $self->read(maxbits, 'readahead');
517 9925 ▶ last unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 82 9843 9925
518 9843 ▶ while ($word == maxval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word == &maxval()) 13011 9843 22854
519 13011 $val += maxbits;
520 13011 $self->skip(maxbits);
521 13011 $word = $self->read(maxbits, 'readahead');
522 }
523 9843 ▶ while (($word >> (maxbits-1) & 1) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($word >> &maxbits() - 1 & 1) != 0) 140798 9843 150641
524 140798 $val++;
525 140798 $word <<= 1;
526 }
527 9843 my $nbits = $val % maxbits;
528 9843 $self->skip($nbits + 1);
529  
530 9834 push @vals, $val;
531 }
532 8959 $self->code_pos_end;
533 8959 ▶ wantarray ? @vals : $vals[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 8945 8959
534 }
535  
536 # binary values of given length
537 817 ▶sub put_binword {
Subroutine put_binword: called CC=6 SCAR=20.3
Pod documented
538 817 my $self = shift;
539 817 my $bits = shift;
540 817 ▶ $self->error_code('param', "bits must be in range 0-" . maxbits)
Branch: 1 branch
BranchTrueFalseTotal
if $bits <= 0 or $bits > &maxbits() 0 817 817
Condition33%1/3
$bits <= 0 or $bits > &maxbits()
l!l&&r!l&&!r
00817
MC/DC0%0/2
$bits <= 0 or $bits > &maxbits()
$bits <= 0 $bits > &maxbits()
Truth table33%1/3
A or B
A= $bits <= 0B= $bits > &maxbits()
ABresult
1X1
011
000
541 if ($bits <= 0) || ($bits > maxbits);
542  
543 817 foreach my $val (@_) {
544 3493 ▶ $self->error_code('zeroval') unless defined $val and $val >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val >= 0 0 3493 3493
Condition33%1/3
defined $val and $val >= 0
!ll&&!rl&&r
003493
MC/DC0%0/2
defined $val and $val >= 0
defined $val $val >= 0
Truth table33%1/3
A and B
A= defined $valB= $val >= 0
ABresult
0X0
100
111
545 3493 $self->write($bits, $val);
546 }
547 817 1;
548 }
549 842 ▶sub get_binword {
Subroutine get_binword: called CC=10 SCAR=23.3
Pod documented
550 842 my $self = shift;
551 842 ▶ $self->error_stream_mode('read') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 0 842 842
552 842 my $bits = shift;
553 842 ▶ $self->error_code('param', "bits must be in range 0-" . maxbits)
Branch: 1 branch
BranchTrueFalseTotal
if $bits <= 0 or $bits > &maxbits() 3 839 842
Condition100%3/3
$bits <= 0 or $bits > &maxbits()
l!l&&r!l&&!r
21839
MC/DC100%2/2
$bits <= 0 or $bits > &maxbits()
$bits <= 0 $bits > &maxbits()
Truth table100%3/3
A or B
A= $bits <= 0B= $bits > &maxbits()
ABresult
1X1
011
000
554 if ($bits <= 0) || ($bits > maxbits);
555 839 my $count = shift;
556 839 ▶ if (!defined $count) { $count = 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $count) { } 815 24 839
elsif ($count < 0) { } 24 0 24
elsif ($count == 0) { } 0 0 0
557 24 elsif ($count < 0) { $count = ~0; } # Get everything
558 0 elsif ($count == 0) { return; }
559  
560 839 my @vals;
561 839 ▶ while ($count-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count-- > 0) 3539 813 4352
562 3539 my $val = $self->read($bits);
563 3537 ▶ last unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 24 3513 3537
564 3513 push @vals, $val;
565 }
566 837 ▶ wantarray ? @vals : $vals[-1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 813 837
567 }
568  
569  
570 # Write one or more text binary strings (e.g. '10010')
571 5064 ▶sub put_string {
Subroutine put_string: called CC=8 SCAR=21.1
Pod documented
572 5064 my $self = shift;
573 5064 ▶ $self->error_stream_mode('write') unless $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->writing 0 5064 5064
574  
575 5064 foreach my $str (@_) {
576 5064 ▶ next unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 5064 5064
577 5064 ▶ $self->error_code('string') if $str =~ tr/01//c;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ tr/01//c 0 5064 5064
578 5064 my $bits = length($str);
579 5064 ▶ next unless $bits > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $bits > 0 0 5064 5064
580  
581 5064 my $spos = 0;
582 5064 ▶ while ($bits >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 32) 3051 5064 8115
583 3051 $self->write(32, oct('0b' . substr($str, $spos, 32)));
584 3051 $spos += 32;
585 3051 $bits -= 32;
586 }
587 5064 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 4927 137 5064
588 4927 $self->write($bits, oct('0b' . substr($str, $spos, $bits)));
589 }
590 }
591 5064 1;
592 }
593 # Get a text binary string. Similar to read, but bits can be 0 - len.
594 2880 ▶sub read_string {
Subroutine read_string: called CC=7 SCAR=19.5
Pod documented
595 2880 my $self = shift;
596 2880 ▶ $self->error_stream_mode('read') if $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->writing 1 2879 2880
597 2879 my $bits = shift;
598 2879 ▶ $self->error_code('param', "bits must be >= 0") unless defined $bits && $bits >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bits and $bits >= 0 1 2878 2879
Condition66%2/3
defined $bits and $bits >= 0
!ll&&!rl&&r
012878
MC/DC50%1/2
defined $bits and $bits >= 0
defined $bits $bits >= 0
Truth table66%2/3
A and B
A= defined $bitsB= $bits >= 0
ABresult
0X0
100
111
599 2878 ▶ $self->error_code('short') unless $bits <= ($self->len - $self->pos);
Branch: 1 branch
BranchTrueFalseTotal
unless $bits <= $self->len - $self->pos 1 2877 2878
600 2877 my $str = '';
601 2877 ▶ while ($bits >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 32) 1542 2877 4419
602 1542 $str .= unpack("B32", pack("N", $self->read(32)));
603 1542 $bits -= 32;
604 }
605 2877 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 2782 95 2877
606 2782 $str .= substr(unpack("B32", pack("N", $self->read($bits))), -$bits);
607 }
608 2877 $str;
609 }
610  
611 # Conversion to and from strings of 0's and 1's. Note that the order is
612 # completely left to right based on what was written.
613  
614 2873 ▶sub to_string { # You should override this.
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
615 2873 my $self = shift;
616 2873 $self->rewind_for_read;
617 2873 $self->read_string($self->len);
618 }
619 2866 ▶sub from_string { # You should override this.
Subroutine from_string: called CC=1 SCAR=0.0
Pod documented
620 2866 my $self = shift;
621 #my $str = shift;
622 #my $bits = shift || length($str);
623 2866 $self->erase_for_write;
624 2866 $self->put_string($_[0]);
625 2866 $self->rewind_for_read;
626 }
627  
628 # Conversion to and from binary. Note that the order is completely left to
629 # right based on what was written. This means it is an array of big-endian
630 # units. This implementation uses 32-bit words as the units.
631  
632 3 ▶sub to_raw { # You ought to override this.
Subroutine to_raw: called CC=3 SCAR=11.0
Pod documented
633 3 my $self = shift;
634 3 $self->rewind_for_read;
635 3 my $len = $self->len;
636 3 my $pos = $self->pos;
637 3 my $vec = '';
638 3 ▶ while ( ($pos+31) < $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 31 < $len) 1296 3 1299
639 1296 $vec .= pack("N", $self->read(32));
640 1296 $pos += 32;
641 }
642 3 ▶ if ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 3 0 3
643 3 $vec .= pack("N", $self->read($len-$pos) << 32-($len-$pos));
644 }
645 3 $vec;
646 }
647 2 ▶sub put_raw { # You ought to override this.
Subroutine put_raw: called CC=5 SCAR=16.3
Pod documented
648 2 my $self = shift;
649 2 ▶ $self->error_stream_mode('write') unless $self->writing;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->writing 0 2 2
650  
651 2 my $vec = shift;
652 2 ▶ my $bits = shift || int((length($vec)+7)/8);
Condition33%1/3
shift() || int((length($vec) + 7) / 8)
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || int((length($vec) + 7) / 8)
shift() int((length($vec) + 7) / 8)
Truth table33%1/3
A || B
A= shift()B= int((length($vec) + 7) / 8)
ABresult
1X1
011
000
653  
654 2 my $vpos = 0;
655 2 ▶ while ($bits >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 32) 1062 2 1064
656 1062 $self->write(32, unpack("N", substr($vec, $vpos, 4)));
657 1062 $vpos += 4;
658 1062 $bits -= 32;
659 }
660 2 ▶ if ($bits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 0) 2 0 2
661 2 my $nbytes = int(($bits+7)/8); # this many bytes left
662 2 my $pvec = substr($vec, $vpos, $nbytes); # extract the bytes
663 2 vec($pvec,33,1) = 0; # zero fill the 32-bit word
664 2 my $word = unpack("N", $pvec); # unpack the filled word
665 2 $word >>= (32-$bits); # shift data to lower bits
666 2 $self->write($bits, $word); # write data to stream
667 }
668 2 1;
669 }
670 5 ▶sub from_raw {
Subroutine from_raw: called CC=2 SCAR=7.1
Pod documented
671 5 my $self = shift;
672 5 my $vec = shift;
673 5 ▶ my $bits = shift || int((length($vec)+7)/8);
Condition33%1/3
shift() || int((length($vec) + 7) / 8)
l!l&&r!l&&!r
500
MC/DC0%0/2
shift() || int((length($vec) + 7) / 8)
shift() int((length($vec) + 7) / 8)
Truth table33%1/3
A || B
A= shift()B= int((length($vec) + 7) / 8)
ABresult
1X1
011
000
674 5 $self->erase_for_write;
675 5 $self->put_raw($vec, $bits);
676 5 $self->rewind_for_read;
677 }
678  
679 # Conversion to and from your internal data. This can be in any form desired.
680 # This could be a little-endian array, or a byte stream, or a string, etc.
681 # The main point is that we can get a single chunk that can be saved off, and
682 # later can restore the stream. This should be efficient.
683  
684 3 ▶sub to_store { # You ought to implement this.
Subroutine to_store: called CC=1 SCAR=0.0
Pod documented
685 3 my $self = shift;
686 3 $self->to_raw(@_);
687 }
688 3 ▶sub from_store { # You ought to implement this.
Subroutine from_store: called CC=1 SCAR=0.0
Pod documented
689 3 my $self = shift;
690 3 $self->from_raw(@_);
691 }
692  
693 # Takes a stream and inserts its contents into the current stream.
694 # Non-destructive to both streams.
695 6 ▶sub put_stream {
Subroutine put_stream: called CC=3 SCAR=11.8
Pod documented
696 6 my $self = shift;
697 6 my $source = shift;
698 6 ▶ return 0 unless defined $source && $source->can('to_string');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source and $source->can("to_string") 0 6 6
Condition33%1/3
defined $source and $source->can("to_string")
!ll&&!rl&&r
006
MC/DC0%0/2
defined $source and $source->can("to_string")
defined $source $source->can("to_string")
Truth table33%1/3
A and B
A= defined $sourceB= $source->can("to_string")
ABresult
0X0
100
111
699  
700 # In an implementation, you could check if ref $source eq __PACKAGE__
701 # and do something special. BLVec / XS does this.
702  
703 # This is reasonably fast for most implementations.
704 6 $self->put_string($source->to_string);
705 # In theory this could be faster. Since all the implementations have custom
706 # string code, and none have custom raw code, it's currently slower.
707 # $self->put_raw($source->to_raw, $source->len);
708 6 1;
709 }
710  
711  
712  
713 # Helper class methods for other functions
714 0 ▶sub _floorlog2 {
Subroutine _floorlog2: not called CC=2 SCAR=17.9
715 0 my $d = shift;
716 0 my $base = 0;
717 0 ▶ $base++ while ($d >>= 1);
Branch: 1 branch
BranchTrueFalseTotal
$d >>= 1 and do { ++$base } 0 0 0
718 0 $base;
719 }
720 0 ▶sub _ceillog2 {
Subroutine _ceillog2: not called CC=2 SCAR=17.9
721 0 my $d = shift;
722 0 $d--;
723 0 my $base = 1;
724 0 ▶ $base++ while ($d >>= 1);
Branch: 1 branch
BranchTrueFalseTotal
$d >>= 1 and do { ++$base } 0 0 0
725 0 $base;
726 }
727 0 ▶sub _bin_to_dec {
Subroutine _bin_to_dec: not called CC=1 SCAR=0.2
728 28 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
729 0 oct '0b' . substr($_[1], 0, $_[0]);
730 }
731 0 ▶sub _dec_to_bin {
Subroutine _dec_to_bin: not called CC=2 SCAR=17.9
732 # The following is typically fastest with 5.9.2 and later:
733 #
734 # scalar reverse unpack("b$bits",($bits>32) ? pack("Q>",$v) : pack("V",$v));
735 #
736 # With 5.9.2 and later on a 64-bit machine, this will work quickly:
737 #
738 # substr(unpack("B64", pack("Q>", $v)), -$bits);
739 #
740 # This is the best compromise that works with 5.8.x, BE/LE, and 32-bit:
741 0 my $bits = shift;
742 0 my $v = shift;
743 0 ▶ if ($bits > 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits > 32) { } 0 0 0
744 # return substr(unpack("B64", pack("Q>", $v)), -$bits); # needs v5.9.2
745 0 return substr(unpack("B32", pack("N", $v>>32)), -($bits-32))
746 . unpack("B32", pack("N", $v));
747 } else {
748 # return substr(unpack("B32", pack("N", $v)), -$bits); # slower
749 0 return scalar reverse unpack("b$bits", pack("V", $v));
750 }
751 }
752  
753 28 ▶no Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
754 28 1;
755  
756  
757 # ABSTRACT: A Role implementing the API for Data::BitStream
758  
759 =pod
760  
761 =head1 NAME
762  
763 Data::BitStream::Base - A Role implementing the API for Data::BitStream
764  
765 =head1 SYNOPSIS
766  
767 use Moo;
768 with 'Data::BitStream::Base';
769  
770 =head1 DESCRIPTION
771  
772 A role written for L<Data::BitStream> that provides the basic API, including
773 generic code for almost all functionality.
774  
775 This is used by particular implementations such as L<Data::BitStream::String>
776 and L<Data::BitStream::WordVec>.
777  
778  
779  
780  
781 =head2 DATA
782  
783 =over 4
784  
785 =item B< pos >
786  
787 A read-only non-negative integer indicating the current position in a read
788 stream. It is advanced by C<read>, C<get>, and C<skip> methods, as well
789 as changed by C<to>, C<from>, C<rewind>, and C<erase> methods.
790  
791 =item B< len >
792  
793 A read-only non-negative integer indicating the current length of the stream
794 in bits. It is advanced by C<write> and C<put> methods, as well as changed
795 by C<from> and C<erase> methods.
796  
797 =item B< writing >
798  
799 A read-only boolean indicating whether the stream is open for writing or
800 reading. Methods for read such as
801 C<read>, C<get>, C<skip>, C<rewind>, C<skip>, and C<exhausted>
802 are not allowed while writing. Methods for write such as
803 C<write> and C<put>
804 are not allowed while reading.
805  
806 The C<write_open> and C<erase_for_write> methods will set writing to true.
807 The C<write_close> and C<rewind_for_read> methods will set writing to false.
808  
809 The read/write distinction allows implementations more freedom in internal
810 caching of data. For instance, they can gather writes into blocks. It also
811 can be helpful in catching mistakes such as reading from a target stream.
812  
813 =item B< mode >
814  
815 The stream mode. Especially useful when given a file. The mode may be one of
816  
817 r (read)
818 ro (readonly)
819 w (write)
820 wo (writeonly)
821 rdwr (readwrite)
822 a (append)
823  
824 =item B< file >
825  
826 The name of a file to read or write (depending on the mode).
827  
828 =item B< fheaderlines >
829  
830 Only applicible when reading a file. Indicates how many header lines exist
831 before the data.
832  
833 =item B< fheader >
834  
835 When writing a file, this is the header to write before the data.
836 When reading a file, this will be set to the header, if fheaderlines was given.
837  
838 =back
839  
840  
841  
842  
843 =head2 CLASS METHODS
844  
845 =over 4
846  
847 =item B< maxbits >
848  
849 Returns the number of bits in a word, which is the largest allowed size of
850 the C<bits> argument to C<read> and C<write>. This will be either 32 or 64.
851  
852 =item B< maxval >
853  
854 Returns the maximum value we can handle. This should be C< 2 ** maxbits - 1 >,
855 or C< 0xFFFF_FFFF > for 32-bit, and C< 0xFFFF_FFFF_FFFF_FFFF > for 64-bit.
856  
857 =back
858  
859  
860  
861  
862 =head2 OBJECT METHODS (I<reading>)
863  
864 These methods are only valid while the stream is in reading state.
865  
866 =over 4
867  
868 =item B< rewind >
869  
870 Moves the position to the stream beginning.
871  
872 =item B< exhausted >
873  
874 Returns true is the stream is at the end. Rarely used.
875  
876 =item B< read($bits [, 'readahead']) >
877  
878 Reads C<$bits> from the stream and returns the value.
879 C<$bits> must be between C<1> and C<maxbits>.
880  
881 Returns undef if the current position is at the end of the stream.
882  
883 Croaks with an off stream error if not enough bits are left in the stream.
884  
885 The position is advanced unless the second argument is the string 'readahead'.
886  
887 I<Note for implementations>: You have to implement this.
888  
889 =item B< readahead($bits>) >
890  
891 Identical to calling read with 'readahead' as the second argument.
892 Returns the value of the next C<$bits> bits (between C<1> and C<maxbits>).
893 Returns undef if the current position is at the end.
894 Allows reading past the end of the stream (fills with zeros as necessary).
895 Does not advance the position.
896  
897 =item B< skip($bits) >
898  
899 Advances the position C<$bits> bits.
900 Typically used in conjunction with C<readahead>.
901  
902 =item B< get_unary([$count]) >
903  
904 Reads one or more values from the stream in C<0000...1> unary coding.
905 If C<$count> is C<1> or not supplied, a single value will be read.
906 If C<$count> is positive, that many values will be read.
907 If C<$count> is negative, values are read until the end of the stream.
908  
909 In list context this returns a list of all values read. In scalar context
910 it returns the last value read.
911  
912 I<Note for implementations>: You should have efficient code for this.
913  
914 =item B< get_unary1([$count]) >
915  
916 Like C<get_unary>, but using C<1111...0> unary coding. Less common.
917  
918 =item B< get_binword($bits, [$count]) >
919  
920 Reads one or more values from the stream as fixed-length binary numbers, each
921 using C<$bits> bits. The treatment of count and return values is identical to
922 C<get_unary>.
923  
924 =item B< read_string($bits) >
925  
926 Reads C<$bits> bits from the stream and returns them as a binary string, such
927 as '0011011'.
928  
929 =back
930  
931  
932  
933  
934 =head2 OBJECT METHODS (I<writing>)
935  
936 These methods are only valid while the stream is in writing state.
937  
938 =over 4
939  
940 =item B< write($bits, $value) >
941  
942 Writes C<$value> to the stream using C<$bits> bits.
943 C<$bits> must be between C<1> and C<maxbits>, unless C<value> is 0 or 1, in
944 which case C<bits> may be larger than C<maxbits>.
945  
946 The stream length will be increased by C<$bits> bits.
947 Regardless of the contents of C<$value>, exactly C<$bits> bits will be used.
948 If C<$value> has more non-zero bits than C<$bits>, the lower bits are written.
949 In other words, C<$value> will be masked before writing.
950  
951 I<Note for implementations>: You have to implement this.
952  
953 =item B< put_unary(@values) >
954  
955 Writes the values to the stream in C<0000...1> unary coding.
956 Unary coding is only appropriate for relatively small numbers, as it uses
957 C<$value + 1> bits.
958  
959 I<Note for implementations>: You should have efficient code for this.
960  
961 =item B< put_unary1(@values) >
962  
963 Like C<put_unary>, but using C<1111...0> unary coding. Less common.
964  
965 =item B< put_binword($bits, @values) >
966  
967 Writes the values to the stream as fixed-length binary values. This is just
968 a loop inserting each value with C<write($bits, $value)>.
969  
970 =item B< put_string(@strings) >
971  
972 Takes one or more binary strings, such as '1001101', '001100', etc. and
973 writes them to the stream. The number of bits used for each value is equal
974 to the string length.
975  
976 =item B< put_raw($packed, [, $bits]) >
977 Writes the packed big-endian vector C<$packed> which has C<$bits> bits of data.
978 If C<$bits> is not present, then C<length($packed)> will be used as the
979 byte-length. It is recommended that you include C<$bits>.
980  
981 =item B< put_stream($source_stream) >
982  
983 Writes the contents of C<$source_stream> to the stream. This is a helper
984 method that might be more efficient than doing it in one of the many other
985 possible ways. Some functionally equivalent methods:
986  
987 $self->put_string( $source_stream->to_string ); # The default for put_stream
988  
989 $self->put_raw( $source_stream->to_raw, $source_stream->len );
990  
991 my $bits = $source_stream->len;
992 $source_stream->rewind_for_read;
993 while ($bits > 0) {
994 my $wbits = ($bits >= 32) ? 32 : $bits;
995 $self->write($wbits, $source_stream->read($wbits));
996 $bits -= $wbits;
997 }
998  
999 =back
1000  
1001  
1002  
1003  
1004 =head2 OBJECT METHODS (I<conversion>)
1005  
1006 These methods may be called at any time, and will adjust the state of the
1007 stream.
1008  
1009 =over 4
1010  
1011 =item B< to_string >
1012  
1013 Returns the stream as a binary string, e.g. '00110101'.
1014  
1015 =item B< to_raw >
1016  
1017 Returns the stream as packed big-endian data. This form is portable to
1018 any other implementation on any architecture.
1019  
1020 =item B< to_store >
1021  
1022 Returns the stream as some scalar holding the data in some implementation
1023 specific way. This may be portable or not, but it can always be read by
1024 the same implementation. It might be more efficient than the raw format.
1025  
1026  
1027 =item B< from_string($string) >
1028  
1029 The stream will be set to the binary string C<$string>.
1030  
1031 =item B< from_raw($packed [, $bits]) >
1032  
1033 The stream is set to the packed big-endian vector C<$packed> which has
1034 C<$bits> bits of data. If C<$bits> is not present, then C<length($packed)>
1035 will be used as the byte-length. It is recommended that you include C<$bits>.
1036  
1037 =item B< from_store($blob [, $bits]) >
1038  
1039 Similar to C<from_raw>, but using the value returned by C<to_store>.
1040  
1041 =back
1042  
1043  
1044  
1045  
1046 =head2 OBJECT METHODS (I<other>)
1047  
1048 =over 4
1049  
1050 =item B< erase >
1051  
1052 Erases all the data, while the writing state is left unchanged. The position
1053 and length will both be 0 after this is finished.
1054  
1055 I<Note for implementations>: You need an 'after' method to actually erase the data.
1056  
1057 =item B< read_open >
1058  
1059 Reads the current input file, if one exists.
1060  
1061 =item B< write_open >
1062  
1063 Changes the state to writing with no other API-visible changes.
1064  
1065 =item B< write_close >
1066  
1067 Changes the state to reading, and the position is set to the end of the
1068 stream. No other API-visible changes happen.
1069  
1070 =item B< erase_for_write >
1071  
1072 A helper function that performs C<erase> followed by C<write_open>.
1073  
1074 =item B< rewind_for_read >
1075  
1076 A helper function that performs C<write_close> followed by C<rewind>.
1077  
1078 =back
1079  
1080  
1081  
1082  
1083 =head2 INTERNAL METHODS
1084  
1085 These methods are used by roles.
1086 As a stream user you should not be using these.
1087  
1088 =over 4
1089  
1090 =item B< code_pos_start >
1091  
1092 =item B< code_pos_end >
1093  
1094 =item B< code_pos_set >
1095  
1096 Used to handle exceptions for codes that call other codes. Generally used
1097 in C< get_* > methods. The primary reasoning for this is that we want to
1098 unroll the stream location back to where the caller tried to read the code
1099 on an error. That way they can try again with a different code, or examine
1100 the bits that resulted in an incorrect code.
1101 C<code_pos_start> starts a new stack entry, C<code_pos_set> sets the start
1102 of the current code so we know where to go back to, and C<code_pos_end>
1103 indicates we're done so the code stack entry can be removed.
1104  
1105 =item B< code_pos_is_set >
1106  
1107 Returns the code stack or C<undef> if not in a code. This should always be
1108 C<undef> for users. If it is not, it means some code routine finished
1109 abnormally and didn't remove their error stack.
1110  
1111 =item B< error_off_stream >
1112  
1113 Croaks with a message about reading or skipping off the stream. If this
1114 happens inside a C<get_> method, it should indicate the outermost code that
1115 was used. The stream position is restored to the start of the outer code.
1116  
1117 =item B< error_stream_mode >
1118  
1119 Croaks with a message about the wrong mode being used. This is what happens
1120 when an attempt is made to write to a stream opened for reading, or read from
1121 a stream opened for writing.
1122  
1123 =item B< error_code >
1124  
1125 Central routine that captures code errors, including incorrect parameters,
1126 values out of range, overflows, range errors, etc. All errors cause a croak
1127 except assertions, which will confess (since they indicate a serious internal
1128 issue). Some additional information is also included if possible (e.g. the
1129 outermost code being used, the allowed range, the value, etc.).
1130  
1131 =back
1132  
1133  
1134  
1135  
1136 =head1 SEE ALSO
1137  
1138 =over 4
1139  
1140 =item L<Data::BitStream>
1141  
1142 =item L<Data::BitStream::String>
1143  
1144 =item L<Data::BitStream::WordVec>
1145  
1146 =back
1147  
1148 =head1 AUTHORS
1149  
1150 Dana Jacobsen E<lt>dana@acm.orgE<gt>
1151  
1152 =head1 COPYRIGHT
1153  
1154 Copyright 2011-2012 by Dana Jacobsen E<lt>dana@acm.orgE<gt>
1155  
1156 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1157  
1158 =cut
« BitStream/Code/Baer.pm ↑ Summary BitStream/Code/Omega.pm »