pathutil.rs
This file
Path
crates/globset/src/pathutil.rs
141 lines · 4.3 KiB · Rust
Last changed by
globset: reduce owned path copying in globsetkmg0308before adoption068a63e
History of this fileTake it
The walk stopped after 200 commits: lines older than 5f5da48 point at it.
globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 1–2why, one step back1use std::borrow::Cow;2globset: reduce owned path copying in globsetkmg0308before adoption068a63e
line 3why, one step back3use bstr::ByteSlice;globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 4–6why, one step back45/// The final component of the path, if it is a normal file.6///globset: fix bug where trailing `.` in file name was incorrectly handledAndrew Gallantbefore adoption4df1298
lines 7–8why, one step back7/// If the path terminates in `..`, or consists solely of a root of prefix,8/// file_name will return `None`.globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
line 9why, one step back9pub(crate) fn file_name<'a>(path: &Cow<'a, [u8]>) -> Option<Cow<'a, [u8]>> {globset: fix bug where trailing `.` in file name was incorrectly handledAndrew Gallantbefore adoption4df1298
line 10why, one step back10 if path.is_empty() {globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 11–13why, one step back11 return None;12 }13 let last_slash = path.rfind_byte(b'/').map(|i| i + 1).unwrap_or(0);globset: fix bug where trailing `.` in file name was incorrectly handledAndrew Gallantbefore adoption4df1298
line 14why, one step back14 let got = match *path {globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
line 15why, one step back15 Cow::Borrowed(path) => Cow::Borrowed(&path[last_slash..]),globset: reduce owned path copying in globsetkmg0308before adoption068a63e
line 16why, one step back16 Cow::Owned(ref path) => Cow::Owned(path[last_slash..].to_vec()),globset: fix bug where trailing `.` in file name was incorrectly handledAndrew Gallantbefore adoption4df1298
lines 17–21why, one step back17 };18 if got == &b".."[..] {19 return None;20 }21 Some(got)globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 22–51why, one step back22}2324/// Return a file extension given a path's file name.25///26/// Note that this does NOT match the semantics of std::path::Path::extension.27/// Namely, the extension includes the `.` and matching is otherwise more28/// liberal. Specifically, the extension is:29///30/// * None, if the file name given is empty;31/// * None, if there is no embedded `.`;32/// * Otherwise, the portion of the file name starting with the final `.`.33///34/// e.g., A file name of `.rs` has an extension `.rs`.35///36/// N.B. This is done to make certain glob match optimizations easier. Namely,37/// a pattern like `*.rs` is obviously trying to match files with a `rs`38/// extension, but it also matches files like `.rs`, which doesn't have an39/// extension according to std::path::Path::extension.40pub(crate) fn file_name_ext<'a>(41 name: &Cow<'a, [u8]>,42) -> Option<Cow<'a, [u8]>> {43 if name.is_empty() {44 return None;45 }46 let last_dot_at = match name.rfind_byte(b'.') {47 None => return None,48 Some(i) => i,49 };50 Some(match *name {51 Cow::Borrowed(name) => Cow::Borrowed(&name[last_dot_at..]),globset: reduce owned path copying in globsetkmg0308before adoption068a63e
line 52why, one step back52 Cow::Owned(ref name) => Cow::Owned(name[last_dot_at..].to_vec()),globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 53–82why, one step back53 })54}5556/// Normalizes a path to use `/` as a separator everywhere, even on platforms57/// that recognize other characters as separators.58#[cfg(unix)]59pub(crate) fn normalize_path(path: Cow<'_, [u8]>) -> Cow<'_, [u8]> {60 // UNIX only uses /, so we're good.61 path62}6364/// Normalizes a path to use `/` as a separator everywhere, even on platforms65/// that recognize other characters as separators.66#[cfg(not(unix))]67pub(crate) fn normalize_path(mut path: Cow<[u8]>) -> Cow<[u8]> {68 use std::path::is_separator;6970 for i in 0..path.len() {71 if path[i] == b'/' || !is_separator(char::from(path[i])) {72 continue;73 }74 path.to_mut()[i] = b'/';75 }76 path77}7879#[cfg(test)]80mod tests {81 use std::borrow::Cow;82style: apply rustfmtAndrew Gallantbefore adoptionbb8172f
line 83why, one step back83 use bstr::{B, ByteVec};globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
line 84why, one step back84globset: reduce owned path copying in globsetkmg0308before adoption068a63e
lines 85–101why, one step back85 use super::{file_name, file_name_ext, normalize_path};8687 macro_rules! name {88 ($name:ident, $path:expr, $expected:expr) => {89 #[test]90 fn $name() {91 let bs = Vec::from($path);92 let got = file_name(&Cow::Owned(bs));93 assert_eq!($expected.map(|s| Cow::Borrowed(B(s))), got);94 }95 };96 }9798 name!(name1, "foo/bar", Some("bar"));99 name!(name2, "foo", Some("foo"));100 name!(name3, "foo/..", None::<&str>);101 name!(name4, "", None::<&str>);globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 102–141why, one step back102103 macro_rules! ext {104 ($name:ident, $file_name:expr, $ext:expr) => {105 #[test]106 fn $name() {107 let bs = Vec::from($file_name);108 let got = file_name_ext(&Cow::Owned(bs));109 assert_eq!($ext.map(|s| Cow::Borrowed(B(s))), got);110 }111 };112 }113114 ext!(ext1, "foo.rs", Some(".rs"));115 ext!(ext2, ".rs", Some(".rs"));116 ext!(ext3, "..rs", Some(".rs"));117 ext!(ext4, "", None::<&str>);118 ext!(ext5, "foo", None::<&str>);119120 macro_rules! normalize {121 ($name:ident, $path:expr, $expected:expr) => {122 #[test]123 fn $name() {124 let bs = Vec::from_slice($path);125 let got = normalize_path(Cow::Owned(bs));126 assert_eq!($expected.to_vec(), got.into_owned());127 }128 };129 }130131 normalize!(normal1, b"foo", b"foo");132 normalize!(normal2, b"foo/bar", b"foo/bar");133 #[cfg(unix)]134 normalize!(normal3, b"foo\\bar", b"foo\\bar");135 #[cfg(not(unix))]136 normalize!(normal3, b"foo\\bar", b"foo/bar");137 #[cfg(unix)]138 normalize!(normal4, b"foo\\bar/baz", b"foo\\bar/baz");139 #[cfg(not(unix))]140 normalize!(normal4, b"foo\\bar/baz", b"foo/bar/baz");141}