Struct uucore::parse_size::Parser
source · pub struct Parser<'parser> {
pub no_empty_numeric: bool,
pub capital_b_bytes: bool,
pub b_byte_count: bool,
pub allow_list: Option<&'parser [&'parser str]>,
pub default_unit: Option<&'parser str>,
}
Expand description
Parser for sizes in SI or IEC units (multiples of 1000 or 1024 bytes).
The Parser::parse
function performs the parse.
Fields§
§no_empty_numeric: bool
Whether to allow empty numeric strings.
capital_b_bytes: bool
Whether to treat the suffix “B” as meaning “bytes”.
b_byte_count: bool
Whether to treat “b” as a “byte count” instead of “block”
allow_list: Option<&'parser [&'parser str]>
Whitelist for the suffix
default_unit: Option<&'parser str>
Default unit when no suffix is provided
Implementations§
source§impl<'parser> Parser<'parser>
impl<'parser> Parser<'parser>
pub fn with_allow_list(&mut self, allow_list: &'parser [&str]) -> &mut Self
pub fn with_default_unit(&mut self, default_unit: &'parser str) -> &mut Self
pub fn with_b_byte_count(&mut self, value: bool) -> &mut Self
pub fn with_allow_empty_numeric(&mut self, value: bool) -> &mut Self
sourcepub fn parse(&self, size: &str) -> Result<u128, ParseSizeError>
pub fn parse(&self, size: &str) -> Result<u128, ParseSizeError>
Parse a size string into a number of bytes.
A size string comprises an integer and an optional unit. The unit may be K, M, G, T, P, E, Z, Y, R or Q (powers of 1024), or KB, MB, etc. (powers of 1000), or b which is 512. Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
§Errors
Will return ParseSizeError
if it’s not possible to parse this
string into a number, e.g. if the string does not begin with a
numeral, or if the unit is not one of the supported units described
in the preceding section.
§Examples
use uucore::parse_size::Parser;
let parser = Parser {
default_unit: Some("M"),
..Default::default()
};
assert_eq!(Ok(123 * 1024 * 1024), parser.parse("123M")); // M is 1024^2
assert_eq!(Ok(123 * 1024 * 1024), parser.parse("123")); // default unit set to "M" on parser instance
assert_eq!(Ok(9 * 1000), parser.parse("9kB")); // kB is 1000
assert_eq!(Ok(2 * 1024), parser.parse("2K")); // K is 1024
assert_eq!(Ok(44251 * 1024), parser.parse("0xACDBK")); // 0xACDB is 44251 in decimal
sourcepub fn parse_u128(&self, size: &str) -> Result<u128, ParseSizeError>
pub fn parse_u128(&self, size: &str) -> Result<u128, ParseSizeError>
Explicit u128 alias for parse()
sourcepub fn parse_u64(&self, size: &str) -> Result<u64, ParseSizeError>
pub fn parse_u64(&self, size: &str) -> Result<u64, ParseSizeError>
Same as parse()
but tries to return u64
sourcepub fn parse_u64_max(&self, size: &str) -> Result<u64, ParseSizeError>
pub fn parse_u64_max(&self, size: &str) -> Result<u64, ParseSizeError>
Same as parse_u64()
, except returns u64::MAX
on overflow
GNU lib/coreutils include similar functionality
and GNU test suite checks this behavior for some utils (split
for example)
sourcepub fn parse_u128_max(&self, size: &str) -> Result<u128, ParseSizeError>
pub fn parse_u128_max(&self, size: &str) -> Result<u128, ParseSizeError>
Same as parse_u64_max()
, except for u128, i.e. returns u128::MAX
on overflow