MCPcopy Create free account
hub / github.com/plexdrive/plexdrive / parseSizeArg

Function parseSizeArg

main.go:258–291  ·  view source on GitHub ↗
(input string)

Source from the content-addressed store, hash-verified

256}
257
258func parseSizeArg(input string) (int64, error) {
259 if "" == input {
260 return 0, nil
261 }
262
263 suffix := input[len(input)-1]
264 suffixLen := 1
265 var multiplier float64
266 switch suffix {
267 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
268 suffixLen = 0
269 case 'b', 'B':
270 multiplier = 1
271 case 'k', 'K':
272 multiplier = 1024
273 case 'm', 'M':
274 multiplier = 1024 * 1024
275 case 'g', 'G':
276 multiplier = 1024 * 1024 * 1024
277 default:
278 return 0, fmt.Errorf("Invalid unit %v for %v", suffix, input)
279 }
280 input = input[:len(input)-suffixLen]
281 value, err := strconv.ParseFloat(input, 64)
282 if nil != err {
283 Log.Debugf("%v", err)
284 return 0, fmt.Errorf("Could not parse numeric value %v", input)
285 }
286 if value < 0 {
287 return 0, fmt.Errorf("Numeric value must not be negative %v", input)
288 }
289 value *= multiplier
290 return int64(value), nil
291}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected