|
@@ -7,6 +7,11 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ decimalKUnit = 1000
|
|
|
+ binaryKUnit = 1024
|
|
|
+)
|
|
|
+
|
|
|
var sizeRegex *regexp.Regexp
|
|
|
|
|
|
func init() {
|
|
@@ -30,36 +35,10 @@ func HumanSize(size int64) string {
|
|
|
return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
|
|
|
}
|
|
|
|
|
|
-// FromHumanSize returns an integer from a human-readable specification of a size
|
|
|
-// using SI standard (eg. "44kB", "17MB")
|
|
|
+// FromHumanSize returns an integer from a human-readable specification of a
|
|
|
+// size using SI standard (eg. "44kB", "17MB")
|
|
|
func FromHumanSize(size string) (int64, error) {
|
|
|
- matches := sizeRegex.FindStringSubmatch(size)
|
|
|
-
|
|
|
- if len(matches) != 3 {
|
|
|
- return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
|
- }
|
|
|
-
|
|
|
- theSize, err := strconv.ParseInt(matches[1], 10, 0)
|
|
|
- if err != nil {
|
|
|
- return -1, err
|
|
|
- }
|
|
|
-
|
|
|
- unit := strings.ToLower(matches[2])
|
|
|
-
|
|
|
- switch unit {
|
|
|
- case "k":
|
|
|
- theSize *= 1000
|
|
|
- case "m":
|
|
|
- theSize *= 1000 * 1000
|
|
|
- case "g":
|
|
|
- theSize *= 1000 * 1000 * 1000
|
|
|
- case "t":
|
|
|
- theSize *= 1000 * 1000 * 1000 * 1000
|
|
|
- case "p":
|
|
|
- theSize *= 1000 * 1000 * 1000 * 1000 * 1000
|
|
|
- }
|
|
|
-
|
|
|
- return theSize, nil
|
|
|
+ return parseSize(size, decimalKUnit)
|
|
|
}
|
|
|
|
|
|
// Parses a human-readable string representing an amount of RAM
|
|
@@ -67,31 +46,37 @@ func FromHumanSize(size string) (int64, error) {
|
|
|
// returns the number of bytes, or -1 if the string is unparseable.
|
|
|
// Units are case-insensitive, and the 'b' suffix is optional.
|
|
|
func RAMInBytes(size string) (int64, error) {
|
|
|
+ return parseSize(size, binaryKUnit)
|
|
|
+}
|
|
|
+
|
|
|
+// Parses the human-readable size string into the amount it represents given
|
|
|
+// the desired kilo unit [decimalKiloUnit=1000|binaryKiloUnit=1024]
|
|
|
+func parseSize(size string, kUnit int64) (int64, error) {
|
|
|
matches := sizeRegex.FindStringSubmatch(size)
|
|
|
|
|
|
if len(matches) != 3 {
|
|
|
return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
|
}
|
|
|
|
|
|
- memLimit, err := strconv.ParseInt(matches[1], 10, 0)
|
|
|
+ theSize, err := strconv.ParseInt(matches[1], 10, 0)
|
|
|
if err != nil {
|
|
|
return -1, err
|
|
|
}
|
|
|
|
|
|
- unit := strings.ToLower(matches[2])
|
|
|
+ unitPrefix := strings.ToLower(matches[2])
|
|
|
|
|
|
- switch unit {
|
|
|
+ switch unitPrefix {
|
|
|
case "k":
|
|
|
- memLimit *= 1024
|
|
|
+ theSize *= kUnit
|
|
|
case "m":
|
|
|
- memLimit *= 1024 * 1024
|
|
|
+ theSize *= kUnit * kUnit
|
|
|
case "g":
|
|
|
- memLimit *= 1024 * 1024 * 1024
|
|
|
+ theSize *= kUnit * kUnit * kUnit
|
|
|
case "t":
|
|
|
- memLimit *= 1024 * 1024 * 1024 * 1024
|
|
|
+ theSize *= kUnit * kUnit * kUnit * kUnit
|
|
|
case "p":
|
|
|
- memLimit *= 1024 * 1024 * 1024 * 1024 * 1024
|
|
|
+ theSize *= kUnit * kUnit * kUnit * kUnit * kUnit
|
|
|
}
|
|
|
|
|
|
- return memLimit, nil
|
|
|
+ return theSize, nil
|
|
|
}
|