1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// Parses a buffer size string with optional unit suffixes into a `usize` value.
///
/// This function accepts numeric values with optional unit suffixes (e.g., `"K"`, `"MB"`, `"GB"`)
/// and converts them into their corresponding byte values. The parsing is case-insensitive,
/// and spaces around the input string are trimmed.
///
/// # Supported Units
///
/// - No unit: Assumes bytes (e.g., `"1024"` → `1024` bytes)
/// - `K` or `KB`: Kilobytes (e.g., `"2K"` or `"2KB"` → `2048` bytes)
/// - `M` or `MB`: Megabytes (e.g., `"1M"` or `"1MB"` → `1048576` bytes)
/// - `G` or `GB`: Gigabytes (e.g., `"1G"` or `"1GB"` → `1073741824` bytes)
///
/// # Arguments
///
/// * `size_str` - A string slice representing the buffer size with an optional unit.
///
/// # Returns
///
/// Returns `Ok(usize)` if parsing succeeds, or an `Err(String)` if the format is invalid.
///
/// # Examples
///
/// ```
/// use simd_r_drive::utils::parse_buffer_size;
///
/// assert_eq!(parse_buffer_size("1024").unwrap(), 1024);
/// assert_eq!(parse_buffer_size("2K").unwrap(), 2048);
/// assert_eq!(parse_buffer_size("1MB").unwrap(), 1_048_576);
/// assert_eq!(parse_buffer_size("1G").unwrap(), 1_073_741_824);
///
/// assert!(parse_buffer_size("abc").is_err()); // Invalid input
/// assert!(parse_buffer_size("10XZ").is_err()); // Invalid unit
/// ```