autocommit 2022-03-23 16:23:28 CET
This commit is contained in:
parent
d7f298ab5f
commit
a9a4fc04b7
13 changed files with 91 additions and 70 deletions
33
src/utils/trim.rs
Normal file
33
src/utils/trim.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
pub trait AsciiTrim {
|
||||
fn trim(&self) -> &[u8];
|
||||
fn trim_start(&self) -> &[u8];
|
||||
fn trim_end(&self) -> &[u8];
|
||||
}
|
||||
|
||||
impl AsciiTrim for [u8] {
|
||||
fn trim(&self) -> &[u8] {
|
||||
self.trim_start().trim_end()
|
||||
}
|
||||
|
||||
fn trim_start(&self) -> &[u8] {
|
||||
let mut s = self;
|
||||
while let Some((b, r)) = s.split_first() {
|
||||
if !matches!(*b, b' ' | b'\t' | b'\n') {
|
||||
break;
|
||||
}
|
||||
s = r;
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn trim_end(&self) -> &[u8] {
|
||||
let mut s = self;
|
||||
while let Some((b, r)) = s.split_last() {
|
||||
if !matches!(*b, b' ' | b'\t' | b'\n') {
|
||||
break;
|
||||
}
|
||||
s = r;
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue