-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
55 lines (51 loc) · 1.02 KB
/
utils.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 ik
import (
"errors"
"math/rand"
"regexp"
"strconv"
"time"
)
var capacityRegExp = regexp.MustCompile("^([0-9]+)([kKmMgGtTpPeE])?(i?[bB])?")
func ParseCapacityString(s string) (int64, error) {
m := capacityRegExp.FindStringSubmatch(s)
if m == nil {
return -1, errors.New("Invalid format: " + s)
}
base := int64(1000)
if len(m[3]) > 0 {
if m[3][0] == 'i' {
base = int64(1024)
}
}
multiply := int64(1)
if len(m[2]) > 0 {
switch m[2][0] {
case 'e', 'E':
multiply *= base
fallthrough
case 'p', 'P':
multiply *= base
fallthrough
case 't', 'T':
multiply *= base
fallthrough
case 'g', 'G':
multiply *= base
fallthrough
case 'm', 'M':
multiply *= base
fallthrough
case 'k', 'K':
multiply *= base
}
}
i, err := strconv.ParseInt(m[1], 10, 64)
if err != nil || multiply*i < i {
return -1, errors.New("Invalid format (out of range): " + s)
}
return multiply * i, nil
}
func NewRandSourceWithTimestampSeed() rand.Source {
return rand.NewSource(time.Now().UnixNano())
}