mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
refactor: extract thirdparty/unit
This commit is contained in:
parent
121061b645
commit
de2cb5d8c7
46
thirdparty/unit/unit.go
vendored
Normal file
46
thirdparty/unit/unit.go
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
package unit
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Information int64
|
||||
|
||||
const (
|
||||
_ Information = iota // ignore first value by assigning to blank identifier
|
||||
KB = 1 << (10 * iota)
|
||||
MB
|
||||
GB
|
||||
TB
|
||||
PB
|
||||
EB
|
||||
)
|
||||
|
||||
func (i Information) String() string {
|
||||
|
||||
tmp := int64(i)
|
||||
|
||||
// default
|
||||
var d int64 = tmp
|
||||
symbol := "B"
|
||||
|
||||
switch {
|
||||
case i > EB:
|
||||
d = tmp / EB
|
||||
symbol = "EB"
|
||||
case i > PB:
|
||||
d = tmp / PB
|
||||
symbol = "PB"
|
||||
case i > TB:
|
||||
d = tmp / TB
|
||||
symbol = "TB"
|
||||
case i > GB:
|
||||
d = tmp / GB
|
||||
symbol = "GB"
|
||||
case i > MB:
|
||||
d = tmp / MB
|
||||
symbol = "MB"
|
||||
case i > KB:
|
||||
d = tmp / KB
|
||||
symbol = "KB"
|
||||
}
|
||||
return fmt.Sprintf("%d %s", d, symbol)
|
||||
}
|
||||
26
thirdparty/unit/unit_test.go
vendored
Normal file
26
thirdparty/unit/unit_test.go
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
package unit
|
||||
|
||||
import "testing"
|
||||
|
||||
// and the award for most meta goes to...
|
||||
|
||||
func TestByteSizeUnit(t *testing.T) {
|
||||
if 1*KB != 1*1024 {
|
||||
t.Fatal(1 * KB)
|
||||
}
|
||||
if 1*MB != 1*1024*1024 {
|
||||
t.Fail()
|
||||
}
|
||||
if 1*GB != 1*1024*1024*1024 {
|
||||
t.Fail()
|
||||
}
|
||||
if 1*TB != 1*1024*1024*1024*1024 {
|
||||
t.Fail()
|
||||
}
|
||||
if 1*PB != 1*1024*1024*1024*1024*1024 {
|
||||
t.Fail()
|
||||
}
|
||||
if 1*EB != 1*1024*1024*1024*1024*1024*1024 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user