1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-18 08:26:45 +02:00
woodpecker/vendor/github.com/docker/go-units/size.go

96 lines
2.4 KiB
Go
Raw Normal View History

2015-09-30 03:21:17 +02:00
package units
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// See: http://en.wikipedia.org/wiki/Binary_prefix
const (
// Decimal
KB = 1000
MB = 1000 * KB
GB = 1000 * MB
TB = 1000 * GB
PB = 1000 * TB
// Binary
KiB = 1024
MiB = 1024 * KiB
GiB = 1024 * MiB
TiB = 1024 * GiB
PiB = 1024 * TiB
)
type unitMap map[string]int64
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}
2016-03-07 21:23:49 +02:00
sizeRegex = regexp.MustCompile(`^(\d+(\.\d+)*) ?([kKmMgGtTpP])?[bB]?$`)
2015-09-30 03:21:17 +02: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"}
// CustomSize returns a human-readable approximation of a size
2016-03-07 21:23:49 +02:00
// using custom format.
2015-09-30 03:21:17 +02: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])
}
// HumanSize returns a human-readable approximation of a size
2016-03-07 21:23:49 +02:00
// capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
2015-09-30 03:21:17 +02:00
func HumanSize(size float64) string {
return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
}
2016-03-07 21:23:49 +02:00
// BytesSize returns a human-readable size in bytes, kibibytes,
// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
2015-09-30 03:21:17 +02:00
func BytesSize(size float64) string {
return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
}
// FromHumanSize returns an integer from a human-readable specification of a
2016-03-07 21:23:49 +02:00
// size using SI standard (eg. "44kB", "17MB").
2015-09-30 03:21:17 +02:00
func FromHumanSize(size string) (int64, error) {
return parseSize(size, decimalMap)
}
// RAMInBytes parses a human-readable string representing an amount of RAM
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
// returns the number of bytes, or -1 if the string is unparseable.
// Units are case-insensitive, and the 'b' suffix is optional.
func RAMInBytes(size string) (int64, error) {
return parseSize(size, binaryMap)
}
2016-03-07 21:23:49 +02:00
// Parses the human-readable size string into the amount it represents.
2015-09-30 03:21:17 +02:00
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
matches := sizeRegex.FindStringSubmatch(sizeStr)
2016-03-07 21:23:49 +02:00
if len(matches) != 4 {
2015-09-30 03:21:17 +02:00
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
}
2016-03-07 21:23:49 +02:00
size, err := strconv.ParseFloat(matches[1], 64)
2015-09-30 03:21:17 +02:00
if err != nil {
return -1, err
}
2016-03-07 21:23:49 +02:00
unitPrefix := strings.ToLower(matches[3])
2015-09-30 03:21:17 +02:00
if mul, ok := uMap[unitPrefix]; ok {
2016-03-07 21:23:49 +02:00
size *= float64(mul)
2015-09-30 03:21:17 +02:00
}
2016-03-07 21:23:49 +02:00
return int64(size), nil
2015-09-30 03:21:17 +02:00
}