MCPcopy Create free account
hub / github.com/ZeppelinMC/Zeppelin / ParseSizeUnit

Function ParseSizeUnit

util/unit.go:21–42  ·  view source on GitHub ↗

ParseSizeUnit parses a size string into a number of bytes.

(sizeStr string)

Source from the content-addressed store, hash-verified

19
20// ParseSizeUnit parses a size string into a number of bytes.
21func ParseSizeUnit(sizeStr string) (uint64, error) {
22 // Trim any surrounding whitespace and convert to lowercase.
23 sizeStr = strings.TrimSpace(strings.ToLower(sizeStr))
24
25 // Find the position where the letters start.
26 for unit := range sizeUnits {
27 if strings.HasSuffix(sizeStr, unit) {
28 // Extract the numeric part of the string.
29 numberStr := strings.TrimSuffix(sizeStr, unit)
30 number, err := strconv.ParseUint(numberStr, 10, 64)
31 if err != nil {
32 return 0, fmt.Errorf("invalid number format: %w", err)
33 }
34
35 // Convert the number to bytes using the corresponding unit.
36 return number * sizeUnits[unit], nil
37 }
38 }
39
40 number, err := strconv.ParseUint(sizeStr, 10, 64)
41 return number, err
42}
43
44var sizeUnitsS = []struct {
45 Unit string

Callers 1

mainFunction · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected