49 lines
1.5 KiB
Rust
49 lines
1.5 KiB
Rust
|
use std::borrow::Cow;
|
||
|
|
||
|
/// Removes all whitespace from a string.
|
||
|
///
|
||
|
/// Example: "foo bar" => "foobar" and "foo \n bar" => "foobar".
|
||
|
#[allow(dead_code)]
|
||
|
pub(crate) fn coalesce_whitespace(input: &str) -> Cow<'_, str> {
|
||
|
let mut state = CoalesceWhitespace::Normal;
|
||
|
for (offset, c) in input.char_indices() {
|
||
|
match (&mut state, c) {
|
||
|
(CoalesceWhitespace::Normal, ' ' | '\t' | '\r' | '\n') => {
|
||
|
let mut ret = String::with_capacity(input.len());
|
||
|
ret.push_str(&input[..offset]);
|
||
|
ret.push(' ');
|
||
|
state = CoalesceWhitespace::HasWhitespace {
|
||
|
in_whitespace: true,
|
||
|
ret,
|
||
|
};
|
||
|
}
|
||
|
(CoalesceWhitespace::Normal, _) => {}
|
||
|
(
|
||
|
CoalesceWhitespace::HasWhitespace { in_whitespace, ret },
|
||
|
' ' | '\t' | '\r' | '\n',
|
||
|
) => {
|
||
|
if !*in_whitespace {
|
||
|
*in_whitespace = true;
|
||
|
ret.push(' ');
|
||
|
}
|
||
|
}
|
||
|
(CoalesceWhitespace::HasWhitespace { in_whitespace, ret }, _) => {
|
||
|
*in_whitespace = false;
|
||
|
ret.push(c);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
match state {
|
||
|
CoalesceWhitespace::Normal => Cow::Borrowed(input),
|
||
|
CoalesceWhitespace::HasWhitespace {
|
||
|
in_whitespace: _,
|
||
|
ret,
|
||
|
} => Cow::Owned(ret),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
enum CoalesceWhitespace {
|
||
|
Normal,
|
||
|
HasWhitespace { in_whitespace: bool, ret: String },
|
||
|
}
|