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>

source

pub fn with_allow_list(&mut self, allow_list: &'parser [&str]) -> &mut Self

source

pub fn with_default_unit(&mut self, default_unit: &'parser str) -> &mut Self

source

pub fn with_b_byte_count(&mut self, value: bool) -> &mut Self

source

pub fn with_allow_empty_numeric(&mut self, value: bool) -> &mut Self

source

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
source

pub fn parse_u128(&self, size: &str) -> Result<u128, ParseSizeError>

Explicit u128 alias for parse()

source

pub fn parse_u64(&self, size: &str) -> Result<u64, ParseSizeError>

Same as parse() but tries to return u64

source

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)

source

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

Trait Implementations§

source§

impl<'parser> Default for Parser<'parser>

source§

fn default() -> Parser<'parser>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'parser> Freeze for Parser<'parser>

§

impl<'parser> RefUnwindSafe for Parser<'parser>

§

impl<'parser> Send for Parser<'parser>

§

impl<'parser> Sync for Parser<'parser>

§

impl<'parser> Unpin for Parser<'parser>

§

impl<'parser> UnwindSafe for Parser<'parser>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.