/
bytesize.go
147 lines (131 loc) · 2.57 KB
/
bytesize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package bytesize
import (
"errors"
"fmt"
"log"
"regexp"
"strconv"
)
const (
_ = 1 << (10 * iota)
KB
MB
GB
TB
PB
)
type ByteSize uint64
func (b ByteSize) UInt64() uint64 {
return uint64(b)
}
func (b ByteSize) AsInt() int {
return int(b)
}
func (b ByteSize) HumanString() string {
switch val := uint64(b); {
case val >= PB:
return fmt.Sprintf("%dPB", val/PB)
case val >= TB:
return fmt.Sprintf("%dTB", val/TB)
case val >= GB:
return fmt.Sprintf("%dGB", val/GB)
case val >= MB:
return fmt.Sprintf("%dMB", val/MB)
case val >= KB:
return fmt.Sprintf("%dKB", val/KB)
default:
return fmt.Sprintf("%d", val)
}
}
func (b ByteSize) MarshalText() ([]byte, error) {
if b == 0 {
return []byte("0"), nil
}
var abs = int64(b)
if abs < 0 {
abs = -abs
}
switch {
case abs%PB == 0:
val := b.UInt64() / PB
return []byte(fmt.Sprintf("%dpb", val)), nil
case abs%TB == 0:
val := b.UInt64() / TB
return []byte(fmt.Sprintf("%dtb", val)), nil
case abs%GB == 0:
val := b.UInt64() / GB
return []byte(fmt.Sprintf("%dgb", val)), nil
case abs%MB == 0:
val := b.UInt64() / MB
return []byte(fmt.Sprintf("%dmb", val)), nil
case abs%KB == 0:
val := b.UInt64() / KB
return []byte(fmt.Sprintf("%dkb", val)), nil
default:
return []byte(fmt.Sprintf("%d", b.UInt64())), nil
}
}
func (p *ByteSize) UnmarshalText(text []byte) error {
n, err := Parse(string(text))
if err != nil {
return err
}
*p = ByteSize(n)
return nil
}
var (
fullRegexp = regexp.MustCompile(`^\s*(\-?[\d\.]+)\s*([kmgtp]?b|[bkmgtp]|)\s*$`)
digitsOnly = regexp.MustCompile(`^\-?\d+$`)
)
var (
ErrBadByteSize = errors.New("invalid bytesize")
ErrBadByteSizeUnit = errors.New("invalid bytesize unit")
)
func Parse(s string) (uint64, error) {
if !fullRegexp.MatchString(s) {
return 0, ErrBadByteSize
}
subs := fullRegexp.FindStringSubmatch(s)
if len(subs) != 3 {
return 0, ErrBadByteSize
}
text := subs[1]
unit := subs[2]
size := uint64(1)
switch unit {
case "b", "":
case "k", "kb":
size = KB
case "m", "mb":
size = MB
case "g", "gb":
size = GB
case "t", "tb":
size = TB
case "p", "pb":
size = PB
default:
return 0, ErrBadByteSizeUnit
}
if digitsOnly.MatchString(text) {
n, err := strconv.ParseUint(text, 10, 64)
if err != nil {
return 0, ErrBadByteSize
}
size *= n
} else {
n, err := strconv.ParseFloat(text, 64)
if err != nil {
return 0, ErrBadByteSize
}
size = uint64(float64(size) * n)
}
return size, nil
}
func MustParse(s string) uint64 {
v, err := Parse(s)
if err != nil {
log.Panicf("parse bytesize failed: %s", err)
}
return v
}