From 8a03ac6caa4095a14ef1f3d84db67e9dd4bf8b6e Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 10 Jun 2021 12:03:25 +0900 Subject: [PATCH 1/2] Prevent double scanning from dircolors --- src/uu/dircolors/src/dircolors.rs | 35 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 2fa2e8b91..80c94c047 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -191,24 +191,27 @@ pub trait StrUtils { impl StrUtils for str { fn purify(&self) -> &Self { - let mut line = self; - for (n, c) in self.chars().enumerate() { - if c != '#' { - continue; - } - + let line = if self.as_bytes().first() == Some(&b'#') { // Ignore if '#' is at the beginning of line - if n == 0 { - line = &self[..0]; - break; + &self[..0] + } else { + let mut line = self; + for (n, _) in self + .as_bytes() + .iter() + .enumerate() + .rev() + .filter(|(_, c)| **c == b'#') + { + // 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; + } } - - // Ignore the content after '#' - // only if it is preceded by at least one whitespace - if self.chars().nth(n - 1).unwrap().is_whitespace() { - line = &self[..n]; - } - } + line + }; line.trim() } From e3197bea39ca6093126dbdf1d24d834f03e09837 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 10 Jun 2021 18:14:23 +0900 Subject: [PATCH 2/2] dircolor purify forward match '#' --- src/uu/dircolors/src/dircolors.rs | 37 ++++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 80c94c047..530f051b3 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -191,27 +191,28 @@ pub trait StrUtils { impl StrUtils for str { 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; - for (n, _) in self - .as_bytes() - .iter() - .enumerate() - .rev() - .filter(|(_, c)| **c == b'#') - { - // 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]; + let mut line = self; + for (n, _) in self + .as_bytes() + .iter() + .enumerate() + .filter(|(_, c)| **c == b'#') + { + // Ignore the content after '#' + // only if it is preceded by at least one whitespace + match self[..n].chars().last() { + Some(c) if c.is_whitespace() => { + line = &self[..n - c.len_utf8()]; break; } + None => { + // n == 0 + line = &self[..0]; + break; + } + _ => (), } - line - }; + } line.trim() }