| 615 | /***** Actual matching and rewriting code *****/ |
| 616 | |
| 617 | bool RE2::Match(const StringPiece& text, |
| 618 | size_t startpos, |
| 619 | size_t endpos, |
| 620 | Anchor re_anchor, |
| 621 | StringPiece* submatch, |
| 622 | int nsubmatch) const { |
| 623 | if (!ok()) { |
| 624 | if (options_.log_errors()) |
| 625 | LOG(ERROR) << "Invalid RE2: " << *error_; |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | if (startpos > endpos || endpos > text.size()) { |
| 630 | if (options_.log_errors()) |
| 631 | LOG(ERROR) << "RE2: invalid startpos, endpos pair. [" |
| 632 | << "startpos: " << startpos << ", " |
| 633 | << "endpos: " << endpos << ", " |
| 634 | << "text size: " << text.size() << "]"; |
| 635 | return false; |
| 636 | } |
| 637 | |
| 638 | StringPiece subtext = text; |
| 639 | subtext.remove_prefix(startpos); |
| 640 | subtext.remove_suffix(text.size() - endpos); |
| 641 | |
| 642 | // Use DFAs to find exact location of match, filter out non-matches. |
| 643 | |
| 644 | // Don't ask for the location if we won't use it. |
| 645 | // SearchDFA can do extra optimizations in that case. |
| 646 | StringPiece match; |
| 647 | StringPiece* matchp = &match; |
| 648 | if (nsubmatch == 0) |
| 649 | matchp = NULL; |
| 650 | |
| 651 | int ncap = 1 + NumberOfCapturingGroups(); |
| 652 | if (ncap > nsubmatch) |
| 653 | ncap = nsubmatch; |
| 654 | |
| 655 | // If the regexp is anchored explicitly, must not be in middle of text. |
| 656 | if (prog_->anchor_start() && startpos != 0) |
| 657 | return false; |
| 658 | if (prog_->anchor_end() && endpos != text.size()) |
| 659 | return false; |
| 660 | |
| 661 | // If the regexp is anchored explicitly, update re_anchor |
| 662 | // so that we can potentially fall into a faster case below. |
| 663 | if (prog_->anchor_start() && prog_->anchor_end()) |
| 664 | re_anchor = ANCHOR_BOTH; |
| 665 | else if (prog_->anchor_start() && re_anchor != ANCHOR_BOTH) |
| 666 | re_anchor = ANCHOR_START; |
| 667 | |
| 668 | // Check for the required prefix, if any. |
| 669 | size_t prefixlen = 0; |
| 670 | if (!prefix_.empty()) { |
| 671 | if (startpos != 0) |
| 672 | return false; |
| 673 | prefixlen = prefix_.size(); |
| 674 | if (prefixlen > subtext.size()) |
no test coverage detected