D7924: rust-matchers: add `build_regex_match` function

Alphare (Raphaël Gomès) phabricator at mercurial-scm.org
Tue Feb 11 11:13:51 UTC 2020


Alphare updated this revision to Diff 20162.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7924?vs=20042&id=20162

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7924/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7924

AFFECTED FILES
  rust/hg-core/src/matchers.rs

CHANGE DETAILS

diff --git a/rust/hg-core/src/matchers.rs b/rust/hg-core/src/matchers.rs
--- a/rust/hg-core/src/matchers.rs
+++ b/rust/hg-core/src/matchers.rs
@@ -10,7 +10,7 @@
 #[cfg(feature = "with-re2")]
 use crate::re2::Re2;
 use crate::{
-    filepatterns::PatternResult,
+    filepatterns::{build_single_regex, PatternResult},
     utils::hg_path::{HgPath, HgPathBuf},
     DirsMultiset, DirstateMapError, IgnorePattern, PatternError,
     PatternSyntax,
@@ -242,6 +242,24 @@
     Err(PatternError::Re2NotInstalled)
 }
 
+/// Returns the regex pattern and a function that matches an `HgPath` against
+/// said regex formed by the given ignore patterns.
+fn build_regex_match<'a>(
+    ignore_patterns: &'a [&'a IgnorePattern],
+) -> PatternResult<(Vec<u8>, Box<dyn Fn(&HgPath) -> bool + Sync>)> {
+    let regexps: Result<Vec<_>, PatternError> = ignore_patterns
+        .into_iter()
+        .map(|k| build_single_regex(*k))
+        .collect();
+    let regexps = regexps?;
+    let full_regex = regexps.join(&b'|');
+
+    let matcher = re_matcher(&full_regex)?;
+    let func = Box::new(move |filename: &HgPath| matcher(filename));
+
+    Ok((full_regex, func))
+}
+
 /// Returns roots and directories corresponding to each pattern.
 ///
 /// This calculates the roots and directories exactly matching the patterns and



To: Alphare, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list