From a672cdfa4593bccfc97b84f4f49ce84bd2e7f41b Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Tue, 5 May 2020 13:27:21 +0800 Subject: [PATCH] add read_str_to_lines, tests, fix bug --- Cargo.toml | 2 +- src/util_env.rs | 8 +++++-- src/util_size.rs | 26 +++++++++++++++++++-- src/util_str.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de703b7..359f79a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.2.4" +version = "0.2.5" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_env.rs b/src/util_env.rs index 4533c76..afb819b 100644 --- a/src/util_env.rs +++ b/src/util_env.rs @@ -1,6 +1,10 @@ - use std::env; pub fn is_env_on(var: &str) -> bool { - env::var(var).map(|v| v.to_lowercase()).map(|v| vec!["true", "yes", "1"].iter().any(|x| x == &v)).unwrap_or(false) + env::var(var).ok().map(|val| is_on(&val)).unwrap_or(false) +} + +pub fn is_on(val: &str) -> bool { + let lower_val = val.to_lowercase(); + vec!["true", "yes", "1"].iter().any(|x| *x == lower_val) } diff --git a/src/util_size.rs b/src/util_size.rs index ff525e6..17d93e0 100644 --- a/src/util_size.rs +++ b/src/util_size.rs @@ -3,8 +3,8 @@ use super::XResult; pub const SIZE_KB: i64 = 1024; pub const SIZE_MB: i64 = SIZE_KB * SIZE_KB; pub const SIZE_GB: i64 = SIZE_MB * SIZE_KB; -pub const SIZE_PB: i64 = SIZE_GB * SIZE_KB; -pub const SIZE_TB: i64 = SIZE_PB * SIZE_KB; +pub const SIZE_TB: i64 = SIZE_GB * SIZE_KB; +pub const SIZE_PB: i64 = SIZE_TB * SIZE_KB; pub fn parse_size(size: &str) -> XResult { @@ -44,3 +44,25 @@ pub fn get_display_size(size: i64) -> String { format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.) } } + + +#[test] +fn test_parse_size() { + assert_eq!(parse_size("1").unwrap(), 1); + assert_eq!(parse_size("1k").unwrap(), 1024); + assert_eq!(parse_size("1m").unwrap(), 1024 * 1024); + assert_eq!(parse_size("1g").unwrap(), 1024 * 1024 * 1024); + assert_eq!(parse_size("1t").unwrap(), 1024 * 1024 * 1024 * 1024); + assert_eq!(parse_size("1p").unwrap(), 1024 * 1024 * 1024 * 1024 * 1024); +} + +#[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"); +} \ No newline at end of file diff --git a/src/util_str.rs b/src/util_str.rs index 28ac26e..15f58b3 100644 --- a/src/util_str.rs +++ b/src/util_str.rs @@ -1,4 +1,29 @@ +/// Split string to lines, splited by '\r', '\n' or "\r\n" +pub fn read_str_to_lines(s: &str) -> Vec { + 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) { let mut k = String::new(); @@ -28,3 +53,37 @@ fn test_split_kv() { assert_eq!(("aa".to_owned(), "bb".to_owned()), split_kv("aa=bb", '=')); assert_eq!(("aa".to_owned(), "bb".to_owned()), split_kv("aa:bb", ':')); } + +#[test] +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); + assert_eq!(lines[0], "aa"); + assert_eq!(lines[1], "bb"); + assert_eq!(lines[2], "cc"); + } +}