MCPcopy Create free account
hub / github.com/PostHog/duckgres / ParseMemoryBytes

Function ParseMemoryBytes

server/sysinfo/sysinfo.go:102–129  ·  view source on GitHub ↗

ParseMemoryBytes parses a DuckDB memory size string (e.g., "4GB", "512MB", "1.5GB") into bytes. Supports both integer and fractional values. Returns 0 if the string is empty, invalid, or "0GB".

(s string)

Source from the content-addressed store, hash-verified

100// ParseMemoryBytes parses a DuckDB memory size string (e.g., "4GB", "512MB", "1.5GB") into bytes.
101// Supports both integer and fractional values. Returns 0 if the string is empty, invalid, or "0GB".
102func ParseMemoryBytes(s string) uint64 {
103 if s == "" {
104 return 0
105 }
106 s = strings.TrimSpace(strings.ToUpper(s))
107
108 var value float64
109 var unit string
110 if _, err := fmt.Sscanf(s, "%f%s", &value, &unit); err != nil || value < 0 {
111 return 0
112 }
113
114 var multiplier float64
115 switch unit {
116 case "KB":
117 multiplier = 1024
118 case "MB":
119 multiplier = 1024 * 1024
120 case "GB":
121 multiplier = 1024 * 1024 * 1024
122 case "TB":
123 multiplier = 1024 * 1024 * 1024 * 1024
124 default:
125 return 0
126 }
127
128 return uint64(value * multiplier)
129}

Callers 1

TestParseMemoryBytesFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseMemoryBytesFunction · 0.68