mirror of
https://github.com/moparisthebest/pacman
synced 2024-11-15 22:05:02 -05:00
b58489d29a
This is a bash wrapper around an awk function that parses human readable sizes and returns their representative values in bytes, as a string. A small test harness is added to validate the functionality. Signed-off-by: Dave Reisner <dreisner@archlinux.org>
52 lines
1.1 KiB
Bash
52 lines
1.1 KiB
Bash
human_to_size() {
|
|
awk -v human="$1" '
|
|
function trim(s) {
|
|
gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
|
|
return s
|
|
}
|
|
|
|
function parse_units(units) {
|
|
if (!units || units == "B")
|
|
return 1
|
|
if (match(units, /^.iB$/))
|
|
return 1024
|
|
if (match(units, /^.B$/))
|
|
return 1000
|
|
if (length(units) == 1)
|
|
return 1024
|
|
|
|
# parse failure: invalid base
|
|
return -1
|
|
}
|
|
|
|
function parse_scale(s) {
|
|
return index("BKMGTPE", s) - 1
|
|
}
|
|
|
|
function isnumeric(string) {
|
|
return match(string, /^[-+]?[[:digit:]]*(\.[[:digit:]]*)?/)
|
|
}
|
|
|
|
BEGIN {
|
|
# peel off the leading number as the size, fail on invalid number
|
|
human = trim(human)
|
|
if (isnumeric(human))
|
|
size = substr(human, RSTART, RLENGTH)
|
|
else
|
|
exit 1
|
|
|
|
# the trimmed remainder is assumed to be the units
|
|
units = trim(substr(human, RLENGTH + 1))
|
|
|
|
base = parse_units(units)
|
|
if (base < 0)
|
|
exit 1
|
|
|
|
scale = parse_scale(substr(units, 1, 1))
|
|
if (scale < 0)
|
|
exit 1
|
|
|
|
printf "%d\n", size * base^scale + (size + 0 > 0 ? 0.5 : -0.5)
|
|
}'
|
|
}
|