diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 18:31:44 +0000 |
commit | c23a457e72abe608715ac76f076f47dc42af07a5 (patch) | |
tree | 2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /vendor/gix-glob/src/pattern.rs | |
parent | Releasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip |
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix-glob/src/pattern.rs')
-rw-r--r-- | vendor/gix-glob/src/pattern.rs | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/vendor/gix-glob/src/pattern.rs b/vendor/gix-glob/src/pattern.rs index 3c0dbfea2..475b912b0 100644 --- a/vendor/gix-glob/src/pattern.rs +++ b/vendor/gix-glob/src/pattern.rs @@ -44,7 +44,18 @@ pub enum Case { impl Pattern { /// Parse the given `text` as pattern, or return `None` if `text` was empty. pub fn from_bytes(text: &[u8]) -> Option<Self> { - crate::parse::pattern(text).map(|(text, mode, first_wildcard_pos)| Pattern { + crate::parse::pattern(text, true).map(|(text, mode, first_wildcard_pos)| Pattern { + text: text.into(), + mode, + first_wildcard_pos, + }) + } + + /// Parse the given `text` as pattern without supporting leading `!` or `\\!` , or return `None` if `text` was empty. + /// + /// This assures that `text` remains entirely unaltered, but removes built-in support for negation as well. + pub fn from_bytes_without_negation(text: &[u8]) -> Option<Self> { + crate::parse::pattern(text, false).map(|(text, mode, first_wildcard_pos)| Pattern { text: text.into(), mode, first_wildcard_pos, @@ -65,30 +76,36 @@ impl Pattern { /// We may take various shortcuts which is when `basename_start_pos` and `is_dir` come into play. /// `basename_start_pos` is the index at which the `path`'s basename starts. /// - /// Lastly, `case` folding can be configured as well. - pub fn matches_repo_relative_path<'a>( + /// `case` folding can be configured as well. + /// `mode` is used to control how [`crate::wildmatch()`] should operate. + pub fn matches_repo_relative_path( &self, - path: impl Into<&'a BStr>, + path: &BStr, basename_start_pos: Option<usize>, is_dir: Option<bool>, case: Case, + mode: wildmatch::Mode, ) -> bool { let is_dir = is_dir.unwrap_or(false); if !is_dir && self.mode.contains(pattern::Mode::MUST_BE_DIR) { return false; } - let flags = wildmatch::Mode::NO_MATCH_SLASH_LITERAL + let flags = mode | match case { Case::Fold => wildmatch::Mode::IGNORE_CASE, Case::Sensitive => wildmatch::Mode::empty(), }; - let path = path.into(); - debug_assert_eq!( - basename_start_pos, - path.rfind_byte(b'/').map(|p| p + 1), - "BUG: invalid cached basename_start_pos provided" - ); + #[cfg(debug_assertions)] + { + if basename_start_pos.is_some() { + debug_assert_eq!( + basename_start_pos, + path.rfind_byte(b'/').map(|p| p + 1), + "BUG: invalid cached basename_start_pos provided" + ); + } + } debug_assert!(!path.starts_with(b"/"), "input path must be relative"); if self.mode.contains(pattern::Mode::NO_SUB_DIR) && !self.mode.contains(pattern::Mode::ABSOLUTE) { @@ -106,11 +123,13 @@ impl Pattern { /// /// Note that this method uses some shortcuts to accelerate simple patterns, but falls back to /// [wildmatch()][crate::wildmatch()] if these fail. - pub fn matches<'a>(&self, value: impl Into<&'a BStr>, mode: wildmatch::Mode) -> bool { - let value = value.into(); + pub fn matches(&self, value: &BStr, mode: wildmatch::Mode) -> bool { match self.first_wildcard_pos { // "*literal" case, overrides starts-with - Some(pos) if self.mode.contains(pattern::Mode::ENDS_WITH) && !value.contains(&b'/') => { + Some(pos) + if self.mode.contains(pattern::Mode::ENDS_WITH) + && (!mode.contains(wildmatch::Mode::NO_MATCH_SLASH_LITERAL) || !value.contains(&b'/')) => + { let text = &self.text[pos + 1..]; if mode.contains(wildmatch::Mode::IGNORE_CASE) { value |