1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

dircolor purify forward match '#'

This commit is contained in:
Jeong YunWon 2021-06-10 18:14:23 +09:00
parent 8a03ac6caa
commit e3197bea39

View file

@ -191,27 +191,28 @@ pub trait StrUtils {
impl StrUtils for str { impl StrUtils for str {
fn purify(&self) -> &Self { fn purify(&self) -> &Self {
let line = if self.as_bytes().first() == Some(&b'#') {
// Ignore if '#' is at the beginning of line
&self[..0]
} else {
let mut line = self; let mut line = self;
for (n, _) in self for (n, _) in self
.as_bytes() .as_bytes()
.iter() .iter()
.enumerate() .enumerate()
.rev()
.filter(|(_, c)| **c == b'#') .filter(|(_, c)| **c == b'#')
{ {
// Ignore the content after '#' // Ignore the content after '#'
// only if it is preceded by at least one whitespace // only if it is preceded by at least one whitespace
if self[..n].chars().last().unwrap().is_whitespace() { match self[..n].chars().last() {
line = &self[..n]; Some(c) if c.is_whitespace() => {
line = &self[..n - c.len_utf8()];
break; break;
} }
None => {
// n == 0
line = &self[..0];
break;
}
_ => (),
}
} }
line
};
line.trim() line.trim()
} }