From c1ef4c4b53dd0e7330565ce086439163e3e0b542 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 20 Jan 2024 14:51:02 +0800 Subject: [PATCH] feat: v0.6.47 --- Cargo.toml | 2 +- src/util_size.rs | 28 ++++++++++++++-------------- src/util_str.rs | 38 ++++++++++---------------------------- 3 files changed, 25 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd69c6c..7eb660f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.46" +version = "0.6.47" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_size.rs b/src/util_size.rs index 6601e3e..b9ac5f5 100644 --- a/src/util_size.rs +++ b/src/util_size.rs @@ -13,7 +13,7 @@ pub fn parse_size(size: &str) -> XResult { lower_size.ends_with('b'), &lower_size[0..lower_size.len()-1], &lower_size ); let parse_and_process_size = |mul: i64| -> XResult { - Ok((mul as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::()?) as i64) + Ok((mul as f64 * no_last_b_size[0..no_last_b_size.len() - 1].parse::()?) as i64) }; match no_last_b_size.chars().last() { Some('k') => parse_and_process_size(SIZE_KB), @@ -27,17 +27,17 @@ pub fn parse_size(size: &str) -> XResult { pub fn get_display_size(size: i64) -> String { if size < SIZE_KB { - size.to_string() + format!("{} {}", size, crate::iff!(size == 1, "byte", "bytes" )) } else if size < SIZE_MB { - format!("{:.*}KB", 2, (size as f64) / SIZE_KB as f64) + format!("{:.*}KiB", 2, (size as f64) / SIZE_KB as f64) } else if size < SIZE_GB { - format!("{:.*}MB", 2, (size as f64) / SIZE_MB as f64) + format!("{:.*}MiB", 2, (size as f64) / SIZE_MB as f64) } else if size < SIZE_TB { - format!("{:.*}GB", 2, (size as f64) / SIZE_GB as f64) + format!("{:.*}GiB", 2, (size as f64) / SIZE_GB as f64) } else if size < SIZE_PB { - format!("{:.*}TB", 2, (size as f64) / SIZE_TB as f64) + format!("{:.*}TiB", 2, (size as f64) / SIZE_TB as f64) } else { - format!("{:.*}PB", 2, (size as f64) / SIZE_PB as f64) + format!("{:.*}PiB", 2, (size as f64) / SIZE_PB as f64) } } @@ -54,11 +54,11 @@ fn test_parse_size() { #[test] fn test_get_display_size() { - assert_eq!(get_display_size(0), "0"); - assert_eq!(get_display_size(111), "111"); - assert_eq!(get_display_size(1024), "1.00KB"); - assert_eq!(get_display_size(1024 * 1024), "1.00MB"); - assert_eq!(get_display_size(1024 * 1024 * 1024), "1.00GB"); - assert_eq!(get_display_size(1024 * 1024 * 1024 * 1024), "1.00TB"); - assert_eq!(get_display_size(1024 * 1024 * 1024 * 1024 * 1024), "1.00PB"); + assert_eq!(get_display_size(0), "0 bytes"); + assert_eq!(get_display_size(111), "111 bytes"); + assert_eq!(get_display_size(1024), "1.00KiB"); + assert_eq!(get_display_size(1024 * 1024), "1.00MiB"); + assert_eq!(get_display_size(1024 * 1024 * 1024), "1.00GiB"); + assert_eq!(get_display_size(1024 * 1024 * 1024 * 1024), "1.00TiB"); + assert_eq!(get_display_size(1024 * 1024 * 1024 * 1024 * 1024), "1.00PiB"); } \ No newline at end of file diff --git a/src/util_str.rs b/src/util_str.rs index 6c96e1d..0bde11f 100644 --- a/src/util_str.rs +++ b/src/util_str.rs @@ -1,29 +1,6 @@ - /// Split string to lines, splited by '\r', '\n' or "\r\n" pub fn read_str_to_lines(s: &str) -> Vec { s.lines().map(|ln| ln.to_owned()).collect() - // let mut r = vec![]; - // let mut line = String::new(); - // let mut cs = s.chars().peekable(); - // while let Some(c) = cs.next() { - // if c == '\n' || c == '\r' { - // r.push(line.clone()); - // line.clear(); - // if c == '\r' { - // if let Some(nc) = cs.peek() { - // if *nc == '\n' { - // cs.next(); - // } - // } - // } - // } else { - // line.push(c); - // } - // } - // if !line.is_empty() { - // r.push(line); - // } - // r } pub fn split_kv(s: &str, split: char) -> (String, String) { @@ -61,25 +38,30 @@ fn test_read_str_to_lines() { let s = ""; let lines = read_str_to_lines(s); assert_eq!(lines.len(), 0); - } { + } + { let s = "\n"; let lines = read_str_to_lines(s); assert_eq!(lines.len(), 1); - } { + } + { let s = "\r"; let lines = read_str_to_lines(s); assert_eq!(lines.len(), 1); - } { + } + { let s = "\r\n"; let lines = read_str_to_lines(s); assert_eq!(lines.len(), 1); - } { + } + { let s = "aa\r\nbb"; let lines = read_str_to_lines(s); assert_eq!(lines.len(), 2); assert_eq!(lines[0], "aa"); assert_eq!(lines[1], "bb"); - } { + } + { let s = "aa\r\nbb\ncc"; let lines = read_str_to_lines(s); assert_eq!(lines.len(), 3);