From 68e7818c272708d777a4ed9a33ca18f98f995fe4 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 22 Jul 2019 01:27:56 +0800 Subject: [PATCH] fix parse_size --- src/lib.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 19fa60e..715b063 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,19 +154,21 @@ pub fn print_lastline(line: &str) { pub fn parse_size(size: &str) -> XResult { let lower_size = size.to_lowercase(); - if lower_size.ends_with("b") { - let no_last_b_size = &lower_size[0..lower_size.len()-1]; - if no_last_b_size.ends_with("k") { - return Ok((SIZE_KB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); - } else if no_last_b_size.ends_with("m") { - return Ok((SIZE_MB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); - } else if no_last_b_size.ends_with("g") { - return Ok((SIZE_GB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); - } else if no_last_b_size.ends_with("t") { - return Ok((SIZE_TB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); - } else if no_last_b_size.ends_with("p") { - return Ok((SIZE_PB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); - } + let no_last_b_size = if lower_size.ends_with("b") { + &lower_size[0..lower_size.len()-1] + } else { + &lower_size + }; + if no_last_b_size.ends_with("k") { + return Ok((SIZE_KB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); + } else if no_last_b_size.ends_with("m") { + return Ok((SIZE_MB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); + } else if no_last_b_size.ends_with("g") { + return Ok((SIZE_GB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); + } else if no_last_b_size.ends_with("t") { + return Ok((SIZE_TB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); + } else if no_last_b_size.ends_with("p") { + return Ok((SIZE_PB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64); } Err(new_box_error(&format!("Cannot parse size: {}", size)))