ignore: more pathutil clean-up
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrew Gallant · committed by Andrew Gallant · parent 6e527e9 · Browse files · 1 file·+56 added−77 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+56 added−77 removed
- other:+56 added−77 removed
Provenance
- Commit
c55818f- Tree
a26f21e- Author
Andrew Gallant <jamslam@gmail.com>- Committer
Andrew Gallant <jamslam@gmail.com>- Parent
6e527e9
1 file changed+56 added−77 removed
modified:crates/ignore/src/pathutil.rs+56 added-77 removed
81 unmodified lines82 /// the path itself.83 ///84 /// This has the same behavior on all platforms.85 fn is_hidden_path_only(path: &Path) -> bool {86- #[cfg(unix)]87- fn imp(path: &Path) -> bool {88- use std::os::unix::ffi::OsStrExt;89-90- if let Some(name) = file_name(path) {91- name.as_bytes().get(0) == Some(&b'.')92- } else {93- false86+ if let Some(name) = file_name(path) {87+ name.as_encoded_bytes().starts_with(b".")88+ } else {89+ false94- }90 }96-97- #[cfg(not(unix))]98- fn imp(path: &Path) -> bool {99- if let Some(name) = file_name(path) {100- name.to_str().map(|s| s.starts_with(".")).unwrap_or(false)101- } else {102- false103- }104- }105-106- imp(path)91 }92 93 /// Strip `prefix` from the `path` and return the remainder.94 ///95 /// If `path` doesn't have a prefix `prefix`, then return `None`.112-#[cfg(unix)]96 pub(crate) fn strip_prefix<'a, P: AsRef<Path> + ?Sized>(97 prefix: &'a P,98 path: &'a Path,99 ) -> Option<&'a Path> {100+ #[cfg(unix)]101+ fn imp<'a>(prefix: &'a Path, path: &'a Path) -> Option<&'a Path> {117- use std::os::unix::ffi::OsStrExt;102+ use std::os::unix::ffi::OsStrExt;103 119- let prefix = prefix.as_ref().as_os_str().as_bytes();120- let path = path.as_os_str().as_bytes();121- if prefix.len() > path.len() || prefix != &path[0..prefix.len()] {122- None123- } else {124- Some(&Path::new(OsStr::from_bytes(&path[prefix.len()..])))104+ let prefix = prefix.as_os_str().as_bytes();105+ let path = path.as_os_str().as_bytes();106+ if prefix.len() > path.len() || prefix != &path[0..prefix.len()] {107+ None108+ } else {109+ Some(&Path::new(OsStr::from_bytes(&path[prefix.len()..])))110+ }111 }126-}112 128-/// Strip `prefix` from the `path` and return the remainder.129-///130-/// If `path` doesn't have a prefix `prefix`, then return `None`.131-#[cfg(not(unix))]132-pub(crate) fn strip_prefix<'a, P: AsRef<Path> + ?Sized>(133- prefix: &'a P,134- path: &'a Path,135-) -> Option<&'a Path> {136- path.strip_prefix(prefix).ok()137-}113+ #[cfg(not(unix))]114+ fn imp<'a>(prefix: &'a Path, path: &'a Path) -> Option<&'a Path> {115+ path.strip_prefix(prefix).ok()116+ }117 139-/// Returns true if this file path is just a file name. i.e., Its parent is140-/// the empty string.141-#[cfg(unix)]142-pub(crate) fn is_file_name<P: AsRef<Path>>(path: P) -> bool {143- use std::os::unix::ffi::OsStrExt;144-145- use memchr::memchr;146-147- let path = path.as_ref().as_os_str().as_bytes();148- memchr(b'/', path).is_none()118+ imp(prefix.as_ref(), path)119 }120 121 /// Returns true if this file path is just a file name. i.e., Its parent is122 /// the empty string.153-#[cfg(not(unix))]123 pub(crate) fn is_file_name<P: AsRef<Path>>(path: P) -> bool {155- path.as_ref().parent().map(|p| p.as_os_str().is_empty()).unwrap_or(false)124+ #[cfg(unix)]125+ {126+ memchr::memchr(b'/', path.as_ref().as_os_str().as_encoded_bytes())127+ .is_none()128+ }129+ #[cfg(not(unix))]130+ {131+ path.as_ref()132+ .parent()133+ .map(|p| p.as_os_str().is_empty())134+ .unwrap_or(false)135+ }136 }137 138 /// The final component of the path, if it is a normal file.139 ///160-/// If the path terminates in ., .., or consists solely of a root of prefix,161-/// file_name will return None.162-#[cfg(unix)]140+/// If the path terminates in `.`, `..`, or consists solely of a root of141+/// prefix, this will return `None`.142 pub(crate) fn file_name<'a, P: AsRef<Path> + ?Sized>(143 path: &'a P,144 ) -> Option<&'a OsStr> {166- use memchr::memrchr;167- use std::os::unix::ffi::OsStrExt;145+ #[cfg(unix)]146+ fn imp(path: &Path) -> Option<&OsStr> {147+ use std::os::unix::ffi::OsStrExt;148 149+ use memchr::memrchr;150+169- let path = path.as_ref().as_os_str().as_bytes();170- if path.is_empty() {171- return None;172- } else if path.len() == 1 && path[0] == b'.' {173- return None;174- } else if path.last() == Some(&b'.') {175- return None;176- } else if path.len() >= 2 && &path[path.len() - 2..] == &b".."[..] {177- return None;151+ let path = path.as_os_str().as_bytes();152+ if path.is_empty() {153+ return None;154+ } else if path.len() == 1 && path[0] == b'.' {155+ return None;156+ } else if path.last() == Some(&b'.') {157+ return None;158+ } else if path.len() >= 2 && &path[path.len() - 2..] == &b".."[..] {159+ return None;160+ }161+ let last_slash = memrchr(b'/', path).map(|i| i + 1).unwrap_or(0);162+ Some(OsStr::from_bytes(&path[last_slash..]))163 }179- let last_slash = memrchr(b'/', path).map(|i| i + 1).unwrap_or(0);180- Some(OsStr::from_bytes(&path[last_slash..]))181-}164 183-/// The final component of the path, if it is a normal file.184-///185-/// If the path terminates in ., .., or consists solely of a root of prefix,186-/// file_name will return None.187-#[cfg(not(unix))]188-pub(crate) fn file_name<'a, P: AsRef<Path> + ?Sized>(189- path: &'a P,190-) -> Option<&'a OsStr> {191- path.as_ref().file_name()165+ #[cfg(not(unix))]166+ fn imp(path: &Path) -> Option<&OsStr> {167+ path.file_name()168+ }169+170+ imp(path.as_ref())171 }