This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
/
unit.go
276 lines (253 loc) · 6.88 KB
/
unit.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
// Copyright 2010 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.
// Ported from https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/time/format.go
// by Michal Vyskocil michal.vyskocil@gmail.com
package internal
import (
"errors"
"fmt"
"math"
"strconv"
)
// Unit represents the units with multiplier suffixes. Is defined as float64 to support
// things like YottaByte and so. It is generalized version of time.Duration
type Unit float64
const (
maxUnit float64 = math.MaxFloat64 / 10
)
var (
ErrEmpty = errors.New("empty")
ErrInvalidNextCharacter = errors.New("expected number or decimal separator")
ErrOverflow = errors.New("overflow")
ErrNoDigits = errors.New("no digits")
)
// parseUnit was ported from time.ParseDuration, generalized to arbitrary suffixes
// and works with float64
// https://cs.opensource.google/go/go/+/refs/tags/go1.18.4:src/time/format.go;l=1511
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
func parseUnit(unitMap map[string]float64, s string) (Unit, error) {
orig := s
var d float64
neg := false
// Consume [-+]?
if s != "" {
c := s[0]
if c == '-' || c == '+' {
neg = c == '-'
s = s[1:]
}
}
// Special case: if all that is left is "0", this is zero.
if s == "0" {
return 0, nil
}
if s == "" {
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrEmpty)
}
for s != "" {
var (
v, f float64 // integers before, after decimal point
scale float64 = 1 // value = v + f/scale
)
var err error
// The next character must be [0-9.]
if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') {
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrInvalidNextCharacter)
}
// Consume [0-9]*
pl := len(s)
v, s, err = leadingInt(s)
if err != nil {
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrOverflow)
}
pre := pl != len(s) // whether we consumed anything before a period
// Consume (\.[0-9]*)?
post := false
if s != "" && s[0] == '.' {
s = s[1:]
pl := len(s)
f, scale, s = leadingFraction(s)
post = pl != len(s)
}
if !pre && !post {
// no digits (e.g. ".s" or "-.s")
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrNoDigits)
}
// Consume unit.
i := 0
for ; i < len(s); i++ {
c := s[i]
if c == '.' || '0' <= c && c <= '9' {
break
}
}
var unit float64
if i == 0 {
unit = 1.0
} else {
u := s[:i]
s = s[i:]
var ok bool
unit, ok = unitMap[u]
if !ok {
return 0, fmt.Errorf("unknown unit %q in size %q", u, orig)
}
}
if v > maxUnit/unit {
// overflow
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrOverflow)
}
v *= unit
if f > 0 {
// float64 is needed to be nanosecond accurate for fractions of hours.
// v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit)
v += float64(float64(f) * (float64(unit) / scale))
if v > maxUnit {
// overflow
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrOverflow)
}
}
d += v
if d > maxUnit {
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrOverflow)
}
}
if neg {
return -Unit(d), nil
}
if d > maxUnit-1 {
return 0, fmt.Errorf("invalid size %q: %w", orig, ErrOverflow)
}
return Unit(d), nil
}
var errLeadingInt = errors.New("bad [0-9]*") // never printed
// leadingInt consumes the leading [0-9]* from s.
func leadingInt(s string) (x float64, rem string, err error) {
i := 0
for ; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
break
}
}
if i == 0 {
return 0, s[0:], nil
}
x, err = strconv.ParseFloat(s[:i], 64)
if err != nil {
return 0, "", errLeadingInt
}
return x, s[i:], nil
}
// leadingFraction consumes the leading [0-9]* from s.
// It is used only for fractions, so does not return an error on overflow,
// it just stops accumulating precision.
func leadingFraction(s string) (x float64, scale float64, rem string) {
i := 0
scale = 1
overflow := false
for ; i < len(s); i++ {
c := s[i]
if c < '0' || c > '9' {
break
}
if overflow {
continue
}
if x > (maxUnit-1)/10 {
// It's possible for overflow to give a positive number, so take care.
overflow = true
continue
}
y := x*10 + float64(c) - '0'
if y > maxUnit {
overflow = true
continue
}
x = y
scale *= 10
}
return x, scale, s[i:]
}
// Byte is a size of disk/memory/buffer capacities
type Byte Unit
// Byte may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M
// 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
// Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
// see https://man7.org/linux/man-pages/man1/head.1.html
const (
Block Byte = 512 // b
KiloByte = 1000 // kB
KibiByte = 1024 // K/KiB
MegaByte = 1000 * KiloByte // MB
MebiByte = 1024 * KibiByte // M/MiB
GigaByte = 1000 * MegaByte // GB
GibiByte = 1024 * MebiByte // G/GiB
TeraByte = 1000 * GigaByte // TB
TebiByte = 1024 * GibiByte // T/TiB
PetaByte = 1000 * TeraByte // PB
PebiByte = 1024 * TebiByte // P/PiB
ExaByte = 1000 * PetaByte // ZB
ExbiByte = 1024 * PebiByte // Z/ZiB
ZettaByte = 1000 * ExaByte // ZB
ZebiByte = 1024 * ExbiByte // Z/ZiB
YottaByte = 1000 * ZettaByte // YB
YobiByte = 1024 * ZebiByte // Y/YiB
)
var ByteSuffixes = map[string]float64{
"b": float64(Block),
"kB": float64(KiloByte),
"K": float64(KibiByte),
"KiB": float64(KibiByte),
"MB": float64(MegaByte),
"M": float64(MebiByte),
"MiB": float64(MebiByte),
"GB": float64(GigaByte),
"G": float64(GibiByte),
"GiB": float64(GibiByte),
"TB": float64(TeraByte),
"T": float64(TebiByte),
"TiB": float64(TebiByte),
"PB": float64(PetaByte),
"P": float64(PebiByte),
"PiB": float64(PebiByte),
"EB": float64(ExaByte),
"E": float64(ExbiByte),
"EiB": float64(ExbiByte),
"ZB": float64(ZettaByte),
"Z": float64(ZebiByte),
"ZiB": float64(ZebiByte),
"YB": float64(YottaByte),
"Y": float64(YobiByte),
"YiB": float64(YobiByte),
}
// ParseByte parses a byte definition.
// A byte string is a possibly signed sequence of
// decimal numbers, each with optional fraction and a unit suffix,
// such as "300K", "-1.5MiB" or "1TB45GB".
// Valid time units are "b" block 512, "kB" kilobyte 1000, "K", "KiB" kibibyte 1024
// and so on for M, G, T, P, E, Z, Y
func ParseByte(s string) (Byte, error) {
u, err := parseUnit(ByteSuffixes, s)
return Byte(u), err
}
// https://pkg.go.dev/github.com/spf13/pflag#Value
func (b Byte) String() string {
// TODO: make it better
return fmt.Sprintf("%.fB", float64(b))
}
func (b Byte) Type() string {
return "Byte"
}
func (b *Byte) Set(value string) error {
x, err := ParseByte(value)
if err != nil {
return err
}
if float64(x) > float64(1<<63) {
return fmt.Errorf("size overflow %f", math.Round(float64(x)))
}
*b = x
return nil
}