-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.go
317 lines (294 loc) · 7.37 KB
/
size.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package ox
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// ParseSize parses a byte size string.
func ParseSize(s string) (Size, error) {
m := sizeRE.FindStringSubmatch(s)
if m == nil {
return 0, fmt.Errorf("%w %q", ErrInvalidSize, s)
}
f, err := strconv.ParseFloat(m[2], 64)
switch {
case err != nil:
return 0, fmt.Errorf("%w: %w", ErrInvalidSize, err)
case m[1] == "-":
f = -f
}
sz, err := parseSize(m[3])
if err != nil {
return 0, fmt.Errorf("%w: %w", ErrInvalidSize, err)
}
return Size(f * float64(sz)), nil
}
// ParseRate parses a byte rate string.
func ParseRate(s string) (Rate, error) {
unit := time.Second
if i := strings.LastIndexByte(s, '/'); i != -1 {
switch strings.ToLower(s[i+1:]) {
// unitMap is the duration unit map.
case "ns":
unit = time.Nanosecond
case "us", "µs", "μs": // U+00B5 = micro symbol, U+03BC = Greek letter mu
unit = time.Microsecond
case "ms":
unit = time.Millisecond
case "s":
unit = time.Second
case "m":
unit = time.Minute
case "h":
unit = time.Hour
default:
return Rate{}, fmt.Errorf("%w %q", ErrInvalidRate, s)
}
s = s[:i]
}
sz, err := ParseSize(s)
if err != nil {
return Rate{}, err
}
return Rate{sz, unit}, nil
}
// AppendSize appends the formatted size to b. A precision below -1 will format
// the value to the fixed precision then trims any trailing '.' / '0'. Formats
// the value based on the verb (see below). When space is true, a space will be
// appended between the formatted size and the suffix.
//
// Supported verbs:
//
// d/D - size in bytes (ex: 12345678)
// f/F - size in best fitting *B/*iB (ex: 999 B, 1.1 KiB) and always with a space
// z/Z - size in best fitting *B/*iB
// k/K - size in KB/KiB (ex: 0.9 kB, 2.3 KiB)
// m/M - size in MB/MiB (ex: 1.2345 MB)
// g/G - size in GB/GiB (ex: 1 GiB)
// t/T - size in TB/TiB (ex: 4.5 TiB)
// p/P - size in PB/PiB (ex: 4.5 PiB)
// s/S - same as f/F
// v/V - same as f/F
func AppendSize(b []byte, size int64, verb rune, prec int, space bool) []byte {
neg, sz, unit := "", float64(size), "B"
switch verb {
case 'd', 'D':
return strconv.AppendInt(b, size, 10)
case 'k':
sz, unit = sz/KiB, "KiB"
case 'K':
sz, unit = sz/KB, "kB"
case 'm':
sz, unit = sz/MiB, "MiB"
case 'M':
sz, unit = sz/MB, "MB"
case 'g':
sz, unit = sz/GiB, "GiB"
case 'G':
sz, unit = sz/GB, "GB"
case 't':
sz, unit = sz/TiB, "TiB"
case 'T':
sz, unit = sz/TB, "TB"
case 'b':
sz, unit = sz/PiB, "PiB"
case 'P':
sz, unit = sz/PB, "PB"
case 'z':
neg, sz, unit = bestSize(size, true)
case 'Z':
neg, sz, unit = bestSize(size, false)
case 'f', 's', 'v':
neg, sz, unit = bestSize(size, true)
prec, space = DefaultSizePrec, true
case 'F', 'S', 'V':
neg, sz, unit = bestSize(size, false)
prec, space = DefaultSizePrec, true
default:
return append(b, "%"+string(verb)+"(error=unknown size verb)"...)
}
aprec := prec
if aprec < -1 {
aprec = -aprec
}
b = append(b, neg...)
b = strconv.AppendFloat(b, sz, 'f', aprec, 64)
// trim right {.,0}
if prec < -1 {
b = bytes.TrimRightFunc(b, func(r rune) bool {
return r == '0'
})
b = bytes.TrimRightFunc(b, func(r rune) bool {
return r == '.'
})
}
if space {
b = append(b, ' ')
}
return append(b, unit...)
}
func bestSize(size int64, iec bool) (string, float64, string) {
n, units, suffix := int64(KB), "kMGTPE", "B"
if iec {
n, units, suffix = KiB, "KMGTPE", "iB"
}
var neg string
if size < 0 {
neg, size = "-", -size
}
if size < n {
return neg, float64(size), "B"
}
e, d := 0, n
for i := size / n; n <= i; i /= n {
d *= n
e++
}
return neg, float64(size) / float64(d), string(units[e]) + suffix
}
// FormatSize formats a byte size.
func FormatSize(size int64, verb rune, prec int, space bool) string {
return string(AppendSize(make([]byte, 0, 28), size, verb, prec, space))
}
// AppendRate appends the formatted rate to b.
func AppendRate(b []byte, rate Rate, verb rune, prec int, space bool) []byte {
return append(append(AppendSize(b, int64(rate.Size), verb, prec, space), '/'), unitString(rate.Unit)...)
}
// FormatRate formats a byte rate.
func FormatRate(rate Rate, verb rune, prec int, space bool) string {
return string(AppendRate(make([]byte, 0, 31), rate, verb, prec, space))
}
// Size is a byte size.
type Size int64
// NewSize creates a byte size.
func NewSize[T inti | uinti](size T) Size {
return Size(size)
}
// Format satisfies the [fmt.Formatter] interface. See [AppendSize] for
// recognized verbs.
func (size Size) Format(f fmt.State, verb rune) {
prec := DefaultSizePrec
if p, ok := f.Precision(); ok {
prec = p
}
_, _ = f.Write(AppendSize(make([]byte, 0, 28), int64(size), verb, prec, f.Flag(' ')))
}
// MarshalText satisfies the [BinaryMarshalUnmarshaler] interface.
func (size Size) MarshalText() ([]byte, error) {
return AppendSize(make([]byte, 0, 28), int64(size), 'z', -2, true), nil
}
// UnmarshalText satisfies the [BinaryMarshalUnmarshaler] interface.
func (size *Size) UnmarshalText(b []byte) error {
i, err := ParseSize(string(b))
if err != nil {
return err
}
*size = Size(i)
return nil
}
// Rate is a byte rate.
type Rate struct {
Size Size
Unit time.Duration
}
// NewRate creates a byte rate.
func NewRate[T inti | uinti](size T, unit time.Duration) Rate {
return Rate{
Size: NewSize(size),
Unit: unit,
}
}
// Format satisfies the [fmt.Formatter] interface. See [AppendRate] for
// recognized verbs.
func (rate Rate) Format(f fmt.State, verb rune) {
prec := DefaultSizePrec
if p, ok := f.Precision(); ok {
prec = p
}
_, _ = f.Write(AppendRate(make([]byte, 0, 31), rate, verb, prec, f.Flag(' ')))
}
// Int64 returns the bytes as an int64.
func (rate Rate) Int64() int64 {
return int64(rate.Size)
}
// UnmarshalText satisfies the [BinaryMarshalUnmarshaler] interface.
func (rate *Rate) UnmarshalText(b []byte) error {
var err error
*rate, err = ParseRate(string(b))
return err
}
// MarshalText satisfies the [BinaryMarshalUnmarshaler] interface.
func (rate *Rate) MarshalText() ([]byte, error) {
return AppendRate(make([]byte, 0, 31), *rate, 'z', -2, true), nil
}
// Byte sizes.
const (
B = 1
KB = 1_000
MB = 1_000_000
GB = 1_000_000_000
TB = 1_000_000_000_000
PB = 1_000_000_000_000_000
EB = 1_000_000_000_000_000_000
KiB = 1_024
MiB = 1_048_576
GiB = 1_073_741_824
TiB = 1_099_511_627_776
PiB = 1_125_899_906_842_624
EiB = 1_152_921_504_606_846_976
)
// parseSize returns the byte size of s.
func parseSize(s string) (int64, error) {
switch strings.ToLower(s) {
case "", "b":
return B, nil
case "kb":
return KB, nil
case "mb":
return MB, nil
case "gb":
return GB, nil
case "tb":
return TB, nil
case "pb":
return PB, nil
case "eb":
return EB, nil
case "kib":
return KiB, nil
case "mib":
return MiB, nil
case "gib":
return GiB, nil
case "tib":
return TiB, nil
case "pib":
return PiB, nil
case "eib":
return EiB, nil
}
return 0, fmt.Errorf("%w %q", ErrUnknownSize, s)
}
// unitString returns the string for a time unit (duration).
func unitString(unit time.Duration) string {
switch {
case unit == 0, unit > time.Hour:
return unitString(DefaultRateUnit)
case unit > time.Minute:
return "h"
case unit > time.Second:
return "m"
case unit > time.Millisecond:
return "s"
case unit > time.Microsecond:
return "ms"
case unit > time.Nanosecond:
return "µs"
}
return "ns"
}
// sizeRE matches sizes.
var sizeRE = regexp.MustCompile(`(?i)^([-+])?([0-9]+(?:\.[0-9]*)?)(?: ?([a-z]+))?$`)