Determines whether regexp matches must be anchored with a fixed string prefix. If so, returns the prefix and the regexp that remains after the prefix. The prefix might be ASCII case-insensitive.
| 677 | // the regexp that remains after the prefix. The prefix might |
| 678 | // be ASCII case-insensitive. |
| 679 | bool Regexp::RequiredPrefix(std::string* prefix, bool* foldcase, |
| 680 | Regexp** suffix) { |
| 681 | prefix->clear(); |
| 682 | *foldcase = false; |
| 683 | *suffix = NULL; |
| 684 | |
| 685 | // No need for a walker: the regexp must be of the form |
| 686 | // 1. some number of ^ anchors |
| 687 | // 2. a literal char or string |
| 688 | // 3. the rest |
| 689 | if (op_ != kRegexpConcat) |
| 690 | return false; |
| 691 | int i = 0; |
| 692 | while (i < nsub_ && sub()[i]->op_ == kRegexpBeginText) |
| 693 | i++; |
| 694 | if (i == 0 || i >= nsub_) |
| 695 | return false; |
| 696 | Regexp* re = sub()[i]; |
| 697 | if (re->op_ != kRegexpLiteral && |
| 698 | re->op_ != kRegexpLiteralString) |
| 699 | return false; |
| 700 | i++; |
| 701 | if (i < nsub_) { |
| 702 | for (int j = i; j < nsub_; j++) |
| 703 | sub()[j]->Incref(); |
| 704 | *suffix = Concat(sub() + i, nsub_ - i, parse_flags()); |
| 705 | } else { |
| 706 | *suffix = new Regexp(kRegexpEmptyMatch, parse_flags()); |
| 707 | } |
| 708 | |
| 709 | bool latin1 = (re->parse_flags() & Latin1) != 0; |
| 710 | Rune* runes = re->op_ == kRegexpLiteral ? &re->rune_ : re->runes_; |
| 711 | int nrunes = re->op_ == kRegexpLiteral ? 1 : re->nrunes_; |
| 712 | ConvertRunesToBytes(latin1, runes, nrunes, prefix); |
| 713 | *foldcase = (re->parse_flags() & FoldCase) != 0; |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | // Determines whether regexp matches must be unanchored |
| 718 | // with a fixed string prefix. If so, returns the prefix. |