Browse code

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)

Francisco Carriedo authored on 2014/07/24 15:55:48
Showing 1 changed files
... ...
@@ -16,17 +16,18 @@ func init() {
16 16
 	}
17 17
 }
18 18
 
19
+var bytePrefixes = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
20
+
19 21
 // HumanSize returns a human-readable approximation of a size
20 22
 // using SI standard (eg. "44kB", "17MB")
21 23
 func HumanSize(size int64) string {
22
-	units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
23 24
 	i := 0
24 25
 	sizef := float64(size)
25 26
 	for sizef >= 1000.0 {
26 27
 		sizef = sizef / 1000.0
27 28
 		i++
28 29
 	}
29
-	return fmt.Sprintf("%.4g %s", sizef, units[i])
30
+	return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
30 31
 }
31 32
 
32 33
 // FromHumanSize returns an integer from a human-readable specification of a size