Skip to content

Commit c5a5d74

Browse files
committed
Changed bytes to gytes
Signed-off-by: Vishal Rana <[email protected]>
1 parent add51d7 commit c5a5d74

File tree

6 files changed

+144
-98
lines changed

6 files changed

+144
-98
lines changed

bytes/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

bytes/bytes.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

bytes/bytes_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

gytes/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Gytes
2+
3+
Format bytes to string
4+
5+
## Installation
6+
7+
```go
8+
go get github.com/labstack/gommon/gytes
9+
```
10+
11+
## [Usage](https://github.com/labstack/gommon/blob/master/gytes/gytes_test.go)
12+
13+
```sh
14+
import github.com/labstack/gommon/gytes
15+
```
16+
17+
### Decimal prefix
18+
19+
```go
20+
fmt.Println(gytes.Format(1323))
21+
```
22+
23+
`1.32 KB`
24+
25+
### Binary prefix
26+
27+
```go
28+
gytes.BinaryPrefix(true)
29+
fmt.Println(gytes.Format(1323))
30+
```
31+
32+
`1.29 KiB`
33+
34+
Above examples operate on global instance of Gytes. To create a new instance
35+
36+
```go
37+

gytes/gytes.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package bytes
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"strconv"
7+
)
8+
9+
var (
10+
global = New()
11+
)
12+
13+
type (
14+
Gytes struct {
15+
iec bool
16+
}
17+
)
18+
19+
func New() *Gytes {
20+
return &Gytes{}
21+
}
22+
23+
// BinaryPrefix turns on binary prefix format.
24+
func (g *Gytes) BinaryPrefix(on bool) {
25+
g.iec = on
26+
}
27+
28+
// Format formats bytes to string. For example, 1323 bytes will return 1.32 KB.
29+
// If binary prefix is set, it will return 1.29 KiB.
30+
func (g *Gytes) Format(b uint64) string {
31+
unit := uint64(1000)
32+
if g.iec {
33+
unit = 1024
34+
}
35+
if b < unit {
36+
return strconv.FormatUint(b, 10) + " B"
37+
} else {
38+
b := float64(b)
39+
unit := float64(unit)
40+
x := math.Floor(math.Log(b) / math.Log(unit))
41+
pre := make([]byte, 1, 2)
42+
pre[0] = "KMGTPE"[uint8(x)-1]
43+
if g.iec {
44+
pre = pre[:2]
45+
pre[1] = 'i'
46+
}
47+
// TODO: Improve performance?
48+
return fmt.Sprintf("%.02f %sB", b/math.Pow(unit, x), pre)
49+
}
50+
}
51+
52+
// BinaryPrefix wraps default instance's BinaryPrefix function.
53+
func BinaryPrefix(on bool) {
54+
global.BinaryPrefix(on)
55+
}
56+
57+
// Format wraps default instance's Format function.
58+
func Format(b uint64) string {
59+
return global.Format(b)
60+
}

gytes/gytes_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package bytes
2+
3+
import "testing"
4+
5+
func TestGytes(t *testing.T) {
6+
// B
7+
b := Format(515)
8+
if b != "515 B" {
9+
t.Errorf("expected `515 B`, got %s", b)
10+
}
11+
12+
// MB
13+
b = Format(13231323)
14+
if b != "13.23 MB" {
15+
t.Errorf("expected `13.23 MB`, got %s", b)
16+
}
17+
18+
// Exact
19+
b = Format(1000 * 1000 * 1000)
20+
if b != "1.00 GB" {
21+
t.Errorf("expected `1.00 GB`, got %s", b)
22+
}
23+
24+
// Binary prefix
25+
BinaryPrefix(true)
26+
b = Format(1323)
27+
if b != "1.29 KiB" {
28+
t.Errorf("expected `1.29 KiB`, got %s", b)
29+
}
30+
}
31+
32+
func TestNew(t *testing.T) {
33+
g := New()
34+
35+
b := g.Format(132313231323)
36+
if b != "132.31 GB" {
37+
t.Errorf("expected `132.31 GB`, got %s", b)
38+
}
39+
40+
// Binary prefix
41+
g.BinaryPrefix(true)
42+
b = Format(1323132313231323)
43+
if b != "1.18 PiB" {
44+
t.Errorf("expected `1.18 PiB`, got %s", b)
45+
}
46+
}
47+

0 commit comments

Comments
 (0)