diff --git a/rust/rhgitaly/src/glob.rs b/rust/rhgitaly/src/glob.rs index 5b12e3d7c9ed1fa0f61d4feff7632d49fb1ba066_cnVzdC9yaGdpdGFseS9zcmMvZ2xvYi5ycw==..75146abe2946c51494fcd4a00e7b5bd13d1df5a0_cnVzdC9yaGdpdGFseS9zcmMvZ2xvYi5ycw== 100644 --- a/rust/rhgitaly/src/glob.rs +++ b/rust/rhgitaly/src/glob.rs @@ -6,5 +6,6 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. use hg::filepatterns; +use hg::matchers; use regex::bytes::Regex; @@ -9,6 +10,5 @@ use regex::bytes::Regex; -use std::io::Write; /// These are matched in order @@ -24,26 +24,5 @@ /// - we don't need huge regexps, as the main use case in RHGitaly is `fnmatch` on path segments. #[allow(rustdoc::broken_intra_doc_links)] pub fn glob_to_regex(pat: &[u8]) -> Regex { - let rx_pat = filepatterns::glob_to_re(pat); - let pattern = &rx_pat; - - // The `regex` crate adds `.*` to the start and end of expressions if there - // are no anchors, so add the start anchor. - let mut escaped_bytes = vec![b'^', b'(', b'?', b':']; - for byte in pattern { - if *byte > 127 { - write!(escaped_bytes, "\\x{:x}", *byte).unwrap(); - } else { - escaped_bytes.push(*byte); - } - } - escaped_bytes.push(b')'); - - // Avoid the cost of UTF8 checking - // - // # Safety - // This is safe because we escaped all non-ASCII bytes. - let pattern_string = unsafe { String::from_utf8_unchecked(escaped_bytes) }; - regex::bytes::RegexBuilder::new(&pattern_string) - .unicode(false) + matchers::re_bytes_builder(&filepatterns::glob_to_re(pat)) .build() @@ -49,3 +28,3 @@ .build() - .unwrap() // TODO unwrap. In theory (must be checked) we cannot make a wrong regexp pattern + .expect("A glob pattern should always convert to a valid regular expression") }