Is this regexp required to start at the end of the text? Only approximate; can return false for complicated regexps like (a\z|b\z), but handles ((a|b)\z). Could use the Walker to write a more exact one.
| 1010 | // Only approximate; can return false for complicated regexps like (a\z|b\z), |
| 1011 | // but handles ((a|b)\z). Could use the Walker to write a more exact one. |
| 1012 | static bool IsAnchorEnd(Regexp** pre, int depth) { |
| 1013 | Regexp* re = *pre; |
| 1014 | Regexp* sub; |
| 1015 | // The depth limit makes sure that we don't overflow |
| 1016 | // the stack on a deeply nested regexp. As the comment |
| 1017 | // above says, IsAnchorEnd is conservative, so returning |
| 1018 | // a false negative is okay. The exact limit is somewhat arbitrary. |
| 1019 | if (re == NULL || depth >= 4) |
| 1020 | return false; |
| 1021 | switch (re->op()) { |
| 1022 | default: |
| 1023 | break; |
| 1024 | case kRegexpConcat: |
| 1025 | if (re->nsub() > 0) { |
| 1026 | sub = re->sub()[re->nsub() - 1]->Incref(); |
| 1027 | if (IsAnchorEnd(&sub, depth+1)) { |
| 1028 | PODArray<Regexp*> subcopy(re->nsub()); |
| 1029 | subcopy[re->nsub() - 1] = sub; // already have reference |
| 1030 | for (int i = 0; i < re->nsub() - 1; i++) |
| 1031 | subcopy[i] = re->sub()[i]->Incref(); |
| 1032 | *pre = Regexp::Concat(subcopy.data(), re->nsub(), re->parse_flags()); |
| 1033 | re->Decref(); |
| 1034 | return true; |
| 1035 | } |
| 1036 | sub->Decref(); |
| 1037 | } |
| 1038 | break; |
| 1039 | case kRegexpCapture: |
| 1040 | sub = re->sub()[0]->Incref(); |
| 1041 | if (IsAnchorEnd(&sub, depth+1)) { |
| 1042 | *pre = Regexp::Capture(sub, re->parse_flags(), re->cap()); |
| 1043 | re->Decref(); |
| 1044 | return true; |
| 1045 | } |
| 1046 | sub->Decref(); |
| 1047 | break; |
| 1048 | case kRegexpEndText: |
| 1049 | *pre = Regexp::LiteralString(NULL, 0, re->parse_flags()); |
| 1050 | re->Decref(); |
| 1051 | return true; |
| 1052 | } |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | void Compiler::Setup(Regexp::ParseFlags flags, int64_t max_mem, |
| 1057 | RE2::Anchor anchor) { |