MCPcopy Index your code
hub / github.com/foxcpp/maddy / ParseDataSize

Function ParseDataSize

framework/config/map.go:227–280  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

225}
226
227func ParseDataSize(s string) (int, error) {
228 if len(s) == 0 {
229 return 0, errors.New("missing a number")
230 }
231
232 // ' ' terminates the number+suffix pair.
233 s = s + " "
234
235 var total int
236 currentDigit := ""
237 suffix := ""
238 for _, ch := range s {
239 if unicode.IsDigit(ch) {
240 if suffix != "" {
241 return 0, errors.New("unexpected digit after a suffix")
242 }
243 currentDigit += string(ch)
244 continue
245 }
246 if ch != ' ' {
247 suffix += string(ch)
248 continue
249 }
250
251 num, err := strconv.Atoi(currentDigit)
252 if err != nil {
253 return 0, err
254 }
255
256 if num < 0 {
257 return 0, errors.New("value must not be negative")
258 }
259
260 switch suffix {
261 case "G":
262 total += num * 1024 * 1024 * 1024
263 case "M":
264 total += num * 1024 * 1024
265 case "K":
266 total += num * 1024
267 case "B", "b":
268 total += num
269 default:
270 if num != 0 {
271 return 0, errors.New("unknown unit suffix: " + suffix)
272 }
273 }
274
275 suffix = ""
276 currentDigit = ""
277 }
278
279 return total, nil
280}
281
282// DataSize maps configuration directive to a int variable, representing data size.
283//

Callers 3

bufferModeDirectiveFunction · 0.92
DataSizeMethod · 0.85
TestParseDataSizeFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseDataSizeFunction · 0.68