From e3f670abe3afd8e741f672a9c8ce1c3cf61bff1d Mon Sep 17 00:00:00 2001 From: Jed Reynolds Date: Mon, 23 Mar 2020 20:25:42 -0700 Subject: [PATCH] Utils: adds expand_unit_str() --- LANforge/Utils.pm | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/LANforge/Utils.pm b/LANforge/Utils.pm index e5fb26cd..d9f68332 100644 --- a/LANforge/Utils.pm +++ b/LANforge/Utils.pm @@ -1080,6 +1080,37 @@ sub hunks_to_hashes { return $rh; } +sub expand_unit_str { + my ($self, $string) = @_; + die("Utils::expand_unit_str expects string to parse") + if (!(defined $string) || ("" eq $string)); + + return 0 if ($string =~ /^0+\s*\w+$/); + + my ($num, $suf) = $string =~ /^(\d+)\s*(\w*)$/; + if (!(defined $num) || ("" eq $num)) { + die("Utils::expand_unit_str exects something like 33Mbps or '33 Mbps', not $string"); + } + my $multiplier = 1; + if (!(defined $suf) || ("" eq $suf)) { + $multiplier = 1; + print STDERR "Utils::expand_unit_str saw no suffix in [$string]\n"; + } + elsif ($suf =~ /^bps$/i) { + $multiplier = 1; + } + elsif ($suf =~ /^kbps$/i) { + $multiplier = 1000; + } + elsif ($suf =~ /^mbps$/i) { + $multiplier = 1000 * 1000; + } + elsif ($suf =~ /^gbps$/i) { + $multiplier = 1000 * 1000 * 1000; + } + return int($num) * $multiplier; +} + #### 1; __END__