MCPcopy
hub / github.com/CodisLabs/codis / Parse

Function Parse

pkg/utils/bytesize/bytesize.go:100–144  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

98)
99
100func Parse(s string) (int64, error) {
101 if !fullRegexp.MatchString(s) {
102 return 0, errors.Trace(ErrBadByteSize)
103 }
104
105 subs := fullRegexp.FindStringSubmatch(s)
106 if len(subs) != 3 {
107 return 0, errors.Trace(ErrBadByteSize)
108 }
109
110 text := subs[1]
111 unit := subs[2]
112
113 size := int64(1)
114 switch unit {
115 case "b", "":
116 case "k", "kb":
117 size = KB
118 case "m", "mb":
119 size = MB
120 case "g", "gb":
121 size = GB
122 case "t", "tb":
123 size = TB
124 case "p", "pb":
125 size = PB
126 default:
127 return 0, errors.Trace(ErrBadByteSizeUnit)
128 }
129
130 if digitsOnly.MatchString(text) {
131 n, err := strconv.ParseInt(text, 10, 64)
132 if err != nil {
133 return 0, errors.Trace(ErrBadByteSize)
134 }
135 size *= n
136 } else {
137 n, err := strconv.ParseFloat(text, 64)
138 if err != nil {
139 return 0, errors.Trace(ErrBadByteSize)
140 }
141 size = int64(float64(size) * n)
142 }
143 return size, nil
144}
145
146func MustParse(s string) int64 {
147 v, err := Parse(s)

Callers 3

UnmarshalTextMethod · 0.70
MustParseFunction · 0.70
TestByteSizeErrorFunction · 0.70

Calls

no outgoing calls

Tested by 1

TestByteSizeErrorFunction · 0.56