468885 {
468886
468887 RegexpStatus xstatus;
468888 if (status == NULL)
468889 status = &xstatus;
468890
468892 StringPiece t = s;
468893
468894
468895 if (global_flags & Latin1) {
468896 std::string* tmp = new std::string;
468897 ConvertLatin1ToUTF8(t, tmp);
468898 status->set_tmp(tmp);
468899 t = *tmp;
468900 }
468901
468902 if (global_flags & Literal) {
468903
468904 while (!t.empty()) {
468905 Rune r;
468906 if (StringPieceToRune(&r, &t, status) < 0)
468907 return NULL;
468908 if (!ps.PushLiteral(r))
468909 return NULL;
468910 }
468911 return ps.DoFinish();
468912 }
468913
468914 StringPiece lastunary = StringPiece();
468915 while (!t.empty()) {
468916 StringPiece isunary = StringPiece();
468917 switch (t[0]) {
468918 default: {
468919 Rune r;
468920 if (StringPieceToRune(&r, &t, status) < 0)
468921 return NULL;
468922 if (!ps.PushLiteral(r))
468923 return NULL;
468924 break;
468925 }
468926
468927 case '(':
468928
468929 if ((ps.flags() & PerlX) && (t.size() >= 2 && t[1] == '?')) {
468930
468931 if (!ps.ParsePerlFlags(&t))
468932 return NULL;
468933 break;
468934 }
468935 if (ps.flags() & NeverCapture) {
468936 if (!ps.DoLeftParenNoCapture())
468937 return NULL;
468938 } else {
468939 if (!ps.DoLeftParen(StringPiece()))
468940 return NULL;
468941 }
468942 t.remove_prefix(1);
468943 break;
468944
468945 case '|':
468946 if (!ps.DoVerticalBar())
468947 return NULL;
468948 t.remove_prefix(1);
468949 break;
468950
468951 case ')':
468952 if (!ps.DoRightParen())
468953 return NULL;
468954 t.remove_prefix(1);
468955 break;
468956
468957 case '^':
468958 if (!ps.PushCaret())
468959 return NULL;
468960 t.remove_prefix(1);
468961 break;
468962
468963 case '$':
468964 if (!ps.PushDollar())
468965 return NULL;
468966 t.remove_prefix(1);
468967 break;
468968
468969 case '.':
468970 if (!ps.PushDot())
468971 return NULL;
468972 t.remove_prefix(1);
468973 break;
468974
468975 case '[': {
468976 Regexp* re;
468977 if (!ps.ParseCharClass(&t, &re, status))
468978 return NULL;
468979 if (!ps.PushRegexp(re))
468980 return NULL;
468981 break;
468982 }
468983
468984 case '*': {
468985 RegexpOp op;
468986 op = kRegexpStar;
468987 goto Rep;
468988 case '+':
468989 op = kRegexpPlus;
468990 goto Rep;
468991 case '?':
468992 op = kRegexpQuest;
468993 goto Rep;
468994 Rep:
468995 StringPiece opstr = t;
468996 bool nongreedy = false;
468997 t.remove_prefix(1);
468998 if (ps.flags() & PerlX) {
468999 if (!t.empty() && t[0] == '?') {
469000 nongreedy = true;
469001 t.remove_prefix(1);
469002 }
469003 if (!lastunary.empty()) {
469004
469005
469006
469007 status->set_code(kRegexpRepeatOp);
469008 status->set_error_arg(StringPiece(
469009 lastunary.data(),
469010 static_cast<size_t>(t.data() - lastunary.data())));
469011 return NULL;
469012 }
469013 }
469014 opstr = StringPiece(opstr.data(),
469015 static_cast<size_t>(t.data() - opstr.data()));
469016 if (!ps.PushRepeatOp(op, opstr, nongreedy))
469017 return NULL;
469018 isunary = opstr;
469019 break;
469020 }
469021
469022 case '{': {
469023 int lo, hi;
469024 StringPiece opstr = t;
469025 if (!MaybeParseRepetition(&t, &lo, &hi)) {
469026
469027 if (!ps.PushLiteral('{'))
469028 return NULL;
469029 t.remove_prefix(1);
469030 break;
469031 }
469032 bool nongreedy = false;
469033 if (ps.flags() & PerlX) {
469034 if (!t.empty() && t[0] == '?') {
469035 nongreedy = true;
469036 t.remove_prefix(1);
469037 }
469038 if (!lastunary.empty()) {
469039
469040 status->set_code(kRegexpRepeatOp);
469041 status->set_error_arg(StringPiece(
469042 lastunary.data(),
469043 static_cast<size_t>(t.data() - lastunary.data())));
469044 return NULL;
469045 }
469046 }
469047 opstr = StringPiece(opstr.data(),
469048 static_cast<size_t>(t.data() - opstr.data()));
469049 if (!ps.PushRepetition(lo, hi, opstr, nongreedy))
469050 return NULL;
469051 isunary = opstr;
469052 break;
469053 }
469054
469055 case '\\': {
469056
469057 if ((ps.flags() & Regexp::PerlB) &&
469058 t.size() >= 2 && (t[1] == 'b' || t[1] == 'B')) {
469059 if (!ps.PushWordBoundary(t[1] == 'b'))
469060 return NULL;
469061 t.remove_prefix(2);
469062 break;
469063 }
469064
469065 if ((ps.flags() & Regexp::PerlX) && t.size() >= 2) {
469066 if (t[1] == 'A') {
469067 if (!ps.PushSimpleOp(kRegexpBeginText))
469068 return NULL;
469069 t.remove_prefix(2);
469070 break;
469071 }
469072 if (t[1] == 'z') {
469073 if (!ps.PushSimpleOp(kRegexpEndText))
469074 return NULL;
469075 t.remove_prefix(2);
469076 break;
469077 }
469078
469079
469080
469081
469082
469083 if (t[1] == 'C') {
469084 if (!ps.PushSimpleOp(kRegexpAnyByte))
469085 return NULL;
469086 t.remove_prefix(2);
469087 break;
469088 }
469089
469090 if (t[1] == 'Q') {
469091 t.remove_prefix(2);
469092 while (!t.empty()) {
469093 if (t.size() >= 2 && t[0] == '\\' && t[1] == 'E') {
469094 t.remove_prefix(2);
469095 break;
469096 }
469097 Rune r;
469098 if (StringPieceToRune(&r, &t, status) < 0)
469099 return NULL;
469100 if (!ps.PushLiteral(r))
469101 return NULL;
469102 }
469103 break;
469104 }
469105 }
469106
469107 if (t.size() >= 2 && (t[1] == 'p' || t[1] == 'P')) {
469108 Regexp* re = new Regexp(kRegexpCharClass, ps.flags() & ~FoldCase);
469109 re->arguments.char_class.ccb_ = new CharClassBuilder;
469110 switch (ParseUnicodeGroup(&t, ps.flags(), re->arguments.char_class.ccb_, status)) {
469111 case kParseOk:
469112 if (!ps.PushRegexp(re))
469113 return NULL;
469114 goto Break2;
469115 case kParseError:
469116 re->Decref();
469117 return NULL;
469118 case kParseNothing:
469119 re->Decref();
469120 break;
469121 }
469122 }
469123
469124 const UGroup *g = MaybeParsePerlCCEscape(&t, ps.flags());
469125 if (g != NULL) {
469126 Regexp* re = new Regexp(kRegexpCharClass, ps.flags() & ~FoldCase);
469127 re->arguments.char_class.ccb_ = new CharClassBuilder;
469128 AddUGroup(re->arguments.char_class.ccb_, g, g->sign, ps.flags());
469129 if (!ps.PushRegexp(re))
469130 return NULL;
469131 break;
469132 }
469133
469134 Rune r;
469135 if (!ParseEscape(&t, &r, status, ps.rune_max()))
469136 return NULL;
469137 if (!ps.PushLiteral(r))
469138 return NULL;
469139 break;
469140 }
469141 }
469142 Break2:
469143 lastunary = isunary;
469144 }
469145 return ps.DoFinish();
469146}
Definition duckdb.cpp:466738