From 9336193aaf3f2f8df209a4204c4f60c71d2460a0 Mon Sep 17 00:00:00 2001 From: Jason Orendorff Date: Sat, 26 Oct 2013 23:52:54 -0700 Subject: [PATCH] Allow RegEx matches of length 0. This seems more correct to me. (However, this is not super important in practice. Grammar files should not contain regular expressions that could match the empty string; if such a RegEx matched 0 characters once, it would match again and again forever, since it wouldn't consume any input.) --- src/RegEx.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/RegEx.cpp b/src/RegEx.cpp index 7dd309f..88f4f91 100644 --- a/src/RegEx.cpp +++ b/src/RegEx.cpp @@ -233,12 +233,13 @@ RegEx::~RegEx() { } int RegEx::longMatch(std::string stringToMatch) { - //If the beginning character is wrong, exit immediantly. Otherwise, get all the states we can get from adding the second character to the state where we accepted the first + // Start in the begin state (only). int lastMatch = -1; - currentStates = *(begin->advance(stringToMatch[0])); + currentStates.clear(); + currentStates.push_back(begin); std::vector nextStates; - for (int i = 1; i < stringToMatch.size(); i++) { + for (int i = 0; i < stringToMatch.size(); i++) { //Go through every current state. Check to see if it is goal, if so update last goal. //Also, add each state's advance to nextStates for (std::vector::size_type j = 0; j < currentStates.size(); j++) { @@ -292,6 +293,7 @@ void RegEx::test() { { RegEx re("a*"); assert(re.longMatch("aa") == 2); + assert(re.longMatch("b") == 0); } std::cout << "RegEx tests pass\n";