1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-29 00:20:04 +08:00

ref read_str_to_lines

This commit is contained in:
2020-05-05 22:00:45 +08:00
parent a672cdfa45
commit ef49c85cf2

View File

@@ -1,28 +1,29 @@
/// Split string to lines, splited by '\r', '\n' or "\r\n" /// Split string to lines, splited by '\r', '\n' or "\r\n"
pub fn read_str_to_lines(s: &str) -> Vec<String> { pub fn read_str_to_lines(s: &str) -> Vec<String> {
let mut r = vec![]; s.lines().map(|ln| ln.to_owned()).collect()
let mut line = String::new(); // let mut r = vec![];
let mut cs = s.chars().peekable(); // let mut line = String::new();
while let Some(c) = cs.next() { // let mut cs = s.chars().peekable();
if c == '\n' || c == '\r' { // while let Some(c) = cs.next() {
r.push(line.clone()); // if c == '\n' || c == '\r' {
line.clear(); // r.push(line.clone());
if c == '\r' { // line.clear();
if let Some(nc) = cs.peek() { // if c == '\r' {
if *nc == '\n' { // if let Some(nc) = cs.peek() {
cs.next(); // if *nc == '\n' {
} // cs.next();
} // }
} // }
} else { // }
line.push(c); // } else {
} // line.push(c);
} // }
if !line.is_empty() { // }
r.push(line); // if !line.is_empty() {
} // r.push(line);
r // }
// r
} }
pub fn split_kv(s: &str, split: char) -> (String, String) { pub fn split_kv(s: &str, split: char) -> (String, String) {