Skip to content

Commit

Permalink
add BytesSize in pkg/units
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Vieux <[email protected]>
  • Loading branch information
vieux committed Oct 14, 2014
1 parent e4976b8 commit ae4689f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/units/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions pkg/units/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"testing"
)

func TestBytesSize(t *testing.T) {
assertEquals(t, "1 KiB", BytesSize(1024))
assertEquals(t, "1 MiB", BytesSize(1024*1024))
assertEquals(t, "1 MiB", BytesSize(1048576))
assertEquals(t, "2 MiB", BytesSize(2*MiB))
assertEquals(t, "3.42 GiB", BytesSize(3.42*GiB))
assertEquals(t, "5.372 TiB", BytesSize(5.372*TiB))
assertEquals(t, "2.22 PiB", BytesSize(2.22*PiB))
}

func TestHumanSize(t *testing.T) {
assertEquals(t, "1 kB", HumanSize(1000))
assertEquals(t, "1.024 kB", HumanSize(1024))
Expand Down

0 comments on commit ae4689f

Please sign in to comment.