add BytesSize in pkg/units
Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
parent
63363f2d49
commit
16f6e4744a
2 changed files with 23 additions and 5 deletions
|
@ -32,18 +32,26 @@ var (
|
|||
sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
|
||||
)
|
||||
|
||||
var unitAbbrs = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
||||
var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
||||
var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
||||
|
||||
// HumanSize returns a human-readable approximation of a size
|
||||
// using SI standard (eg. "44kB", "17MB")
|
||||
func HumanSize(size int64) string {
|
||||
return intToString(float64(size), 1000.0, decimapAbbrs)
|
||||
}
|
||||
|
||||
func BytesSize(size float64) string {
|
||||
return intToString(size, 1024.0, binaryAbbrs)
|
||||
}
|
||||
|
||||
func intToString(size, unit float64, _map []string) string {
|
||||
i := 0
|
||||
sizef := float64(size)
|
||||
for sizef >= 1000.0 {
|
||||
sizef = sizef / 1000.0
|
||||
for size >= unit {
|
||||
size = size / unit
|
||||
i++
|
||||
}
|
||||
return fmt.Sprintf("%.4g %s", sizef, unitAbbrs[i])
|
||||
return fmt.Sprintf("%.4g %s", size, _map[i])
|
||||
}
|
||||
|
||||
// FromHumanSize returns an integer from a human-readable specification of a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue