2014-05-12 23:40:19 +00:00
|
|
|
package units
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HumanSize returns a human-readable approximation of a size
|
|
|
|
// using SI standard (eg. "44kB", "17MB")
|
|
|
|
func HumanSize(size int64) string {
|
|
|
|
i := 0
|
|
|
|
var sizef float64
|
|
|
|
sizef = float64(size)
|
|
|
|
units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
|
|
for sizef >= 1000.0 {
|
|
|
|
sizef = sizef / 1000.0
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.4g %s", sizef, units[i])
|
|
|
|
}
|
|
|
|
|
2014-03-31 07:30:20 +00:00
|
|
|
// FromHumanSize returns an integer from a human-readable specification of a size
|
|
|
|
// using SI standard (eg. "44kB", "17MB")
|
|
|
|
func FromHumanSize(size string) (int64, error) {
|
|
|
|
re, error := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
|
|
|
|
if error != nil {
|
|
|
|
return -1, fmt.Errorf("%s does not specify not a size", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := re.FindStringSubmatch(size)
|
|
|
|
|
|
|
|
if len(matches) != 3 {
|
|
|
|
return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
theSize, error := strconv.ParseInt(matches[1], 10, 0)
|
|
|
|
if error != nil {
|
|
|
|
return -1, error
|
|
|
|
}
|
|
|
|
|
|
|
|
unit := strings.ToLower(matches[2])
|
|
|
|
|
|
|
|
if unit == "k" {
|
|
|
|
theSize *= 1000
|
|
|
|
} else if unit == "m" {
|
|
|
|
theSize *= 1000 * 1000
|
|
|
|
} else if unit == "g" {
|
|
|
|
theSize *= 1000 * 1000 * 1000
|
|
|
|
} else if unit == "t" {
|
|
|
|
theSize *= 1000 * 1000 * 1000 * 1000
|
|
|
|
} else if unit == "p" {
|
|
|
|
theSize *= 1000 * 1000 * 1000 * 1000 * 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
return theSize, nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 23:40:19 +00:00
|
|
|
// Parses a human-readable string representing an amount of RAM
|
2014-06-23 19:36:08 +00:00
|
|
|
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
|
|
|
|
// returns the number of bytes, or -1 if the string is unparseable.
|
2014-05-12 23:40:19 +00:00
|
|
|
// Units are case-insensitive, and the 'b' suffix is optional.
|
|
|
|
func RAMInBytes(size string) (bytes int64, err error) {
|
2014-06-23 19:36:08 +00:00
|
|
|
re, error := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
|
2014-05-12 23:40:19 +00:00
|
|
|
if error != nil {
|
|
|
|
return -1, error
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := re.FindStringSubmatch(size)
|
|
|
|
|
|
|
|
if len(matches) != 3 {
|
|
|
|
return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
memLimit, error := strconv.ParseInt(matches[1], 10, 0)
|
|
|
|
if error != nil {
|
|
|
|
return -1, error
|
|
|
|
}
|
|
|
|
|
|
|
|
unit := strings.ToLower(matches[2])
|
|
|
|
|
|
|
|
if unit == "k" {
|
|
|
|
memLimit *= 1024
|
|
|
|
} else if unit == "m" {
|
|
|
|
memLimit *= 1024 * 1024
|
|
|
|
} else if unit == "g" {
|
|
|
|
memLimit *= 1024 * 1024 * 1024
|
2014-06-23 19:36:08 +00:00
|
|
|
} else if unit == "t" {
|
|
|
|
memLimit *= 1024 * 1024 * 1024 * 1024
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return memLimit, nil
|
|
|
|
}
|