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-10-06 15:19:41 -04:00
|
|
|
|
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-10-06 15:19:41 -04:00
|
|
|
|
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-09-15 23:30:10 -04:00
|
|
|
sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
|
2014-07-22 17:23:52 -04:00
|
|
|
)
|
|
|
|
|
2014-10-13 23:54:32 -04: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 02:55:48 -04:00
|
|
|
|
2015-04-28 04:05:28 -04:00
|
|
|
// CustomSize returns a human-readable approximation of a size
|
|
|
|
// using custom format
|
|
|
|
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 19:40:19 -04:00
|
|
|
// HumanSize returns a human-readable approximation of a size
|
|
|
|
// using SI standard (eg. "44kB", "17MB")
|
2014-11-25 03:02:47 -05:00
|
|
|
func HumanSize(size float64) string {
|
2015-04-28 04:05:28 -04:00
|
|
|
return CustomSize("%.4g %s", float64(size), 1000.0, decimapAbbrs)
|
2014-10-13 23:54:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func BytesSize(size float64) string {
|
2015-04-28 04:05:28 -04:00
|
|
|
return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
|
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-10-06 15:19:41 -04:00
|
|
|
// RAMInBytes 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-10-06 15:19:41 -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
|
|
|
}
|