-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte.go
More file actions
195 lines (182 loc) · 4.12 KB
/
byte.go
File metadata and controls
195 lines (182 loc) · 4.12 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package bytefmt provides implementation of a fmt.Formatter for bytes.
package bytefmt
import (
"fmt"
"math"
"strconv"
)
const (
Byte uint64 = 1 << (10 * iota)
Kilobyte
Megabyte
Gigabyte
Terabyte
Petabyte
Exabyte
)
type Bytes struct {
Value uint64
names []string
}
func New(v uint64, n ...string) Bytes {
b := Bytes{Value: v}
b.Names(n...)
return b
}
var names = []string{"B", "K", "M", "G", "T", "P", "E"}
func (b *Bytes) Names(n ...string) []string {
if len(n) == 0 {
if len(b.names) == 0 {
b.names = names
}
return b.names
}
b.names = n
if len(b.names) < len(names) {
b.names = append(b.names, names[len(b.names):]...)
}
return b.names
}
func (b Bytes) String() string {
var (
f float64
i int
)
if b.Value >= Exabyte {
f = b.float64()
i = 6
} else if b.Value >= Petabyte {
f = b.float64()
i = 5
} else if b.Value >= Terabyte {
f = b.float64()
i = 4
} else if b.Value >= Gigabyte {
f = b.float64()
i = 3
} else if b.Value >= Megabyte {
f = b.float64()
i = 2
} else if b.Value >= Kilobyte {
f = b.float64()
i = 1
} else {
f = b.float64()
i = 0
}
return fmt.Sprintf("%v%s", f, names[i])
}
/*
Other flags:
+ always print a sign for numeric values (%+q);
- pad with spaces on the right rather than the left (left-justify the field)
' ' (space) leave a space between value and units (% d);
0 pad with leading zeros rather than spaces;
*/
func (b Bytes) Format(f fmt.State, c rune) {
var (
v interface{} // value
vf = "%" // value format
uf = "%" // unit format
pf = "" // padding format
u = b.name() // unit of measure name
uw = len([]rune(u)) // unit name width
)
if f.Flag('+') {
vf += "+"
}
if f.Flag('#') {
vf += "#"
}
w, ok := f.Width()
if !ok {
w = -1
}
if f.Flag(' ') {
if w == -1 {
uf += strconv.Itoa(uw + 1)
} else {
uf += strconv.Itoa(uw + w)
}
} else if w != -1 {
pf += "%"
if f.Flag('-') {
pf += "-"
} else if f.Flag('0') {
pf += "0"
}
pf += strconv.Itoa(w) + "s"
}
if p, ok := f.Precision(); ok {
vf += "." + strconv.Itoa(p)
}
vf += string(c)
uf += "s"
var s string
if c == 's' || c == 'q' {
v = fmt.Sprintf("%v", b.float64())
s = fmt.Sprint(v) + fmt.Sprintf(uf, u)
if pf != "" {
s = fmt.Sprintf(pf, s)
}
s = fmt.Sprintf(vf, s)
} else {
if c == 'd' {
v = int64(math.Round(b.float64()))
} else {
v = b.float64()
}
s = fmt.Sprintf(vf+uf, v, u)
if pf != "" {
s = fmt.Sprintf(pf, s)
}
}
f.Write([]byte(s))
}
// float64 returns returns a number in units of measure
// when converted to which the smallest integer is obtained
func (b Bytes) float64() float64 {
if b.Value >= Exabyte {
return b.exabytes()
} else if b.Value >= Petabyte {
return b.petabytes()
} else if b.Value >= Terabyte {
return b.terabytes()
} else if b.Value >= Gigabyte {
return b.gigabytes()
} else if b.Value >= Megabyte {
return b.megabytes()
} else if b.Value >= Kilobyte {
return b.kilobytes()
} else {
return float64(b.Value)
}
}
func (b Bytes) kilobytes() float64 { return float64(b.Value) / float64(Kilobyte) }
func (b Bytes) megabytes() float64 { return float64(b.Value) / float64(Megabyte) }
func (b Bytes) gigabytes() float64 { return float64(b.Value) / float64(Gigabyte) }
func (b Bytes) terabytes() float64 { return float64(b.Value) / float64(Terabyte) }
func (b Bytes) petabytes() float64 { return float64(b.Value) / float64(Petabyte) }
func (b Bytes) exabytes() float64 { return float64(b.Value) / float64(Exabyte) }
// name returns the name of the unit of measure
// when converted to which the smallest integer is obtained
func (b Bytes) name() string {
if b.Value >= Exabyte {
return b.names[6]
} else if b.Value >= Petabyte {
return b.names[5]
} else if b.Value >= Terabyte {
return b.names[4]
} else if b.Value >= Gigabyte {
return b.names[3]
} else if b.Value >= Megabyte {
return b.names[2]
} else if b.Value >= Kilobyte {
return b.names[1]
} else {
return b.names[0]
}
}