pkg/units: Moved 'units' out of function

No need to initialize every time the function executes since it works as
a catalog.

Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)
This commit is contained in:
Francisco Carriedo 2014-07-23 23:55:48 -07:00
parent 7595bd19d8
commit e8322a7fd7

View file

@ -16,17 +16,18 @@ func init() {
} }
} }
var bytePrefixes = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
// HumanSize returns a human-readable approximation of a size // HumanSize returns a human-readable approximation of a size
// using SI standard (eg. "44kB", "17MB") // using SI standard (eg. "44kB", "17MB")
func HumanSize(size int64) string { func HumanSize(size int64) string {
units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
i := 0 i := 0
sizef := float64(size) sizef := float64(size)
for sizef >= 1000.0 { for sizef >= 1000.0 {
sizef = sizef / 1000.0 sizef = sizef / 1000.0
i++ i++
} }
return fmt.Sprintf("%.4g %s", sizef, units[i]) return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
} }
// FromHumanSize returns an integer from a human-readable specification of a size // FromHumanSize returns an integer from a human-readable specification of a size