forked from projectmoon/tenebrous-dicebot
Actually only trim the start and end of the string.
Be careful what you find on Stack Overflow, kids.
This commit is contained in:
parent
d36a38d16f
commit
1f5c6d7553
|
@ -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 "));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue