From 328b0d8afc88050ddcbe73677d632c4f39dfacfb Mon Sep 17 00:00:00 2001 From: miloyip Date: Mon, 25 May 2015 19:49:07 +0800 Subject: [PATCH] Minor refactor regex --- include/rapidjson/internal/regex.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 7ee99b6..dace328 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -152,8 +152,7 @@ private: } void Patch(SizeType l, SizeType s) { - SizeType next; - for (; l != kRegexInvalidState; l = next) { + for (SizeType next; l != kRegexInvalidState; l = next) { next = GetState(l).out; GetState(l).out = s; } @@ -173,7 +172,7 @@ private: switch (codepoint) { case '|': while (!operatorStack.Empty() && *operatorStack.template Top() < kAlternation) - if (!Eval(operandStack, operatorStack)) + if (!Eval(operandStack, *operatorStack.template Pop(1))) return; *operatorStack.template Push() = kAlternation; *atomCountStack.template Top() = 0; @@ -186,7 +185,7 @@ private: case ')': while (!operatorStack.Empty() && *operatorStack.template Top() != kLeftParenthesis) - if (!Eval(operandStack, operatorStack)) + if (!Eval(operandStack, *operatorStack.template Pop(1))) return; if (operatorStack.Empty()) return; @@ -196,20 +195,17 @@ private: break; case '?': - *operatorStack.template Push() = kZeroOrOne; - if (!Eval(operandStack, operatorStack)) + if (!Eval(operandStack, kZeroOrOne)) return; break; case '*': - *operatorStack.template Push() = kZeroOrMore; - if (!Eval(operandStack, operatorStack)) + if (!Eval(operandStack, kZeroOrMore)) return; break; case '+': - *operatorStack.template Push() = kOneOrMore; - if (!Eval(operandStack, operatorStack)) + if (!Eval(operandStack, kOneOrMore)) return; break; @@ -221,7 +217,7 @@ private: } while (!operatorStack.Empty()) - if (!Eval(operandStack, operatorStack)) + if (!Eval(operandStack, *operatorStack.template Pop(1))) return; // Link the operand to matching state. @@ -229,6 +225,7 @@ private: Frag* e = operandStack.template Pop(1); Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0)); root_ = e->start; + #if RAPIDJSON_REGEX_VERBOSE printf("root: %d\n", root_); for (SizeType i = 0; i < stateCount_ ; i++) { @@ -240,9 +237,8 @@ private: } } - bool Eval(Stack& operandStack, Stack& operatorStack) { - // printf("Eval %c\n", "?*+.|("[*operatorStack.template Top()]); - switch (*operatorStack.template Pop(1)) { + bool Eval(Stack& operandStack, Operator op) { + switch (op) { case kConcatenation: if (operandStack.GetSize() >= sizeof(Frag) * 2) { Frag e2 = *operandStack.template Pop(1);