-
Notifications
You must be signed in to change notification settings - Fork 1
/
unhuman
executable file
·56 lines (47 loc) · 1.31 KB
/
unhuman
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env perl
use strict;
use warnings;
use Math::BigFloat;
my %suffixes = (
k => 1024,
m => 1024 ** 2,
g => 1024 ** 3,
t => 1024 ** 4,
p => 1024 ** 5,
);
my $suffix_re = join q{}, q{[}, keys %suffixes, q{]};
$suffix_re = qr/(?:$suffix_re(?:b?)|e\+\d+)/i;
sub un_format_bytes {
my ($number, $suffix) = @_;
my ($big_number) = Math::BigFloat->new($number);
$suffix =~ s/b$//i;
$suffix = lc $suffix;
return $big_number * $suffixes{$suffix};
}
sub un_format_scientific {
my ($number, $suffix) = @_;
$suffix =~ s/^e\+//i;
my ($big_number) = Math::BigFloat->new($number);
$big_number->bpow($suffix);
}
while (<>) {
chomp;
# Copied from Regexp::Common's $RE{num}{real}.
my $re = qr/(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)
(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])
(?:(?:[-+]?)(?:[0123456789]+))|))/x;
my @parts = split /\b($re$suffix_re)\b/io;
foreach my $part (@parts) {
if ($part =~ m/^($re)($suffix_re)$/io) {
my ($number, $suffix) = ($1, $2);
if ($suffix =~ m/^e/i) {
print un_format_scientific($number, $suffix);
} else {
print un_format_bytes($number, $suffix);
}
} else {
print $part;
}
}
print qq{\n};
}