-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.go
55 lines (46 loc) · 944 Bytes
/
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
package main
import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
)
const (
Byte = 1
Kilo = 1 << (10 * iota)
Mega
Giga
)
var Units = map[string]int{
"b": Byte,
"k": Kilo,
"m": Mega,
"g": Giga,
}
var re = regexp.MustCompile(`(?i)([\d.]+)\s*([bkmg])b?`)
// Parse size from the given string and return parsed bytes.
func Parse(s string) (int, error) {
if re.MatchString(s) {
found := re.FindStringSubmatch(s)
size, err := strconv.ParseFloat(found[1], 64)
if err != nil {
return -1, err
}
parsedUnits := strings.ToLower(found[2])
units, ok := Units[parsedUnits]
if !ok {
return -1, fmt.Errorf("invalid units %s", parsedUnits)
}
if units == Byte {
// reject fractional amount of bytes
_, frac := math.Modf(size)
if frac > 0 {
return -1, errors.New("fractional of byte detected")
}
}
return int(size * float64(units)), nil
}
return -1, errors.New("invalid input string")
}