| 1 |
package datasize |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
type ByteSize uint64 |
| 11 |
|
| 12 |
const ( |
| 13 |
B ByteSize = 1 |
| 14 |
KB = B << 10 |
| 15 |
MB = KB << 10 |
| 16 |
GB = MB << 10 |
| 17 |
TB = GB << 10 |
| 18 |
PB = TB << 10 |
| 19 |
EB = PB << 10 |
| 20 |
|
| 21 |
fnUnmarshalText string = "UnmarshalText" |
| 22 |
maxUint64 uint64 = (1 << 64) - 1 |
| 23 |
cutoff uint64 = maxUint64 / 10 |
| 24 |
) |
| 25 |
|
| 26 |
var ErrBits = errors.New("unit with capital unit prefix and lower case unit (b) - bits, not bytes ") |
| 27 |
|
| 28 |
func (b ByteSize) Bytes() uint64 { |
| 29 |
return uint64(b) |
| 30 |
} |
| 31 |
|
| 32 |
func (b ByteSize) KBytes() float64 { |
| 33 |
v := b / KB |
| 34 |
r := b % KB |
| 35 |
return float64(v) + float64(r)/float64(KB) |
| 36 |
} |
| 37 |
|
| 38 |
func (b ByteSize) MBytes() float64 { |
| 39 |
v := b / MB |
| 40 |
r := b % MB |
| 41 |
return float64(v) + float64(r)/float64(MB) |
| 42 |
} |
| 43 |
|
| 44 |
func (b ByteSize) GBytes() float64 { |
| 45 |
v := b / GB |
| 46 |
r := b % GB |
| 47 |
return float64(v) + float64(r)/float64(GB) |
| 48 |
} |
| 49 |
|
| 50 |
func (b ByteSize) TBytes() float64 { |
| 51 |
v := b / TB |
| 52 |
r := b % TB |
| 53 |
return float64(v) + float64(r)/float64(TB) |
| 54 |
} |
| 55 |
|
| 56 |
func (b ByteSize) PBytes() float64 { |
| 57 |
v := b / PB |
| 58 |
r := b % PB |
| 59 |
return float64(v) + float64(r)/float64(PB) |
| 60 |
} |
| 61 |
|
| 62 |
func (b ByteSize) EBytes() float64 { |
| 63 |
v := b / EB |
| 64 |
r := b % EB |
| 65 |
return float64(v) + float64(r)/float64(EB) |
| 66 |
} |
| 67 |
|
| 68 |
func (b ByteSize) String() string { |
| 69 |
switch { |
| 70 |
case b == 0: |
| 71 |
return fmt.Sprint("0B") |
| 72 |
case b%EB == 0: |
| 73 |
return fmt.Sprintf("%dEB", b/EB) |
| 74 |
case b%PB == 0: |
| 75 |
return fmt.Sprintf("%dPB", b/PB) |
| 76 |
case b%TB == 0: |
| 77 |
return fmt.Sprintf("%dTB", b/TB) |
| 78 |
case b%GB == 0: |
| 79 |
return fmt.Sprintf("%dGB", b/GB) |
| 80 |
case b%MB == 0: |
| 81 |
return fmt.Sprintf("%dMB", b/MB) |
| 82 |
case b%KB == 0: |
| 83 |
return fmt.Sprintf("%dKB", b/KB) |
| 84 |
default: |
| 85 |
return fmt.Sprintf("%dB", b) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func (b ByteSize) HR() string { |
| 90 |
return b.HumanReadable() |
| 91 |
} |
| 92 |
|
| 93 |
func (b ByteSize) HumanReadable() string { |
| 94 |
switch { |
| 95 |
case b > EB: |
| 96 |
return fmt.Sprintf("%.1f EB", b.EBytes()) |
| 97 |
case b > PB: |
| 98 |
return fmt.Sprintf("%.1f PB", b.PBytes()) |
| 99 |
case b > TB: |
| 100 |
return fmt.Sprintf("%.1f TB", b.TBytes()) |
| 101 |
case b > GB: |
| 102 |
return fmt.Sprintf("%.1f GB", b.GBytes()) |
| 103 |
case b > MB: |
| 104 |
return fmt.Sprintf("%.1f MB", b.MBytes()) |
| 105 |
case b > KB: |
| 106 |
return fmt.Sprintf("%.1f KB", b.KBytes()) |
| 107 |
default: |
| 108 |
return fmt.Sprintf("%d B", b) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func (b ByteSize) MarshalText() ([]byte, error) { |
| 113 |
return []byte(b.String()), nil |
| 114 |
} |
| 115 |
|
| 116 |
func (b *ByteSize) UnmarshalText(t []byte) error { |
| 117 |
var val uint64 |
| 118 |
var unit string |
| 119 |
|
| 120 |
|
| 121 |
t0 := t |
| 122 |
|
| 123 |
var c byte |
| 124 |
var i int |
| 125 |
|
| 126 |
ParseLoop: |
| 127 |
for i < len(t) { |
| 128 |
c = t[i] |
| 129 |
switch { |
| 130 |
case '0' <= c && c <= '9': |
| 131 |
if val > cutoff { |
| 132 |
goto Overflow |
| 133 |
} |
| 134 |
|
| 135 |
c = c - '0' |
| 136 |
val *= 10 |
| 137 |
|
| 138 |
if val > val+uint64(c) { |
| 139 |
|
| 140 |
goto Overflow |
| 141 |
} |
| 142 |
val += uint64(c) |
| 143 |
i++ |
| 144 |
|
| 145 |
default: |
| 146 |
if i == 0 { |
| 147 |
goto SyntaxError |
| 148 |
} |
| 149 |
break ParseLoop |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
unit = strings.TrimSpace(string(t[i:])) |
| 154 |
switch unit { |
| 155 |
case "Kb", "Mb", "Gb", "Tb", "Pb", "Eb": |
| 156 |
goto BitsError |
| 157 |
} |
| 158 |
unit = strings.ToLower(unit) |
| 159 |
switch unit { |
| 160 |
case "", "b", "byte": |
| 161 |
|
| 162 |
|
| 163 |
case "k", "kb", "kilo", "kilobyte", "kilobytes": |
| 164 |
if val > maxUint64/uint64(KB) { |
| 165 |
goto Overflow |
| 166 |
} |
| 167 |
val *= uint64(KB) |
| 168 |
|
| 169 |
case "m", "mb", "mega", "megabyte", "megabytes": |
| 170 |
if val > maxUint64/uint64(MB) { |
| 171 |
goto Overflow |
| 172 |
} |
| 173 |
val *= uint64(MB) |
| 174 |
|
| 175 |
case "g", "gb", "giga", "gigabyte", "gigabytes": |
| 176 |
if val > maxUint64/uint64(GB) { |
| 177 |
goto Overflow |
| 178 |
} |
| 179 |
val *= uint64(GB) |
| 180 |
|
| 181 |
case "t", "tb", "tera", "terabyte", "terabytes": |
| 182 |
if val > maxUint64/uint64(TB) { |
| 183 |
goto Overflow |
| 184 |
} |
| 185 |
val *= uint64(TB) |
| 186 |
|
| 187 |
case "p", "pb", "peta", "petabyte", "petabytes": |
| 188 |
if val > maxUint64/uint64(PB) { |
| 189 |
goto Overflow |
| 190 |
} |
| 191 |
val *= uint64(PB) |
| 192 |
|
| 193 |
case "E", "EB", "e", "eb", "eB": |
| 194 |
if val > maxUint64/uint64(EB) { |
| 195 |
goto Overflow |
| 196 |
} |
| 197 |
val *= uint64(EB) |
| 198 |
|
| 199 |
default: |
| 200 |
goto SyntaxError |
| 201 |
} |
| 202 |
|
| 203 |
*b = ByteSize(val) |
| 204 |
return nil |
| 205 |
|
| 206 |
Overflow: |
| 207 |
*b = ByteSize(maxUint64) |
| 208 |
return &strconv.NumError{fnUnmarshalText, string(t0), strconv.ErrRange} |
| 209 |
|
| 210 |
SyntaxError: |
| 211 |
*b = 0 |
| 212 |
return &strconv.NumError{fnUnmarshalText, string(t0), strconv.ErrSyntax} |
| 213 |
|
| 214 |
BitsError: |
| 215 |
*b = 0 |
| 216 |
return &strconv.NumError{fnUnmarshalText, string(t0), ErrBits} |
| 217 |
} |
| 218 |
|
| 219 |
func Parse(t []byte) (ByteSize, error) { |
| 220 |
var v ByteSize |
| 221 |
err := v.UnmarshalText(t) |
| 222 |
return v, err |
| 223 |
} |
| 224 |
|
| 225 |
func MustParse(t []byte) ByteSize { |
| 226 |
v, err := Parse(t) |
| 227 |
if err != nil { |
| 228 |
panic(err) |
| 229 |
} |
| 230 |
return v |
| 231 |
} |
| 232 |
|
| 233 |
func ParseString(s string) (ByteSize, error) { |
| 234 |
return Parse([]byte(s)) |
| 235 |
} |
| 236 |
|
| 237 |
func MustParseString(s string) ByteSize { |
| 238 |
return MustParse([]byte(s)) |
| 239 |
} |