MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / parse_memory_to_bytes

Function parse_memory_to_bytes

crates/openshell-driver-podman/src/container.rs:1094–1120  ·  view source on GitHub ↗

Parse a Kubernetes-style memory quantity to bytes. Supports: `Ki`, `Mi`, `Gi`, `Ti` (binary) and `k`, `M`, `G`, `T` (decimal), as well as plain byte values.

(quantity: &str)

Source from the content-addressed store, hash-verified

1092/// Supports: `Ki`, `Mi`, `Gi`, `Ti` (binary) and `k`, `M`, `G`, `T`
1093/// (decimal), as well as plain byte values.
1094fn parse_memory_to_bytes(quantity: &str) -> Option<u64> {
1095 let suffixes: &[(&str, u64)] = &[
1096 ("Ei", 1024 * 1024 * 1024 * 1024 * 1024 * 1024),
1097 ("Pi", 1024 * 1024 * 1024 * 1024 * 1024),
1098 ("Ti", 1024 * 1024 * 1024 * 1024),
1099 ("Gi", 1024 * 1024 * 1024),
1100 ("Mi", 1024 * 1024),
1101 ("Ki", 1024),
1102 ("E", 1_000_000_000_000_000_000),
1103 ("P", 1_000_000_000_000_000),
1104 ("T", 1_000_000_000_000),
1105 ("G", 1_000_000_000),
1106 ("M", 1_000_000),
1107 ("K", 1_000),
1108 ("k", 1_000),
1109 ];
1110
1111 for (suffix, multiplier) in suffixes {
1112 if let Some(num_str) = quantity.strip_suffix(suffix) {
1113 let num: u64 = num_str.parse().ok()?;
1114 return num.checked_mul(*multiplier);
1115 }
1116 }
1117
1118 // Plain bytes.
1119 quantity.parse().ok()
1120}
1121
1122#[cfg(test)]
1123mod tests {

Callers 1

build_resource_limitsFunction · 0.85

Calls 1

parseMethod · 0.45

Tested by

no test coverage detected