line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# format::number Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Format::Number; |
7
|
1
|
|
|
1
|
|
1089
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use base qw(Metabrik::Shell::Command Metabrik::System::Package); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
595
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
thousands_sep => [ qw(char) ], |
20
|
|
|
|
|
|
|
decimal_point => [ qw(char) ], |
21
|
|
|
|
|
|
|
kibi_suffix => [ qw(string) ], |
22
|
|
|
|
|
|
|
kilo_suffix => [ qw(string) ], |
23
|
|
|
|
|
|
|
mebi_suffix => [ qw(string) ], |
24
|
|
|
|
|
|
|
mega_suffix => [ qw(string) ], |
25
|
|
|
|
|
|
|
gibi_suffix => [ qw(string) ], |
26
|
|
|
|
|
|
|
giga_suffix => [ qw(string) ], |
27
|
|
|
|
|
|
|
tebi_suffix => [ qw(string) ], |
28
|
|
|
|
|
|
|
tera_suffix => [ qw(string) ], |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
attributes_default => { |
31
|
|
|
|
|
|
|
thousands_sep => '.', |
32
|
|
|
|
|
|
|
decimal_point => ',', |
33
|
|
|
|
|
|
|
kibi_suffix => 'Kb', |
34
|
|
|
|
|
|
|
kilo_suffix => 'KB', |
35
|
|
|
|
|
|
|
mebi_suffix => 'Mb', |
36
|
|
|
|
|
|
|
mega_suffix => 'MB', |
37
|
|
|
|
|
|
|
gibi_suffix => 'Gb', |
38
|
|
|
|
|
|
|
giga_suffix => 'GB', |
39
|
|
|
|
|
|
|
tebi_suffix => 'Tb', |
40
|
|
|
|
|
|
|
tera_suffix => 'TB', |
41
|
|
|
|
|
|
|
}, |
42
|
|
|
|
|
|
|
commands => { |
43
|
|
|
|
|
|
|
from_number => [ qw(number) ], |
44
|
|
|
|
|
|
|
to_number => [ qw(string) ], |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
require_modules => { |
47
|
|
|
|
|
|
|
'Number::Format' => [ ], |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _new { |
53
|
0
|
|
|
0
|
|
|
my $self = shift; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
my $x = Number::Format->new( |
56
|
|
|
|
|
|
|
-thousands_sep => $self->thousands_sep, |
57
|
|
|
|
|
|
|
-decimal_point => $self->decimal_point, |
58
|
|
|
|
|
|
|
-kibi_suffix => $self->kibi_suffix, |
59
|
|
|
|
|
|
|
-kilo_suffix => $self->kilo_suffix, |
60
|
|
|
|
|
|
|
-mebi_suffix => $self->mebi_suffix, |
61
|
|
|
|
|
|
|
-mega_suffix => $self->mega_suffix, |
62
|
|
|
|
|
|
|
-gibi_suffix => $self->gibi_suffix, |
63
|
|
|
|
|
|
|
-giga_suffix => $self->giga_suffix, |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $x; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub from_number { |
70
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
71
|
0
|
|
|
|
|
|
my ($number) = @_; |
72
|
|
|
|
|
|
|
|
73
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('from_number', $number) or return; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
if ($number !~ /^\d+/) { |
76
|
0
|
|
|
|
|
|
return $self->log->error("from_number: number [$number] is not valid"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my $x = $self->_new; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return $x->format_bytes($number); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub to_number { |
85
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
86
|
0
|
|
|
|
|
|
my ($string) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('to_number', $string) or return; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
my $x = $self->_new; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $tebi = $self->tebi_suffix; |
93
|
0
|
|
|
|
|
|
my $tera = $self->tera_suffix; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Number::Format does not support TB |
96
|
0
|
0
|
|
|
|
|
if ($string =~ s{${tera}$}{}) { |
|
|
0
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
$string *= 1024; |
98
|
0
|
|
|
|
|
|
$string .= $self->giga_suffix; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
elsif ($string =~ s{${tebi}$}{}) { |
101
|
0
|
|
|
|
|
|
$string *= 1024 / 8; |
102
|
0
|
|
|
|
|
|
$string .= $self->gibi_suffix; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
return $x->unformat_number($string); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
1; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
__END__ |