From 1f5c6d755330b8655f253730af79581cd0ca3b57 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Mon, 31 Aug 2020 00:05:40 +0000 Subject: [PATCH] Actually only trim the start and end of the string. Be careful what you find on Stack Overflow, kids. --- src/parser.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index c3449c1..7f1e90d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,6 +10,23 @@ pub fn eat_whitespace(input: &str) -> IResult<&str, &str> { Ok((input, whitespace)) } +/// Remove the whitespace on the ends of the string. pub fn trim(input: &str) -> String { - input.chars().filter(|c| !c.is_whitespace()).collect() + //2 allocations, how fun + String::from(input).trim().to_owned() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic_trim_test() { + assert_eq!(String::from("blah"), trim(" blah ")); + } + + #[test] + fn trim_only_removes_ends_test() { + assert_eq!(String::from("b l a h"), trim(" b l a h ")); + } }