From be7d6dd9ced86d22f440d5cb79f419a28ca53994 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Fri, 5 Apr 2019 21:03:22 -0400 Subject: regex: print out final regex in trace mode This is useful for debugging to see what regex is actually being run. We put this as a trace since the regex can be quite gnarly. (It is not pretty printed.) --- grep-regex/src/crlf.rs | 5 +++++ grep-regex/src/matcher.rs | 14 +++++++++++++- grep-regex/src/word.rs | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/grep-regex/src/crlf.rs b/grep-regex/src/crlf.rs index 838a28f8..244f536e 100644 --- a/grep-regex/src/crlf.rs +++ b/grep-regex/src/crlf.rs @@ -34,6 +34,11 @@ impl CRLFMatcher { } Ok(CRLFMatcher { regex, names }) } + + /// Return the underlying regex used by this matcher. + pub fn regex(&self) -> &Regex { + &self.regex + } } impl Matcher for CRLFMatcher { diff --git a/grep-regex/src/matcher.rs b/grep-regex/src/matcher.rs index b15d824c..391439d9 100644 --- a/grep-regex/src/matcher.rs +++ b/grep-regex/src/matcher.rs @@ -50,9 +50,12 @@ impl RegexMatcherBuilder { if let Some(ref re) = fast_line_regex { trace!("extracted fast line regex: {:?}", re); } + + let matcher = RegexMatcherImpl::new(&chir)?; + trace!("final regex: {:?}", matcher.regex()); Ok(RegexMatcher { config: self.config.clone(), - matcher: RegexMatcherImpl::new(&chir)?, + matcher: matcher, fast_line_regex: fast_line_regex, non_matching_bytes: non_matching_bytes, }) @@ -370,6 +373,15 @@ impl RegexMatcherImpl { Ok(RegexMatcherImpl::Standard(StandardMatcher::new(expr)?)) } } + + /// Return the underlying regex object used. + fn regex(&self) -> &Regex { + match *self { + RegexMatcherImpl::Word(ref x) => x.regex(), + RegexMatcherImpl::CRLF(ref x) => x.regex(), + RegexMatcherImpl::Standard(ref x) => &x.regex, + } + } } // This implementation just dispatches on the internal matcher impl except diff --git a/grep-regex/src/word.rs b/grep-regex/src/word.rs index be740020..86291f20 100644 --- a/grep-regex/src/word.rs +++ b/grep-regex/src/word.rs @@ -55,6 +55,11 @@ impl WordMatcher { } Ok(WordMatcher { regex, names, locs }) } + + /// Return the underlying regex used by this matcher. + pub fn regex(&self) -> &Regex { + &self.regex + } } impl Matcher for WordMatcher { -- cgit v1.2.3