Skip to content

Instantly share code, notes, and snippets.

@iitalics
Created February 21, 2024 20:19
Show Gist options
  • Save iitalics/dc6732982530e8ec782ff1856e2be6e4 to your computer and use it in GitHub Desktop.
Save iitalics/dc6732982530e8ec782ff1856e2be6e4 to your computer and use it in GitHub Desktop.
const fn str_trim_ascii_whitespace(s: &str) -> &str {
let mut bs = s.as_bytes();
while let Some((hd, tl)) = bs.split_first() {
if hd.is_ascii_whitespace() {
bs = tl;
} else {
break;
}
}
while let Some((tl, hd)) = bs.split_last() {
if tl.is_ascii_whitespace() {
bs = hd;
} else {
break;
}
}
match std::str::from_utf8(bs) {
Ok(s) => s,
Err(_) => unreachable!(),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment