2014-05-12 23:40:19 +00:00
|
|
|
package units
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-25 16:33:04 +00:00
|
|
|
// See: http://en.wikipedia.org/wiki/Binary_prefix
|
2014-07-22 21:23:52 +00:00
|
|
|
const (
|
2014-07-25 16:33:04 +00:00
|
|
|
// Decimal
|
2014-10-06 19:19:41 +00:00
|
|
|
|
2014-07-27 02:44:23 +00:00
|
|
|
KB = 1000
|
|
|
|
MB = 1000 * KB
|
|
|
|
GB = 1000 * MB
|
|
|
|
TB = 1000 * GB
|
|
|
|
PB = 1000 * TB
|
2014-07-25 16:33:04 +00:00
|
|
|
|
|
|
|
// Binary
|
2014-10-06 19:19:41 +00:00
|
|
|
|
2014-07-27 02:44:23 +00:00
|
|
|
KiB = 1024
|
|
|
|
MiB = 1024 * KiB
|
|
|
|
GiB = 1024 * MiB
|
|
|
|
TiB = 1024 * GiB
|
|
|
|
PiB = 1024 * TiB
|
2014-07-25 16:33:04 +00:00
|
|
|
)
|
|
|
|
|
2014-07-27 02:44:23 +00:00
|
|
|
type unitMap map[string]int64
|
2014-07-25 16:33:04 +00: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-09-16 03:30:10 +00:00
|
|
|
sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
|
2014-07-22 21:23:52 +00:00
|
|
|
)
|
|
|
|
|
2014-10-14 03:54:32 +00:00
|
|
|
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
|
|
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
2014-07-24 06:55:48 +00:00
|
|
|
|
2015-04-28 08:05:28 +00:00
|
|
|
// CustomSize returns a human-readable approximation of a size
|
2015-07-25 08:35:07 +00:00
|
|
|
// using custom format.
|
2015-04-28 08:05:28 +00:00
|
|
|
func CustomSize(format string, size float64, base float64, _map []string) string {
|
|
|
|
i := 0
|
|
|
|
for size >= base {
|
|
|
|
size = size / base
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(format, size, _map[i])
|
|
|
|
}
|
|
|
|
|
2014-05-12 23:40:19 +00:00
|
|
|
// HumanSize returns a human-readable approximation of a size
|
2015-07-25 08:35:07 +00:00
|
|
|
// using SI standard (eg. "44kB", "17MB").
|
2014-11-25 08:02:47 +00:00
|
|
|
func HumanSize(size float64) string {
|
2015-06-23 16:45:34 +00:00
|
|
|
return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
|
2014-10-14 03:54:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// BytesSize returns a human-readable size in bytes, kibibytes,
|
|
|
|
// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
|
2014-10-14 03:54:32 +00:00
|
|
|
func BytesSize(size float64) string {
|
2015-04-28 08:05:28 +00:00
|
|
|
return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:23:52 +00:00
|
|
|
// FromHumanSize returns an integer from a human-readable specification of a
|
2015-07-25 08:35:07 +00:00
|
|
|
// size using SI standard (eg. "44kB", "17MB").
|
2014-03-31 07:30:20 +00:00
|
|
|
func FromHumanSize(size string) (int64, error) {
|
2014-07-25 16:33:04 +00:00
|
|
|
return parseSize(size, decimalMap)
|
2014-03-31 07:30:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 19:19:41 +00:00
|
|
|
// RAMInBytes 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.
|
2014-07-22 19:16:50 +00:00
|
|
|
func RAMInBytes(size string) (int64, error) {
|
2014-07-25 16:33:04 +00:00
|
|
|
return parseSize(size, binaryMap)
|
2014-07-22 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// Parses the human-readable size string into the amount it represents.
|
2014-07-25 16:33:04 +00:00
|
|
|
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
|
|
|
matches := sizeRegex.FindStringSubmatch(sizeStr)
|
2014-05-12 23:40:19 +00:00
|
|
|
if len(matches) != 3 {
|
2014-10-06 19:19:41 +00:00
|
|
|
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 16:33:04 +00:00
|
|
|
size, err := strconv.ParseInt(matches[1], 10, 0)
|
2014-07-22 06:58:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:23:52 +00:00
|
|
|
unitPrefix := strings.ToLower(matches[2])
|
2014-07-25 16:33:04 +00:00
|
|
|
if mul, ok := uMap[unitPrefix]; ok {
|
2014-07-27 02:44:23 +00:00
|
|
|
size *= mul
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 16:33:04 +00:00
|
|
|
return size, nil
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|