Browse Source

Changed bytes to gytes

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana 10 năm trước cách đây
mục cha
commit
c5a5d7440d
6 tập tin đã thay đổi với 144 bổ sung98 xóa
  1. 0 31
      bytes/README.md
  2. 0 37
      bytes/bytes.go
  3. 0 30
      bytes/bytes_test.go
  4. 37 0
      gytes/README.md
  5. 60 0
      gytes/gytes.go
  6. 47 0
      gytes/gytes_test.go

+ 0 - 31
bytes/README.md

@@ -1,31 +0,0 @@
-# Bytes
-
-Format bytes to string
-
-## Installation
-
-```go
-go get github.com/labstack/gommon/bytes
-```
-
-## [Usage](https://github.com/labstack/gommon/blob/master/bytes/bytes_test.go)
-
-```sh
-import gytes github.com/labstack/gommon/bytes
-```
-
-### Decimal prefix
-
-```go
-fmt.Println(gytes.Format(1323))
-```
-
-`1.32 KB`
-
-### Binary prefix
-
-```go
-fmt.Println(gytes.FormatB(1323))
-```
-
-`1.29 KiB`

+ 0 - 37
bytes/bytes.go

@@ -1,37 +0,0 @@
-package bytes
-
-import (
-	"fmt"
-	"math"
-)
-
-// Format formats bytes to string with decimal prefix. For example, 1000 would returns
-// 1 KB
-func Format(b uint64) string {
-	return format(float64(b), false)
-}
-
-// FormatB formats bytes to string with binary prefix. For example, 1024 would
-// return 1 KiB.
-func FormatB(b uint64) string {
-	return format(float64(b), true)
-}
-
-func format(b float64, bin bool) string {
-	unit := float64(1000)
-	if bin {
-		unit = 1024
-	}
-	if b < unit {
-		return fmt.Sprintf("%.0f B", b)
-	} else {
-		x := math.Floor(math.Log(b) / math.Log(unit))
-		pre := make([]byte, 1, 2)
-		pre[0] = "KMGTPE"[uint8(x)-1]
-		if bin {
-			pre = pre[:2]
-			pre[1] = 'i'
-		}
-		return fmt.Sprintf("%.02f %sB", b/math.Pow(unit, x), pre)
-	}
-}

+ 0 - 30
bytes/bytes_test.go

@@ -1,30 +0,0 @@
-package bytes
-
-import "testing"
-
-func TestFormat(t *testing.T) {
-	// B
-	f := Format(515)
-	if f != "515 B" {
-		t.Errorf("formatted bytes should be 515 B, found %s", f)
-	}
-
-	// MB
-	f = Format(13231323)
-	if f != "13.23 MB" {
-		t.Errorf("formatted bytes should be 13.23 MB, found %s", f)
-	}
-
-	// Exact
-	f = Format(1000 * 1000 * 1000)
-	if f != "1.00 GB" {
-		t.Errorf("formatted bytes should be 1.00 GB, found %s", f)
-	}
-}
-
-func TestFormatB(t *testing.T) {
-	f := FormatB(1323)
-	if f != "1.29 KiB" {
-		t.Errorf("formatted bytes should be 1.29 KiB, found %s", f)
-	}
-}

+ 37 - 0
gytes/README.md

@@ -0,0 +1,37 @@
+# Gytes
+
+Format bytes to string
+
+## Installation
+
+```go
+go get github.com/labstack/gommon/gytes
+```
+
+## [Usage](https://github.com/labstack/gommon/blob/master/gytes/gytes_test.go)
+
+```sh
+import github.com/labstack/gommon/gytes
+```
+
+### Decimal prefix
+
+```go
+fmt.Println(gytes.Format(1323))
+```
+
+`1.32 KB`
+
+### Binary prefix
+
+```go
+gytes.BinaryPrefix(true)
+fmt.Println(gytes.Format(1323))
+```
+
+`1.29 KiB`
+
+Above examples operate on global instance of Gytes. To create a new instance
+
+```go
+

+ 60 - 0
gytes/gytes.go

@@ -0,0 +1,60 @@
+package bytes
+
+import (
+	"fmt"
+	"math"
+	"strconv"
+)
+
+var (
+	global = New()
+)
+
+type (
+	Gytes struct {
+		iec bool
+	}
+)
+
+func New() *Gytes {
+	return &Gytes{}
+}
+
+// BinaryPrefix turns on binary prefix format.
+func (g *Gytes) BinaryPrefix(on bool) {
+	g.iec = on
+}
+
+// Format formats bytes to string. For example, 1323 bytes will return 1.32 KB.
+// If binary prefix is set, it will return 1.29 KiB.
+func (g *Gytes) Format(b uint64) string {
+	unit := uint64(1000)
+	if g.iec {
+		unit = 1024
+	}
+	if b < unit {
+		return strconv.FormatUint(b, 10) + " B"
+	} else {
+		b := float64(b)
+		unit := float64(unit)
+		x := math.Floor(math.Log(b) / math.Log(unit))
+		pre := make([]byte, 1, 2)
+		pre[0] = "KMGTPE"[uint8(x)-1]
+		if g.iec {
+			pre = pre[:2]
+			pre[1] = 'i'
+		}
+		// TODO: Improve performance?
+		return fmt.Sprintf("%.02f %sB", b/math.Pow(unit, x), pre)
+	}
+}
+
+// BinaryPrefix wraps default instance's BinaryPrefix function.
+func BinaryPrefix(on bool) {
+	global.BinaryPrefix(on)
+}
+
+// Format wraps default instance's Format function.
+func Format(b uint64) string {
+	return global.Format(b)
+}

+ 47 - 0
gytes/gytes_test.go

@@ -0,0 +1,47 @@
+package bytes
+
+import "testing"
+
+func TestGytes(t *testing.T) {
+	// B
+	b := Format(515)
+	if b != "515 B" {
+		t.Errorf("expected `515 B`, got %s", b)
+	}
+
+	// MB
+	b = Format(13231323)
+	if b != "13.23 MB" {
+		t.Errorf("expected `13.23 MB`, got %s", b)
+	}
+
+	// Exact
+	b = Format(1000 * 1000 * 1000)
+	if b != "1.00 GB" {
+		t.Errorf("expected `1.00 GB`, got %s", b)
+	}
+
+	// Binary prefix
+	BinaryPrefix(true)
+	b = Format(1323)
+	if b != "1.29 KiB" {
+		t.Errorf("expected `1.29 KiB`, got %s", b)
+	}
+}
+
+func TestNew(t *testing.T) {
+	g := New()
+
+	b := g.Format(132313231323)
+	if b != "132.31 GB" {
+		t.Errorf("expected `132.31 GB`, got %s", b)
+	}
+
+	// Binary prefix
+	g.BinaryPrefix(true)
+	b = Format(1323132313231323)
+	if b != "1.18 PiB" {
+		t.Errorf("expected `1.18 PiB`, got %s", b)
+	}
+}
+