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'#') { let mut line = self;
// Ignore if '#' is at the beginning of line for (n, _) in self
&self[..0] .as_bytes()
} else { .iter()
let mut line = self; .enumerate()
for (n, _) in self .filter(|(_, c)| **c == b'#')
.as_bytes() {
.iter() // Ignore the content after '#'
.enumerate() // only if it is preceded by at least one whitespace
.rev() match self[..n].chars().last() {
.filter(|(_, c)| **c == b'#') Some(c) if c.is_whitespace() => {
{ line = &self[..n - c.len_utf8()];
// Ignore the content after '#'
// only if it is preceded by at least one whitespace
if self[..n].chars().last().unwrap().is_whitespace() {
line = &self[..n];
break; break;
} }
None => {
// n == 0
line = &self[..0];
break;
}
_ => (),
} }
line }
};
line.trim() line.trim()
} }