diff options
author | Andrew Gallant <jamslam@gmail.com> | 2016-09-16 16:13:28 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2016-09-16 16:13:28 -0400 |
commit | 0d14c74e634563c29877a33db3dc6f56ea16a95d (patch) | |
tree | 8ed420e3ce81629b4ffd4f125c946a36b9b20988 /src/pathutil.rs | |
parent | 1c5884b2f9cc74211fb86fe8af7f6d089e764ebb (diff) |
Some minor performance tweaks.
This includes moving basename-only globs into separate regexes. The hope
is that if the regex processes less input, it will be faster.
Diffstat (limited to 'src/pathutil.rs')
-rw-r--r-- | src/pathutil.rs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/pathutil.rs b/src/pathutil.rs index 3cc92f7b..01342ac0 100644 --- a/src/pathutil.rs +++ b/src/pathutil.rs @@ -11,6 +11,8 @@ improvement on just listing the files to search (!). use std::ffi::OsStr; use std::path::Path; +use memchr::memrchr; + /// Strip `prefix` from the `path` and return the remainder. /// /// If `path` doesn't have a prefix `prefix`, then return `None`. @@ -58,13 +60,7 @@ pub fn file_name<'a, P: AsRef<Path> + ?Sized>( } else if path.len() >= 2 && &path[path.len() - 2..] == &b".."[..] { return None; } - let mut last_slash = 0; - for (i, &b) in path.iter().enumerate().rev() { - if b == b'/' { - last_slash = i + 1; - break; - } - } + let last_slash = memrchr(b'/', path).map(|i| i + 1).unwrap_or(0); Some(OsStr::from_bytes(&path[last_slash..])) } @@ -78,3 +74,25 @@ pub fn file_name<'a, P: AsRef<Path> + ?Sized>( ) -> Option<&'a OsStr> { path.as_ref().file_name() } + +/// Returns true if and only if this file path is considered to be hidden. +#[cfg(unix)] +pub fn is_hidden<P: AsRef<Path>>(path: P) -> bool { + use std::os::unix::ffi::OsStrExt; + + if let Some(name) = file_name(path.as_ref()) { + name.as_bytes().get(0) == Some(&b'.') + } else { + false + } +} + +/// Returns true if and only if this file path is considered to be hidden. +#[cfg(not(unix))] +pub fn is_hidden<P: AsRef<Path>>(path: P) -> bool { + if let Some(name) = file_name(path) { + name.to_str().map(|s| s.starts_with(".")).unwrap_or(false) + } else { + false + } +} |