Skip to main content

parse_size_in_bytes

Function parse_size_in_bytes 

Source
pub fn parse_size_in_bytes(raw: &str) -> Option<u64>
Expand description

Parses a size setting into a byte count, as the Agent’s GetSizeInBytes does.

This ports parseSizeInBytes and the string path of cast.ToInt:

  1. Surrounding whitespace is trimmed.
  2. If the text is at least three bytes long and ends in b or B, a preceding k, m, or g (either case) selects a multiplier of 2^10, 2^20, or 2^30 and both letters are removed. Any other preceding byte leaves a multiplier of one and removes only the b. A two-byte value such as 1B is never stripped, so it is not a number. There is no T, no bare K/M/G, and no iB spelling.
  3. The remaining text is trimmed again, an all-zero fraction is dropped (10.0 is 10), and the result is read as a Go integer literal with base 0: an optional sign, then decimal digits or a 0x, 0o, 0b, or leading-0 (octal) prefix, with _ digit separators only between digits or after a prefix. The value must fit in an i64.
  4. The count is multiplied by the multiplier and must fit in a u64.

Returns None when the Agent’s parse fails, the value is negative, or the product overflows. The Agent reads all of these as 0 without a diagnostic, so None keeps them apart from a configured 0. Otherwise, returns the Agent’s byte count.