globset: reduce owned path copying in globset
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #3454
authored by kmg0308 · committed by Andrew Gallant · parent 3a57099 · Browse files · 2 files·+21 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+21 added−13 removed
- other:+21 added−13 removed
Provenance
- Commit
068a63e- Tree
9f4e57b- Author
kmg0308 <113580700+kmg0308@users.noreply.github.com>- Committer
Andrew Gallant <jamslam@gmail.com>- Parent
3a57099
2 files changed+21 added−13 removed
modified:crates/globset/src/lib.rs+1 added-1 removed
381 unmodified lines382 383 /// Returns true if all globs in this set match the path given.384 ///385 /// This takes a Candidate as input, which can be used to amortize the cost386- /// of peparing a path for matching.386+ /// of preparing a path for matching.387 ///388 /// This will return true if the set of globs is empty, as in that case all389 /// `0` of the globs will match.390 pub fn matches_all_candidate(&self, path: &Candidate<'_>) -> bool {749 unmodified linesmodified:crates/globset/src/pathutil.rs+20 added-12 removed
1 use std::borrow::Cow;2 3-use bstr::{ByteSlice, ByteVec};3+use bstr::ByteSlice;4 5 /// The final component of the path, if it is a normal file.6 ///7 /// If the path terminates in `..`, or consists solely of a root of prefix,4 unmodified lines12 }13 let last_slash = path.rfind_byte(b'/').map(|i| i + 1).unwrap_or(0);14 let got = match *path {15 Cow::Borrowed(path) => Cow::Borrowed(&path[last_slash..]),16- Cow::Owned(ref path) => {17- let mut path = path.clone();18- path.drain_bytes(..last_slash);19- Cow::Owned(path)20- }16+ Cow::Owned(ref path) => Cow::Owned(path[last_slash..].to_vec()),17 };18 if got == &b".."[..] {19 return None;20 }27 unmodified lines48 Some(i) => i,49 };50 Some(match *name {51 Cow::Borrowed(name) => Cow::Borrowed(&name[last_dot_at..]),56- Cow::Owned(ref name) => {57- let mut name = name.clone();58- name.drain_bytes(..last_dot_at);59- Cow::Owned(name)60- }52+ Cow::Owned(ref name) => Cow::Owned(name[last_dot_at..].to_vec()),53 })54 }55 56 /// Normalizes a path to use `/` as a separator everywhere, even on platforms24 unmodified lines81 use std::borrow::Cow;82 83 use bstr::{B, ByteVec};84 93- use super::{file_name_ext, normalize_path};85+ use super::{file_name, file_name_ext, normalize_path};86 87+ 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+ }97+98+ name!(name1, "foo/bar", Some("bar"));99+ name!(name2, "foo", Some("foo"));100+ name!(name3, "foo/..", None::<&str>);101+ name!(name4, "", None::<&str>);102+103 macro_rules! ext {104 ($name:ident, $file_name:expr, $ext:expr) => {105 #[test]106 fn $name() {35 unmodified lines