blib/lib/Number/Bytes/Human.pm

filter: Statementstmt 89.5% 171 / 191 Branchbran 81.9% 77 / 94 Conditioncond 82.7% 67 / 81 MC/DCmcdc 58.5% 24 / 41 Subroutinesub 84.2% 16 / 19 Podpod 100% 6 / 6 totaltotal 83.5% 361 / 432 CC 82 SCAR 32.1
CC
82
Cov
86%
_parse_args
42.1
_parse_bytes
22.3
_format_bytes
15.5
CRAP 24.7
↑ Summary
1 package Number::Bytes::Human;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 our $VERSION = '0.11';
7  
8 11 require Exporter;
9 11 our @ISA = qw(Exporter);
10 11 our @EXPORT_OK = qw(format_bytes parse_bytes);
11  
12 11 require POSIX;
13 11 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #my $DEFAULT_BLOCK = 1024;
16 #my $DEFAULT_ZERO = '0';
17 #my $DEFAULT_ROUND_STYLE = 'ceil';
18 11 my %DEFAULT_SUFFIXES = (
19 1024 => ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],
20 1000 => ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],
21 1024000 => ['', 'M', 'T', 'E', 'Y'],
22 si_1024 => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'],
23 si_1000 => ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
24 );
25 11 my @DEFAULT_PREFIXES = @{$DEFAULT_SUFFIXES{1024}};
26  
27 129 ▶sub _default_suffixes {
Subroutine _default_suffixes: called CC=4 SCAR=17.2
28 129 ▶ my $set = shift || 1024;
Condition50%1/2
shift() || 1024
l!l
1290
MC/DC0%0/1
shift() || 1024
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29 129 ▶ if (exists $DEFAULT_SUFFIXES{$set}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DEFAULT_SUFFIXES{$set}) 129 0 129
30 129 ▶ return @{$DEFAULT_SUFFIXES{$set}} if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 129 0 129
31 0 return [ @{$DEFAULT_SUFFIXES{$set}} ];
32 }
33 0 croak "unknown suffix set '$set'";
34 }
35  
36 my %ROUND_FUNCTIONS = (
37 127 ▶ ceil => sub { return POSIX::ceil($_[0] * (10 ** $_[1])) / 10**$_[1]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 0 ▶ floor => sub { return POSIX::floor($_[0] * (10 ** $_[1])) / 10**$_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 ▶ round => sub { return sprintf( "%." . ( $_[1] || 0 ) . "f", $_[0] ); },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] || 0
l!l
00
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
40 0 ▶ trunc => sub { return sprintf( "%d", $_[0] * (10 ** $_[1])) / 10**$_[1]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 # what about 'ceiling'?
42 11 );
43  
44 151 ▶sub _round_function {
Subroutine _round_function: called CC=2 SCAR=7.6
45 151 my $style = shift;
46 151 ▶ if (exists $ROUND_FUNCTIONS{$style}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ROUND_FUNCTIONS{$style}) 151 0 151
47 151 return $ROUND_FUNCTIONS{$style}
48 }
49 0 croak "unknown round style '$style'";
50 }
51  
52 # options
53 # block | block_size | base | bs => 1024 | 1000
54 # base_1024 | block_1024 | 1024 => $true
55 # base_1000 | block_1000 | 1000 => $true
56 #
57 # round_function => \&
58 # round_style => 'ceiling', 'round', 'floor', 'trunc'
59 #
60 # suffixes => 1024 | 1000 | si_1024 | si_1000 | 1024000 | \@
61 # si => 1
62 # unit => string (eg., 'B' | 'bps' | 'b')
63 #
64 # zero => '0' (default) | '-' | '0%S' | undef
65 #
66 #
67 # supress_point_zero | no_point_zero =>
68 # b_to_i => 1
69 # to_s => \&
70 #
71 # allow_minus => 0 | 1
72 # too_large => string
73 # quiet => 1 (supresses "too large number" warning)
74  
75  
76  
77 # PROBABLY CRAP:
78 # precision => integer
79  
80 # parsed options
81 # BLOCK => 1024 | 1000
82 # ROUND_STYLE => 'ceil', 'round', 'floor', 'trunc'
83 # ROUND_FUNCTION => \&
84 # SUFFIXES => \@
85 # ZERO =>
86 # SI => undef | 1 Parse SI compatible
87  
88  
89 =begin private
90  
91 $options = _parse_args($seed, $args)
92 $options = _parse_args($seed, arg1 => $val1, ...)
93  
94 $seed is undef or a hashref
95 $args is a hashref
96  
97 =end private
98  
99 =cut
100  
101 177 ▶sub _parse_args {
Subroutine _parse_args: called CC=41 SCAR=37.4
102 177 my $seed = shift;
103 177 my %args;
104  
105 my %options;
106 177 ▶ unless (defined $seed) { # use defaults
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seed) 149 28 177
107 149 $options{BLOCK} = 1024;
108 149 $options{ROUND_STYLE} = 'ceil';
109 149 $options{ROUND_FUNCTION} = _round_function($options{ROUND_STYLE});
110 149 $options{ZERO} = '0';
111 149 $options{SI} = undef;
112 149 $options{PRECISION} = 1;
113 149 $options{PRECISION_CUTOFF} = 1;
114 #$options{SUFFIXES} = # deferred to the last minute when we know BLOCK, seek [**]
115 149 $options{UNIT} = undef;
116 }
117 # else { %options = %$seed } # this is set if @_!=0, down below
118  
119 177 ▶ if (@_==0) { # quick return for default values (no customized args)
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 67 110 177
elsif (@_ == 1 and ref $_[0]) { } 27 83 110
120 67 ▶ return (defined $seed) ? $seed : \%options;
Branch: 1 branch
BranchTrueFalseTotal
defined $seed ? : 0 67 67
121 ▶ } elsif (@_==1 && ref $_[0]) { # \%args
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
83027
MC/DC50%1/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
122 27 %args = %{$_[0]};
123 } else { # arg1 => $val1, arg2 => $val2
124 83 %args = @_;
125 }
126  
127 # this is done here so this assignment/copy doesn't happen if @_==0
128 110 ▶ %options = %$seed unless %options;
Branch: 1 branch
BranchTrueFalseTotal
unless %options 28 82 110
129  
130 # block | block_size | base | bs => 1024 | 1000
131 # block_1024 | base_1024 | 1024 => $true
132 # block_1000 | base_1000 | 1024 => $true
133 110 ▶ if ($args{block} ||
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'block'} or $args{'block_size'} or $args{'base'} or $args{'bs'}) { } 76 34 110
elsif ($args{'block_1024'} or $args{'base_1024'} or $args{'1024'}) { } 3 31 34
elsif ($args{'block_1000'} or $args{'base_1000'} or $args{'1000'}) { } 3 28 31
Condition100%9/9
$args{"block"} or $args{"block_size"} or $args{"base"} or $args{"bs"}
l!l&&r!l&&!r
67034
$args{"block"} or $args{"block_size"} or $args{"base"}
l!l&&r!l&&!r
51104
$args{"block"} or $args{"block_size"}
l!l&&r!l&&!r
41105
MC/DC100%4/4
$args{"block"} or $args{"block_size"} or $args{"base"} or $args{"bs"}
$args{"block"} $args{"block_size"} $args{"base"} $args{"bs"}
Truth table100%5/5
A or B or C or D
A= $args{"block"}B= $args{"block_size"}C= $args{"base"}D= $args{"bs"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
134 $args{block_size} ||
135 $args{base} ||
136 $args{bs}
137 ) {
138 my $block = $args{block} ||
139 $args{block_size} ||
140 $args{base} ||
141 76 ▶ $args{bs};
Condition88%8/9
$args{'block'} || $args{'block_size'} || $args{'base'} || $args{'bs'}
l!l&&r!l&&!r
6700
$args{"block"} or $args{"block_size"} or $args{"base"}
l!l&&r!l&&!r
5170
$args{"block"} or $args{"block_size"}
l!l&&r!l&&!r
4171
MC/DC0%0/4
$args{'block'} || $args{'block_size'} || $args{'base'} || $args{'bs'}
$args{"block"} $args{"block_size"} $args{"base"} $args{'bs'}
Truth table80%4/5
A or B or C || D
A= $args{"block"}B= $args{"block_size"}C= $args{"base"}D= $args{'bs'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
142 76 ▶ unless ($block==1000 || $block==1024 || $block==1_024_000) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block == 1000 or $block == 1024 or $block == 1024000) 1 75 76
Condition100%6/6
$block == 1000 or $block == 1024 or $block == 1024000
l!l&&r!l&&!r
50251
$block == 1000 or $block == 1024
l!l&&r!l&&!r
42826
MC/DC100%3/3
$block == 1000 or $block == 1024 or $block == 1024000
$block == 1000 $block == 1024 $block == 1024000
Truth table100%4/4
A or B or C
A= $block == 1000B= $block == 1024C= $block == 1024000
ABCresult
1XX1
01X1
0011
0000
143 1 croak "invalid base: $block (should be 1024, 1000 or 1024000)";
144 }
145 75 $options{BLOCK} = $block;
146  
147 ▶ } elsif ($args{block_1024} ||
Condition100%6/6
$args{"block_1024"} or $args{"base_1024"} or $args{1024}
l!l&&r!l&&!r
2131
$args{"block_1024"} or $args{"base_1024"}
l!l&&r!l&&!r
1132
MC/DC100%3/3
$args{"block_1024"} or $args{"base_1024"} or $args{1024}
$args{"block_1024"} $args{"base_1024"} $args{1024}
Truth table100%4/4
A or B or C
A= $args{"block_1024"}B= $args{"base_1024"}C= $args{1024}
ABCresult
1XX1
01X1
0011
0000
148 $args{base_1024} ||
149 $args{1024}) {
150  
151 3 $options{BLOCK} = 1024;
152 ▶ } elsif ($args{block_1000} ||
Condition100%6/6
$args{"block_1000"} or $args{"base_1000"} or $args{1000}
l!l&&r!l&&!r
2128
$args{"block_1000"} or $args{"base_1000"}
l!l&&r!l&&!r
1129
MC/DC100%3/3
$args{"block_1000"} or $args{"base_1000"} or $args{1000}
$args{"block_1000"} $args{"base_1000"} $args{1000}
Truth table100%4/4
A or B or C
A= $args{"block_1000"}B= $args{"base_1000"}C= $args{1000}
ABCresult
1XX1
01X1
0011
0000
153 $args{base_1000} ||
154 $args{1000}) {
155  
156 3 $options{BLOCK} = 1000;
157 }
158  
159 # round_function => \&
160 # round_style => 'ceil' | 'floor' | 'round' | 'trunc'
161 109 ▶ if ($args{round_function}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'round_function'}) { } 4 105 109
elsif ($args{'round_style'}) { } 2 103 105
162 4 ▶ unless (ref $args{round_function} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args{"round_function"} eq "CODE") 2 2 4
163 2 croak "round function ($args{round_function}) should be a code ref";
164 }
165 2 $options{ROUND_FUNCTION} = $args{round_function};
166 2 ▶ $options{ROUND_STYLE} = $args{round_style} || 'unknown';
Condition100%2/2
$args{'round_style'} || 'unknown'
l!l
11
MC/DC100%1/1
$args{'round_style'} || 'unknown'
$args{'round_style'}
Truth table100%2/2
A
A= $args{'round_style'}
Aresult
11
00
167 } elsif ($args{round_style}) {
168 2 $options{ROUND_FUNCTION} = _round_function($args{round_style});
169 2 $options{ROUND_STYLE} = $args{round_style};
170 }
171  
172 # SI compatibility (mostly for parsing)
173 107 ▶ if ($args{si}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"si"}) 15 92 107
174 15 $options{SI} = 1;
175 }
176  
177 # suffixes => 1024 | 1000 | si_1024 | si_1000 | 1024000 | \@
178 107 ▶ if ($args{suffixes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"suffixes"}) 4 103 107
179 4 ▶ if (ref $args{suffixes} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'suffixes'} eq 'ARRAY') { } 4 0 4
elsif ($args{'suffixes'} =~ /^(si_)?(1000|1024)$/) { } 0 0 0
180 4 $options{SUFFIXES} = $args{suffixes};
181 } elsif ($args{suffixes} =~ /^(si_)?(1000|1024)$/) {
182 0 $options{SUFFIXES} = _default_suffixes($args{suffixes});
183 } else {
184 0 croak "suffixes ($args{suffixes}) should be 1024, 1000, si_1024, si_1000, 1024000 or an array ref";
185 }
186 }
187 107 ▶ if (defined $args{unit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"unit"}) 6 101 107
188 6 $options{UNIT} = $args{unit};
189 }
190  
191 # zero => undef | string
192 107 ▶ if (exists $args{zero}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"zero"}) 11 96 107
193 11 $options{ZERO} = $args{zero};
194 11 ▶ if (defined $options{ZERO}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"ZERO"}) 9 2 11
195 9 $options{ZERO} =~ s/%S/$options{SUFFIXES}->[0]/g
196 }
197 }
198  
199 # precision => <integer>
200 107 ▶ if (exists $args{precision} and $args{precision} =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"precision"} and $args{"precision"} =~ /\A\d+\z/) 2 105 107
Condition66%2/3
exists $args{"precision"} and $args{"precision"} =~ /\A\d+\z/
!ll&&!rl&&r
10502
MC/DC50%1/2
exists $args{"precision"} and $args{"precision"} =~ /\A\d+\z/
exists $args{"precision"} $args{"precision"} =~ /\A\d+\z/
Truth table66%2/3
A and B
A= exists $args{"precision"}B= $args{"precision"} =~ /\A\d+\z/
ABresult
0X0
100
111
201 2 $options{PRECISION} = $args{precision};
202 }
203  
204 # precision_cutoff => <intenger>
205 107 ▶ if (exists $args{precision_cutoff} and ($args{precision_cutoff} =~ /\A\d+\z/ or $args{precision_cutoff} = '-1')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"precision_cutoff"} and $args{"precision_cutoff"} =~ /\A\d+\z/ || ($args{"precision_cutoff"} = -1)) 1 106 107
Condition60%3/5
exists $args{"precision_cutoff"} and $args{"precision_cutoff"} =~ /\A\d+\z/ || ($args{"precision_cutoff"} = -1)
!ll&&!rl&&r
10601
$args{"precision_cutoff"} =~ /\A\d+\z/ or $args{"precision_cutoff"} = -1
l!l
01
MC/DC0%0/2
exists $args{"precision_cutoff"} and $args{"precision_cutoff"} =~ /\A\d+\z/ || ($args{"precision_cutoff"} = -1)
exists $args{"precision_cutoff"} $args{"precision_cutoff"} =~ /\A\d+\z/
Truth table66%2/3
A and B
A= exists $args{"precision_cutoff"}B= $args{"precision_cutoff"} =~ /\A\d+\z/
ABresult
0X0
100
111
206 1 $options{PRECISION_CUTOFF} = $args{precision_cutoff};
207 }
208  
209 # quiet => 1
210 107 ▶ if ($args{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"quiet"}) 0 107 107
211 0 $options{QUIET} = 1;
212 }
213  
214 107 ▶ if (defined $seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed) 25 82 107
215 25 %$seed = %options;
216 25 return $seed;
217 }
218 82 return \%options
219 }
220  
221 # NOTE. _format_bytes() SHOULD not change $options - NEVER.
222  
223 73 ▶sub _format_bytes {
Subroutine _format_bytes: called CC=15 SCAR=27.4
224 73 my $bytes = shift;
225 73 ▶ return undef unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 3 70 73
226 70 my $options = shift;
227 70 my %options = %$options;
228  
229 70 local *human_round = $options{ROUND_FUNCTION};
230  
231 70 ▶ return $options{ZERO} if ($bytes==0 && defined $options{ZERO});
Branch: 1 branch
BranchTrueFalseTotal
if $bytes == 0 and defined $options{"ZERO"} 11 59 70
Condition100%3/3
$bytes == 0 and defined $options{"ZERO"}
!ll&&!rl&&r
58111
MC/DC100%2/2
$bytes == 0 and defined $options{"ZERO"}
$bytes == 0 defined $options{"ZERO"}
Truth table100%3/3
A and B
A= $bytes == 0B= defined $options{"ZERO"}
ABresult
0X0
100
111
232  
233 59 my $block = $options{BLOCK};
234  
235 # if a suffix set was not specified, pick a default [**]
236 59 ▶ my @suffixes = $options{SUFFIXES} ? @{$options{SUFFIXES}} : _default_suffixes( ($options{SI} ? 'si_' : '') . $block);
Branch: 2 branches
BranchTrueFalseTotal
$options{'SUFFIXES'} ? : 1 58 59
$options{'SI'} ? : 4 54 58
237  
238 # WHAT ABOUT NEGATIVE NUMBERS: -1K ?
239 59 my $sign = '';
240 59 ▶ if ($bytes<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < 0) 3 56 59
241 3 $bytes = -$bytes;
242 3 $sign = '-';
243 }
244  
245 59 my $suffix = $suffixes[0];
246 59 my $x = $bytes;
247 59 my $magnitude = 0;
248 59 ▶ if($bytes >= $block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes >= $block) 30 29 59
249 # return "$sign$bytes" if $bytes<$block;
250 do {
251 67 $x /= $block;
252 67 $magnitude++;
253 30 ▶ } while ( human_round($x, $options{PRECISION}) >= $block );
Branch: 1 branch
BranchTrueFalseTotal
human_round($x, $options{"PRECISION"}) >= $block and do { do { $x /= $block; ++$magnitude } } 37 30 67
254 30 ▶ if($magnitude >= (0 + @suffixes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magnitude >= 0 + @suffixes) 0 30 30
255 0 ▶ carp "number too large (>= $block**$magnitude)" unless ($options{QUIET});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"QUIET"} 0 0 0
256 }
257 30 $suffix = $suffixes[$magnitude];
258 }
259 #$x = human_round( $x, $options{PRECISION} );
260  
261 59 $x = _precision_cutoff($x, $options);
262 #reasses encase the precision_cutoff caused the value to cross the block size
263 59 ▶ if($x >= $block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $block) 1 58 59
264 1 $x /= $block;
265 1 $magnitude++;
266 1 ▶ if($magnitude >= (0 + @suffixes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magnitude >= 0 + @suffixes) 0 1 1
267 0 ▶ carp "number too large (>= $block**$magnitude)" unless ($options{QUIET});
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"QUIET"} 0 0 0
268 }
269 1 $suffix = $suffixes[$magnitude];
270 1 $x = _precision_cutoff($x, $options);
271 }
272  
273 59 ▶ my $unit = $options{UNIT} || '';
Condition100%2/2
$options{'UNIT'} || ''
l!l
257
MC/DC100%1/1
$options{'UNIT'} || ''
$options{'UNIT'}
Truth table100%2/2
A
A= $options{'UNIT'}
Aresult
11
00
274  
275 59 return $sign . $x . $suffix . $unit;
276  
277 }
278  
279 60 ▶sub _precision_cutoff {
Subroutine _precision_cutoff: called CC=3 SCAR=11.0
280 60 my $bytes = shift;
281 60 my $options = shift;
282 60 my %options = %$options;
283 60 ▶ if ( $options{PRECISION_CUTOFF} != -1 and ( length( sprintf( "%d", $bytes ) ) > $options{PRECISION_CUTOFF} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'PRECISION_CUTOFF'} != -1 and length sprintf('%d', $bytes) > $options{'PRECISION_CUTOFF'}) { } 33 27 60
Condition66%2/3
$options{"PRECISION_CUTOFF"} != -1 and length sprintf("%d", $bytes) > $options{"PRECISION_CUTOFF"}
!ll&&!rl&&r
02733
MC/DC50%1/2
$options{"PRECISION_CUTOFF"} != -1 and length sprintf("%d", $bytes) > $options{"PRECISION_CUTOFF"}
$options{"PRECISION_CUTOFF"} != -1 length sprintf("%d", $bytes) > $options{"PRECISION_CUTOFF"}
Truth table66%2/3
A and B
A= $options{"PRECISION_CUTOFF"} != -1B= length sprintf("%d", $bytes) > $options{"PRECISION_CUTOFF"}
ABresult
0X0
100
111
284 33 $bytes = sprintf( "%d", human_round( $bytes, 0 ) );
285 } else {
286 27 $bytes = sprintf( "%." . $options{PRECISION} . "f", human_round( $bytes, $options{PRECISION} ) );
287 }
288 60 return $bytes;
289 }
290  
291 93 ▶sub _parse_bytes {
Subroutine _parse_bytes: called CC=20 SCAR=31.1
292 93 my $human = shift;
293 93 my $options = shift;
294 93 my %options = %$options;
295  
296 93 ▶ return 0 if( exists $options{ZERO} && ((!defined $options{ZERO} && !defined $human) || (defined $human && $human eq $options{ZERO})) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"ZERO"} and !(defined $options{"ZERO"} || defined $human) || defined $human && $human eq $options{"ZERO"} 13 80 93
Condition83%10/12
exists $options{"ZERO"} and !(defined $options{"ZERO"} || defined $human) || defined $human && $human eq $options{"ZERO"}
!ll&&!rl&&r
08013
not defined $options{"ZERO"} || defined $human or defined $human and $human eq $options{"ZERO"}
l!l&&r!l&&!r
11280
defined $options{'ZERO'} || defined $human
l!l&&r!l&&!r
9201
defined $human and $human eq $options{"ZERO"}
!ll&&!rl&&r
37712
MC/DC60%3/5
exists $options{"ZERO"} and !(defined $options{"ZERO"} || defined $human) || defined $human && $human eq $options{"ZERO"}
exists $options{"ZERO"} defined $options{'ZERO'} defined $human defined $human $human eq $options{"ZERO"}
Truth table50%4/8
A and not(B || C) or D and E
A= exists $options{"ZERO"}B= defined $options{'ZERO'}C= defined $humanD= defined $humanE= $human eq $options{"ZERO"}
ABCDEresult
0XXXX0
11X0X0
11X100
1010X0
101100
100XX1
11X111
101111
297 80 ▶ return undef unless defined $human;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $human 3 77 80
298  
299 77 my %suffix_mult;
300 my %suffix_block;
301 77 my $m;
302  
303 77 ▶ if( $options{SUFFIXES} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'SUFFIXES'}) { } 0 77 77
304 0 $m = 1;
305 0 foreach my $s (@{$options{SUFFIXES}}) {
306 0 $suffix_mult{$s} = $m;
307 0 $suffix_block{$s} = $options{BLOCK};
308 0 $m *= $suffix_block{$s};
309 }
310 } else {
311 77 ▶ if( !defined $options{SI} || $options{SI} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $options{"SI"} or $options{"SI"} == 1) 77 0 77
Condition66%2/3
not defined $options{"SI"} or $options{"SI"} == 1
l!l&&r!l&&!r
7160
MC/DC0%0/2
not defined $options{"SI"} or $options{"SI"} == 1
not defined $options{"SI"} $options{"SI"} == 1
Truth table66%2/3
not(A) or B
A= not defined $options{"SI"}B= $options{"SI"} == 1
ABresult
1X1
011
000
312 # If SI compatibility has been set BLOCK is ignored as it is infered from the unit
313 77 $m = 1;
314 77 foreach my $s (@{$DEFAULT_SUFFIXES{si_1000}}) {
315 693 $suffix_mult{$s} = $m;
316 693 $suffix_block{$s} = 1000;
317 693 $m *= $suffix_block{$s};
318 }
319
320 77 $m = 1;
321 77 foreach my $s (@{$DEFAULT_SUFFIXES{si_1024}}) {
322 693 $suffix_mult{$s} = $m;
323 693 $suffix_block{$s} = 1024;
324 693 $m *= $suffix_block{$s};
325 }
326 }
327  
328 # The regular suffixes are only taken into account in default mode without specifically asking for SI compliance
329 77 ▶ if( !defined $options{SI} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"SI"}) 71 6 77
330 71 $m = 1;
331 71 foreach my $s (_default_suffixes( $options{BLOCK} )) {
332 595 $suffix_mult{$s} = $m;
333 595 $suffix_block{$s} = $options{BLOCK};
334 595 $m *= $suffix_block{$s};
335 }
336 }
337 }
338  
339 77 my ($sign, $k, $unit) = ($human =~ /^\s*(-?)\s*(\d*(?:\.\d*)?)\s*(\D*)$/);
340  
341 # print STDERR "S: $sign K: $k U: $unit\n";
342  
343  
344 77 my $mult;
345 77 ▶ my $u = $options{UNIT} || '';
Condition100%2/2
$options{'UNIT'} || ''
l!l
275
MC/DC100%1/1
$options{'UNIT'} || ''
$options{'UNIT'}
Truth table100%2/2
A
A= $options{'UNIT'}
Aresult
11
00
346 77 foreach my $s (keys %suffix_block) {
347 1033 ▶ if( $unit =~ /^${s}${u}$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit =~ /^$s$u$/i) 77 956 1033
348 77 ▶ $mult = ($sign eq '-' ? -1 : 1) * $suffix_mult{$s};
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' ? : 3 74 77
349 77 last;
350 }
351 }
352  
353 77 ▶ if( !defined $mult ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mult) 0 77 77
354 0 carp "Could not parse human readable byte value '$human'";
355 0 return undef;
356 }
357  
358 77 my $bytes = int( $k * $mult );
359  
360 77 return $bytes;
361 }
362  
363  
364 # convert byte count (file size) to human readable format
365 71 ▶sub format_bytes {
Subroutine format_bytes: called CC=1 SCAR=0.0
Pod documented
366 71 my $bytes = shift;
367 71 my $options = _parse_args(undef, @_);
368 #use YAML; print Dump $options;
369 71 return _format_bytes($bytes, $options);
370 }
371  
372 # convert human readable format to byte count (file size)
373 76 ▶sub parse_bytes {
Subroutine parse_bytes: called CC=1 SCAR=0.0
Pod documented
374 76 my $human = shift;
375 76 my $options = _parse_args(undef, @_);
376 #use YAML; print Dump $options;
377 76 return _parse_bytes($human, $options);
378 }
379  
380 ### the OO way
381  
382 # new()
383 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
384 2 my $proto = shift;
385 2 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
386 2 my $opts = _parse_args(undef, @_);
387 2 return bless $opts, $class;
388 }
389  
390 # set_options()
391 1 ▶sub set_options {
Subroutine set_options: called CC=1 SCAR=0.0
Pod documented
392 1 my $self = shift;
393 1 return $self->_parse_args(@_);
394 }
395  
396 # format()
397 2 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod documented
398 2 my $self = shift;
399 2 my $bytes = shift;
400 2 return _format_bytes($bytes, $self);
401 }
402  
403 # parse()
404 17 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
405 17 my $self = shift;
406 17 my $human = shift;
407 17 return _parse_bytes($human, $self);
408 }
409  
410 # the solution by COG in Filesys::DiskUsage
411 # convert size to human readable format
412 #sub _convert {
413 # defined (my $size = shift) || return undef;
414 # my $config = {@_};
415 # $config->{human} || return $size;
416 # my $block = $config->{'Human-readable'} ? 1000 : 1024;
417 # my @args = qw/B K M G/;
418 #
419 # while (@args && $size > $block) {
420 # shift @args;
421 # $size /= $block;
422 # }
423 #
424 # if ($config->{'truncate-readable'} > 0) {
425 # $size = sprintf("%.$config->{'truncate-readable'}f",$size);
426 # }
427 #
428 # "$size$args[0]";
429 #}
430 #
431 # not exact: 1024 => 1024B instead of 1K
432 # not nicely formatted => 1.00 instead of 1K
433  
434 11 1;
435  
436 __END__
↑ Summary