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)
| 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". |
| 102 | func 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 | } |
no outgoing calls