diff options
author | sharkdp <davidpeter@web.de> | 2018-02-24 17:02:14 +0100 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2018-02-25 10:25:59 +0100 |
commit | e5ee5eb7b360d3e15efd582d0ab5e11b880046e1 (patch) | |
tree | 4854872faa04f60a9cd51120aea11b9b77d5a35c | |
parent | 82eeeda5044fa16cc6b359f8c813038d1319758b (diff) |
Minor speedup for strip_prefix
-rw-r--r-- | src/output.rs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/output.rs b/src/output.rs index cc3fb46..115b31a 100644 --- a/src/output.rs +++ b/src/output.rs @@ -20,8 +20,22 @@ use std::os::unix::fs::PermissionsExt; use ansi_term; +/// Remove the `./` prefix from a path. +fn strip_current_dir<'a>(pathbuf: &'a PathBuf) -> &'a Path { + let mut iter = pathbuf.components(); + let mut iter_next = iter.clone(); + if iter_next.next() == Some(Component::CurDir) { + iter.next(); + } + iter.as_path() +} + pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) { - let path = entry.strip_prefix(".").unwrap_or(entry); + let path = if entry.is_absolute() { + entry.as_path() + } else { + strip_current_dir(entry) + }; let r = if let Some(ref ls_colors) = config.ls_colors { print_entry_colorized(path, config, ls_colors, &wants_to_quit) |