Is this regexp required to start at the beginning of the text? Only approximate; can return false for complicated regexps like (\Aa|\Ab), but handles (\A(a|b)). Could use the Walker to write a more exact one.
| 963 | // Only approximate; can return false for complicated regexps like (\Aa|\Ab), |
| 964 | // but handles (\A(a|b)). Could use the Walker to write a more exact one. |
| 965 | static bool IsAnchorStart(Regexp** pre, int depth) { |
| 966 | Regexp* re = *pre; |
| 967 | Regexp* sub; |
| 968 | // The depth limit makes sure that we don't overflow |
| 969 | // the stack on a deeply nested regexp. As the comment |
| 970 | // above says, IsAnchorStart is conservative, so returning |
| 971 | // a false negative is okay. The exact limit is somewhat arbitrary. |
| 972 | if (re == NULL || depth >= 4) |
| 973 | return false; |
| 974 | switch (re->op()) { |
| 975 | default: |
| 976 | break; |
| 977 | case kRegexpConcat: |
| 978 | if (re->nsub() > 0) { |
| 979 | sub = re->sub()[0]->Incref(); |
| 980 | if (IsAnchorStart(&sub, depth+1)) { |
| 981 | PODArray<Regexp*> subcopy(re->nsub()); |
| 982 | subcopy[0] = sub; // already have reference |
| 983 | for (int i = 1; i < re->nsub(); i++) |
| 984 | subcopy[i] = re->sub()[i]->Incref(); |
| 985 | *pre = Regexp::Concat(subcopy.data(), re->nsub(), re->parse_flags()); |
| 986 | re->Decref(); |
| 987 | return true; |
| 988 | } |
| 989 | sub->Decref(); |
| 990 | } |
| 991 | break; |
| 992 | case kRegexpCapture: |
| 993 | sub = re->sub()[0]->Incref(); |
| 994 | if (IsAnchorStart(&sub, depth+1)) { |
| 995 | *pre = Regexp::Capture(sub, re->parse_flags(), re->cap()); |
| 996 | re->Decref(); |
| 997 | return true; |
| 998 | } |
| 999 | sub->Decref(); |
| 1000 | break; |
| 1001 | case kRegexpBeginText: |
| 1002 | *pre = Regexp::LiteralString(NULL, 0, re->parse_flags()); |
| 1003 | re->Decref(); |
| 1004 | return true; |
| 1005 | } |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | // Is this regexp required to start at the end of the text? |
| 1010 | // Only approximate; can return false for complicated regexps like (a\z|b\z), |