forked from inhies/go-bytesize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbytesize.go
executable file
·261 lines (218 loc) · 5.49 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
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
// Package bytesize provides functionality for measuring and formatting byte
// sizes.
//
// You can also perfom mathmatical operation with ByteSize's and the result
// will be a valid ByteSize with the correct size suffix.
package bytesize
import (
"errors"
"fmt"
"strconv"
"strings"
"unicode"
)
// This code was originally based on http://golang.org/doc/progs/eff_bytesize.go
//
// Since then many improvements have been made. The following is the original
// copyright notice:
// Copyright 2009 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.
// ByteSize represents a number of bytes
type ByteSize uint64
// Byte size size suffixes.
const (
B ByteSize = 1
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
EB
)
// Used for returning long unit form of string representation.
var longUnitMap = map[ByteSize]string{
B: "byte",
KB: "kilobyte",
MB: "megabyte",
GB: "gigabyte",
TB: "terabyte",
PB: "petabyte",
EB: "exabyte",
}
// Used for returning string representation.
var shortUnitMap = map[ByteSize]string{
B: "B",
KB: "KB",
MB: "MB",
GB: "GB",
TB: "TB",
PB: "PB",
EB: "EB",
}
// Used to convert user input to ByteSize
var unitMap = map[string]ByteSize{
"B": B,
"BYTE": B,
"BYTES": B,
"KB": KB,
"KILOBYTE": KB,
"KILOBYTES": KB,
"MB": MB,
"MEGABYTE": MB,
"MEGABYTES": MB,
"GB": GB,
"GIGABYTE": GB,
"GIGABYTES": GB,
"TB": TB,
"TERABYTE": TB,
"TERABYTES": TB,
"PB": PB,
"PETABYTE": PB,
"PETABYTES": PB,
"EB": EB,
"EXABYTE": EB,
"EXABYTES": EB,
}
var (
// Use long units, such as "megabytes" instead of "MB".
LongUnits bool = false
// String format of bytesize output. The unit of measure will be appended
// to the end. Uses the same formatting options as the fmt package.
Format string = "%.2f"
)
// Parse parses a byte size string. A byte size string is a number followed by
// a unit suffix, such as "1024B" or "1 MB". Valid byte units are "B", "KB",
// "MB", "GB", "TB", "PB" and "EB". You can also use the long
// format of units, such as "kilobyte" or "kilobytes".
func Parse(s string) (ByteSize, error) {
// Remove leading and trailing whitespace
s = strings.TrimSpace(s)
split := make([]string, 0)
for i, r := range s {
if !unicode.IsDigit(r) && r != '.' {
// Split the string by digit and size designator, remove whitespace
split = append(split, strings.TrimSpace(string(s[:i])))
split = append(split, strings.TrimSpace(string(s[i:])))
break
}
}
// Check to see if we split successfully
if len(split) != 2 {
return 0, errors.New("unrecognized size suffix")
}
// Check for MB, MEGABYTE, and MEGABYTES
unit, ok := unitMap[strings.ToUpper(split[1])]
if !ok {
return 0, errors.New("Unrecognized size suffix " + split[1])
}
value, err := strconv.ParseFloat(split[0], 64)
if err != nil {
return 0, err
}
bytesize := ByteSize(value * float64(unit))
return bytesize, nil
}
// Satisfy the flag package Value interface.
func (b *ByteSize) Set(s string) error {
bs, err := Parse(s)
if err != nil {
return err
}
*b = bs
return nil
}
// Satisfy the pflag package Value interface.
func (b ByteSize) Type() string { return "byte_size" }
// Satisfy the encoding.TextUnmarshaler interface.
func (b ByteSize) UnmarshalText(text []byte) error {
return b.Set(string(text))
}
// Satisfy the flag package Getter interface.
func (b ByteSize) Get() interface{} { return ByteSize(b) }
// New returns a new ByteSize type set to s.
func New(s float64) ByteSize {
return ByteSize(s)
}
// Returns a string representation of b with the specified formatting and units.
func (b ByteSize) Format(format string, unit string, longUnits bool) string {
return b.format(format, unit, longUnits)
}
// String returns the string form of b using the package global Format and
// LongUnits options.
func (b ByteSize) String() string {
return b.format(Format, "", LongUnits)
}
func (b ByteSize) format(format string, unit string, longUnits bool) string {
var unitSize ByteSize
if unit != "" {
var ok bool
unitSize, ok = unitMap[strings.ToUpper(unit)]
if !ok {
return "Unrecognized unit: " + unit
}
} else {
switch {
case b >= EB:
unitSize = EB
case b >= PB:
unitSize = PB
case b >= TB:
unitSize = TB
case b >= GB:
unitSize = GB
case b >= MB:
unitSize = MB
case b >= KB:
unitSize = KB
default:
unitSize = B
}
}
if longUnits {
var s string
value := fmt.Sprintf(format, float64(b)/float64(unitSize))
if printS, _ := strconv.ParseFloat(strings.TrimSpace(value), 64); printS > 0 && printS != 1 {
s = "s"
}
return fmt.Sprintf(format+longUnitMap[unitSize]+s, float64(b)/float64(unitSize))
}
return fmt.Sprintf(format+shortUnitMap[unitSize], float64(b)/float64(unitSize))
}
func (b ByteSize) Bytes() uint64 {
return uint64(b)
}
func (b ByteSize) KiloBytes() float64 {
return b.as(KB)
}
func (b ByteSize) MegaBytes() float64 {
return b.as(MB)
}
func (b ByteSize) GigaBytes() float64 {
return b.as(GB)
}
func (b ByteSize) TeraBytes() float64 {
return b.as(TB)
}
func (b ByteSize) PetaBytes() float64 {
return b.as(PB)
}
func (b ByteSize) ExaBytes() float64 {
return b.as(EB)
}
func (b ByteSize) as(size ByteSize) float64 {
v := b / size
r := b % size
return float64(v) + float64(r)/float64(size)
}
func (b ByteSize) Round(size ByteSize) ByteSize {
v := b / size
r := b % size
if r > 0 {
v++
}
return v * size
}
func (b ByteSize) Trunc(size ByteSize) ByteSize {
v := b / size
return v * size
}