2014-05-12 19:40:19 -04:00
|
|
|
package units
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-25 12:33:04 -04:00
|
|
|
// See: http://en.wikipedia.org/wiki/Binary_prefix
|
2014-07-22 17:23:52 -04:00
|
|
|
const (
|
2014-07-25 12:33:04 -04:00
|
|
|
// Decimal
|
2014-07-26 22:44:23 -04:00
|
|
|
KB = 1000
|
|
|
|
MB = 1000 * KB
|
|
|
|
GB = 1000 * MB
|
|
|
|
TB = 1000 * GB
|
|
|
|
PB = 1000 * TB
|
2014-07-25 12:33:04 -04:00
|
|
|
|
|
|
|
// Binary
|
2014-07-26 22:44:23 -04:00
|
|
|
KiB = 1024
|
|
|
|
MiB = 1024 * KiB
|
|
|
|
GiB = 1024 * MiB
|
|
|
|
TiB = 1024 * GiB
|
|
|
|
PiB = 1024 * TiB
|
2014-07-25 12:33:04 -04:00
|
|
|
)
|
|
|
|
|
2014-07-26 22:44:23 -04:00
|
|
|
type unitMap map[string]int64
|
2014-07-25 12:33:04 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
|
|
|
|
binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
|
2014-07-22 17:23:52 -04:00
|
|
|
)
|
|
|
|
|
2014-07-22 02:50:27 -04:00
|
|
|
var sizeRegex *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
2014-07-24 15:34:11 -04:00
|
|
|
sizeRegex = regexp.MustCompile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
|
2014-07-22 02:50:27 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 12:33:04 -04:00
|
|
|
var unitAbbrs = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
2014-07-24 02:55:48 -04:00
|
|
|
|
2014-05-12 19:40:19 -04:00
|
|
|
// HumanSize returns a human-readable approximation of a size
|
|
|
|
// using SI standard (eg. "44kB", "17MB")
|
|
|
|
func HumanSize(size int64) string {
|
2014-07-24 02:53:16 -04:00
|
|
|
i := 0
|
|
|
|
sizef := float64(size)
|
2014-05-12 19:40:19 -04:00
|
|
|
for sizef >= 1000.0 {
|
|
|
|
sizef = sizef / 1000.0
|
|
|
|
i++
|
|
|
|
}
|
2014-07-25 12:33:04 -04:00
|
|
|
return fmt.Sprintf("%.4g %s", sizef, unitAbbrs[i])
|
2014-05-12 19:40:19 -04:00
|
|
|
}
|
|
|
|
|
2014-07-22 17:23:52 -04:00
|
|
|
// FromHumanSize returns an integer from a human-readable specification of a
|
|
|
|
// size using SI standard (eg. "44kB", "17MB")
|
2014-03-31 03:30:20 -04:00
|
|
|
func FromHumanSize(size string) (int64, error) {
|
2014-07-25 12:33:04 -04:00
|
|
|
return parseSize(size, decimalMap)
|
2014-03-31 03:30:20 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 19:40:19 -04:00
|
|
|
// Parses a human-readable string representing an amount of RAM
|
2014-06-23 15:36:08 -04:00
|
|
|
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
|
|
|
|
// returns the number of bytes, or -1 if the string is unparseable.
|
2014-05-12 19:40:19 -04:00
|
|
|
// Units are case-insensitive, and the 'b' suffix is optional.
|
2014-07-22 15:16:50 -04:00
|
|
|
func RAMInBytes(size string) (int64, error) {
|
2014-07-25 12:33:04 -04:00
|
|
|
return parseSize(size, binaryMap)
|
2014-07-22 17:23:52 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 12:33:04 -04:00
|
|
|
// Parses the human-readable size string into the amount it represents
|
|
|
|
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
|
|
|
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
2014-05-12 19:40:19 -04:00
|
|
|
if len(matches) != 3 {
|
2014-07-25 12:33:04 -04:00
|
|
|
return -1, fmt.Errorf("Invalid size: '%s'", sizeStr)
|
2014-05-12 19:40:19 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 12:33:04 -04:00
|
|
|
size, err := strconv.ParseInt(matches[1], 10, 0)
|
2014-07-22 02:58:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
2014-05-12 19:40:19 -04:00
|
|
|
}
|
|
|
|
|
2014-07-22 17:23:52 -04:00
|
|
|
unitPrefix := strings.ToLower(matches[2])
|
2014-07-25 12:33:04 -04:00
|
|
|
if mul, ok := uMap[unitPrefix]; ok {
|
2014-07-26 22:44:23 -04:00
|
|
|
size *= mul
|
2014-05-12 19:40:19 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 12:33:04 -04:00
|
|
|
return size, nil
|
2014-05-12 19:40:19 -04:00
|
|
|
}
|