1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00

add read_str_to_lines, tests, fix bug

This commit is contained in:
2020-05-05 13:27:21 +08:00
parent 66d3540b65
commit a672cdfa45
4 changed files with 90 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "rust_util"
version = "0.2.4"
version = "0.2.5"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "Hatter's Rust Util"

View File

@@ -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)
}

View File

@@ -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<i64> {
@@ -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");
}

View File

@@ -1,4 +1,29 @@
/// Split string to lines, splited by '\r', '\n' or "\r\n"
pub fn read_str_to_lines(s: &str) -> Vec<String> {
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");
}
}