Skip to content

Commit

Permalink
Check before matching in order to avoid wasting time.
Browse files Browse the repository at this point in the history
Change-Id: I3e53e2b01c222b8a8d40a082157f5f8bd95c5a42
Reviewed-on: https://code-review.googlesource.com/32450
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Aug 30, 2018
1 parent bbcb676 commit d499a52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
14 changes: 6 additions & 8 deletions re2/re2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ bool RE2::Match(const StringPiece& text,
Anchor re_anchor,
StringPiece* submatch,
int nsubmatch) const {
if (!ok() || suffix_regexp_ == NULL) {
if (!ok()) {
if (options_.log_errors())
LOG(ERROR) << "Invalid RE2: " << *error_;
return false;
Expand Down Expand Up @@ -784,6 +784,11 @@ bool RE2::DoMatch(const StringPiece& text,
return false;
}

if (NumberOfCapturingGroups() < n) {
// RE has fewer capturing groups than number of Arg pointers passed in.
return false;
}

// Count number of capture groups needed.
int nvec;
if (n == 0 && consumed == NULL)
Expand Down Expand Up @@ -816,13 +821,6 @@ bool RE2::DoMatch(const StringPiece& text,
return true;
}

int ncap = NumberOfCapturingGroups();
if (ncap < n) {
// RE has fewer capturing groups than number of arg pointers passed in
delete[] heapvec;
return false;
}

// If we got here, we must have matched the whole pattern.
for (int i = 0; i < n; i++) {
const StringPiece& s = vec[i+1];
Expand Down
9 changes: 5 additions & 4 deletions util/pcre.cc
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,11 @@ bool PCRE::DoMatchImpl(const StringPiece& text,
int n,
int* vec,
int vecsize) const {
if (NumberOfCapturingGroups() < n) {
// RE has fewer capturing groups than number of Arg pointers passed in.
return false;
}

assert((1 + n) * 3 <= vecsize); // results + PCRE workspace
int matches = TryMatch(text, 0, anchor, true, vec, vecsize);
assert(matches >= 0); // TryMatch never returns negatives
Expand All @@ -624,10 +629,6 @@ bool PCRE::DoMatchImpl(const StringPiece& text,
// We are not interested in results
return true;
}
if (NumberOfCapturingGroups() < n) {
// PCRE has fewer capturing groups than number of arg pointers passed in
return false;
}

// If we got here, we must have matched the whole pattern.
// We do not need (can not do) any more checks on the value of 'matches' here
Expand Down

0 comments on commit d499a52

Please sign in to comment.