diff options
author | Irene Knapp <ireneista@gmail.com> | 2020-12-06 01:15:02 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2020-12-06 01:15:02 -0800 |
commit | 07acfa0b030cc2f481f76fc85a664bfbcddbc623 (patch) | |
tree | b4d55f5fd2bf56279994677e12ed47e9478d9683 /lib/src | |
parent | 08a18531f04a395a556444ecba149356eeddf9d7 (diff) |
Add a function that trims the lines
Diffstat (limited to 'lib/src')
-rw-r--r-- | lib/src/lib.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 0272485..5f87317 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -36,12 +36,29 @@ pub fn read_lines_file(filename: &str) -> Result<Vec<String>> { Ok(input) } -pub fn group_lines_by_blanks(lines: Vec<String>) -> Result<Vec<Vec<String>>> { +pub fn trim_lines<'a>(lines: &'a Vec<String>) -> Vec<&str> { + let mut trimmed_lines = Vec::new(); + + for line in lines { + match line.strip_suffix("\n") { + Some(stripped) => { + trimmed_lines.push(stripped); + } + None => { + trimmed_lines.push(line); + } + } + } + + trimmed_lines +} + +pub fn group_lines_by_blanks(lines: Vec<&str>) -> Vec<Vec<&str>> { let mut all_groups = Vec::new(); let mut current_group = Vec::new(); for line in lines { - if line.trim().len() == 0 { + if line.len() == 0 { all_groups.push(current_group); current_group = Vec::new(); } else { @@ -53,7 +70,7 @@ pub fn group_lines_by_blanks(lines: Vec<String>) -> Result<Vec<Vec<String>>> { all_groups.push(current_group); } - Ok(all_groups) + all_groups } pub fn read_int_file(filename: &str) -> Result<Vec<i64>> { |