2014-05-12 23:40:19 +00:00
|
|
|
package units
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-22 21:23:52 +00:00
|
|
|
const (
|
|
|
|
decimalKUnit = 1000
|
|
|
|
binaryKUnit = 1024
|
|
|
|
)
|
|
|
|
|
2014-07-22 06:50:27 +00:00
|
|
|
var sizeRegex *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
2014-07-24 19:34:11 +00:00
|
|
|
sizeRegex = regexp.MustCompile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
|
2014-07-22 06:50:27 +00:00
|
|
|
}
|
|
|
|
|
2014-07-24 06:55:48 +00:00
|
|
|
var bytePrefixes = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
|
|
|
2014-05-12 23:40:19 +00:00
|
|
|
// HumanSize returns a human-readable approximation of a size
|
|
|
|
// using SI standard (eg. "44kB", "17MB")
|
|
|
|
func HumanSize(size int64) string {
|
2014-07-24 06:53:16 +00:00
|
|
|
i := 0
|
|
|
|
sizef := float64(size)
|
2014-05-12 23:40:19 +00:00
|
|
|
for sizef >= 1000.0 {
|
|
|
|
sizef = sizef / 1000.0
|
|
|
|
i++
|
|
|
|
}
|
2014-07-24 06:55:48 +00:00
|
|
|
return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
|
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
|
|
|
|
// size using SI standard (eg. "44kB", "17MB")
|
2014-03-31 07:30:20 +00:00
|
|
|
func FromHumanSize(size string) (int64, error) {
|
2014-07-22 21:23:52 +00:00
|
|
|
return parseSize(size, decimalKUnit)
|
2014-03-31 07:30:20 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2014-07-22 19:16:50 +00:00
|
|
|
func RAMInBytes(size string) (int64, error) {
|
2014-07-22 21:23:52 +00:00
|
|
|
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) {
|
2014-07-22 06:50:27 +00:00
|
|
|
matches := sizeRegex.FindStringSubmatch(size)
|
2014-05-12 23:40:19 +00:00
|
|
|
|
|
|
|
if len(matches) != 3 {
|
|
|
|
return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
|
|
}
|
|
|
|
|
2014-07-22 21:23:52 +00:00
|
|
|
theSize, 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-05-12 23:40:19 +00:00
|
|
|
|
2014-07-22 21:23:52 +00:00
|
|
|
switch unitPrefix {
|
2014-07-22 07:38:47 +00:00
|
|
|
case "k":
|
2014-07-22 21:23:52 +00:00
|
|
|
theSize *= kUnit
|
2014-07-22 07:38:47 +00:00
|
|
|
case "m":
|
2014-07-22 21:23:52 +00:00
|
|
|
theSize *= kUnit * kUnit
|
2014-07-22 07:38:47 +00:00
|
|
|
case "g":
|
2014-07-22 21:23:52 +00:00
|
|
|
theSize *= kUnit * kUnit * kUnit
|
2014-07-22 07:38:47 +00:00
|
|
|
case "t":
|
2014-07-22 21:23:52 +00:00
|
|
|
theSize *= kUnit * kUnit * kUnit * kUnit
|
2014-07-22 06:49:52 +00:00
|
|
|
case "p":
|
2014-07-22 21:23:52 +00:00
|
|
|
theSize *= kUnit * kUnit * kUnit * kUnit * kUnit
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:23:52 +00:00
|
|
|
return theSize, nil
|
2014-05-12 23:40:19 +00:00
|
|
|
}
|