// replace number literals with postfixes like '256kb' and '0.5mb'
// with corresponding integers.
replace number literals with postfixes like '256kb' and '0.5mb'
with corresponding integers.
replace number literals with postfixes like ' 256kb ' and ' 0 . 5mb ' with corresponding integers .
fn preprocess_numbers(text: &str) -> BoxResult<String> {
    let number_with_postfix = RegexBuilder::new(r#"(\d+(\.\d+)*)(k|m|g)b*"#).case_insensitive(true).build()?;
    let mut conversion_error: Option<String> = None;
    let res = number_with_postfix.replace_all(text,|caps: &Captures| {
        let nums = &caps[1];
        let num = match nums.parse::<f64>() {
            Ok(x) => x,
            Err(e) => {
                conversion_error = Some(e.description().into());
                return "".into();
            }
        };
        let postfix = &caps[3];
        let mult: u64 = match postfix {
            "k" | "K" => 1024,
            "m" | "M" => 1024*1024,
            "g" | "G" => 1024*1024*1024,
            _ => unreachable!(),
        };
        let num = num * mult as f64;
        num.to_string()
    });
    if let Some(err) = conversion_error {
        err_io(&err)
    } else {
        Ok(res.into_owned())
    }
}
fn preprocess_numbers ( text : & str ) -> BoxResult < String > { let number_with_postfix = RegexBuilder :: new ( r#"(\d+(\.\d+)*)(k|m|g)b*"# ) . case_insensitive ( true ) . build ( ) ? ; let mut conversion_error : Option < String > = None ; let res = number_with_postfix . replace_all ( text , | caps : & Captures | { let nums = & caps [ 1 ] ; let num = match nums . parse :: < f64 > ( ) { Ok ( x ) => x , Err ( e ) => { conversion_error = Some ( e . description ( ) . into ( ) ) ; return " " . into ( ) ; } } ; let postfix = & caps [ 3 ] ; let mult : u64 = match postfix { " " | " " => 1024 , " " | " " => 1024 * 1024 , " " | " " => 1024 * 1024 * 1024 , _ => unreachable ! ( ) , } ; let num = num * mult as f64 ; num . to_string ( ) } ) ; if let Some ( err ) = conversion_error { err_io ( & err ) } else { Ok ( res . into_owned ( ) ) } }
replace number literals with postfixes like '256kb' and '0.5mb'
with corresponding integers.
replace number literals with postfixes like ' 256kb ' and ' 0 . 5mb ' with corresponding integers .
text
&str