use bytes::Bytes;
pub trait AsciiExt {
fn split_once<F>(&self, separator: F) -> Option<(Self, Self)>
where
F: FnMut(u8) -> bool,
Self: Sized;
fn trim_ascii_start(&self) -> Self;
fn trim_ascii_end(&self) -> Self;
fn trim_ascii(&self) -> Self
where
Self: Sized,
{
self.trim_ascii_start().trim_ascii_end()
}
}
impl AsciiExt for Bytes {
fn split_once<F>(&self, mut pred: F) -> Option<(Self, Self)>
where
F: FnMut(u8) -> bool,
{
self.iter().position(|&b| pred(b)).map(|idx| {
let start = self.slice(..idx);
let end = self.slice(idx + 1..);
(start, end)
})
}
fn trim_ascii_start(&self) -> Self {
let start = self
.iter()
.position(|&b| !b.is_ascii_whitespace())
.unwrap_or(self.len());
self.slice(start..)
}
fn trim_ascii_end(&self) -> Self {
let end = self
.iter()
.rposition(|&b| !b.is_ascii_whitespace())
.unwrap_or(usize::MAX)
.wrapping_add(1);
self.slice(..end)
}
}