Actually only trim the start and end of the string.

Be careful what you find on Stack Overflow, kids.
This commit is contained in:
projectmoon 2020-08-31 00:05:40 +00:00
parent d36a38d16f
commit 1f5c6d7553
1 changed files with 18 additions and 1 deletions

View File

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