Skip to content
Myr

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

1 file+56 added−77 removed

modified:crates/ignore/src/pathutil.rs+56 added-77 removed
81 unmodified lines
82/// the path itself.83///84/// This has the same behavior on all platforms.85fn is_hidden_path_only(path: &Path) -> bool {86 #[cfg(unix)]87 fn imp(path: &Path) -> bool {88 use std::os::unix::ffi::OsStrExt;8990 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 }9697 #[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 }105106 imp(path)91}9293/// 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)]96pub(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;103119 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}112128/// 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))]132pub(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 }117139/// Returns true if this file path is just a file name. i.e., Its parent is140/// the empty string.141#[cfg(unix)]142pub(crate) fn is_file_name<P: AsRef<Path>>(path: P) -> bool {143 use std::os::unix::ffi::OsStrExt;144145 use memchr::memchr;146147 let path = path.as_ref().as_os_str().as_bytes();148 memchr(b'/', path).is_none()118 imp(prefix.as_ref(), path)119}120121/// Returns true if this file path is just a file name. i.e., Its parent is122/// the empty string.153#[cfg(not(unix))]123pub(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}137138/// 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`.142pub(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;148149 use memchr::memrchr;150169 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}164183/// 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))]188pub(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 }169170 imp(path.as_ref())171}