CAR/Utils.pm

filter: Statementstmt 94.7% 36 / 38 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 87.8% 58 / 66 CC 9 SCAR 16.4
CC
9
Cov
87%
decode_varint
8.6
encode_varint
2.0
BEGIN
1.0
CRAP 5.2
« CAR/CID.pm ↑ Summary CAR/v1.pm »
1 2 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 #
3 2 package Archive::CAR::Utils v0.0.4 {
4 2 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our @EXPORT_OK = qw[encode_varint decode_varint decode_cid systell];
6 #
7 151 ▶ sub systell ($fh) {
Subroutine systell: called CC=1 SCAR=0.0
Pod documented
8 151 return tell($fh);
9 }
10  
11 21 ▶ sub encode_varint ($val) {
Subroutine encode_varint: called CC=2 SCAR=6.9
Pod documented
12 21 my $out = '';
13 21 ▶ while ( $val >= 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val >= 128) 5 21 26
14 5 $out .= chr( ( $val & 0x7f ) | 0x80 );
15 5 $val >>= 7;
16 }
17 21 $out .= chr($val);
18 21 return $out;
19 }
20  
21 133 ▶ sub decode_varint ( $str_or_fh, $offset //= 0 ) {
Subroutine decode_varint: called CC=8 SCAR=21.5
Pod documented
22 2 ▶ use Scalar::Util qw[openhandle];
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Handles both scalar strings and filehandles
25 133 my $is_fh = openhandle($str_or_fh);
26 133 my ( $val, $shift, $bytes_read ) = ( 0, 0, 0 );
27 133 my $initial_offset = $offset;
28 133 while (1) {
29 139 my $byte_val;
30 139 ▶ if ($is_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_fh) { } 139 0 139
31 139 my $byte_char;
32 139 my $bytes_read_now = read( $str_or_fh, $byte_char, 1 );
33 139 ▶ return ( undef, 0 ) unless defined $bytes_read_now && $bytes_read_now == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes_read_now and $bytes_read_now == 1 3 136 139
Condition66%2/3
defined $bytes_read_now and $bytes_read_now == 1
!ll&&!rl&&r
03136
MC/DC50%1/2
defined $bytes_read_now and $bytes_read_now == 1
defined $bytes_read_now $bytes_read_now == 1
Truth table66%2/3
A and B
A= defined $bytes_read_nowB= $bytes_read_now == 1
ABresult
0X0
100
111
34 136 $byte_val = ord($byte_char);
35 }
36 else {
37 0 ▶ return ( undef, 0 ) unless $offset < length($str_or_fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $offset < length $str_or_fh 0 0 0
38 0 $byte_val = ord( substr( $str_or_fh, $offset++, 1 ) );
39 }
40 136 $bytes_read++;
41 136 $val |= ( $byte_val & 0x7f ) << $shift;
42 136 ▶ return ( $val, $bytes_read ) unless $byte_val & 0x80;
Branch: 1 branch
BranchTrueFalseTotal
unless $byte_val & 128 130 6 136
43 6 $shift += 7;
44 6 ▶ return ( undef, $bytes_read ) if $shift >= 64;
Branch: 1 branch
BranchTrueFalseTotal
if $shift >= 64 0 6 6
45 }
46 }
47  
48 28 ▶ sub decode_cid ($fh) {
Subroutine decode_cid: called CC=1 SCAR=0.0
Pod documented
49 28 require Archive::CAR::CID;
50 28 return Archive::CAR::CID->decode($fh);
51 }
52 };
53 #
54 2 1;
« CAR/CID.pm ↑ Summary CAR/v1.pm »