Skip to content

Commit

Permalink
Improve coverage of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
miloyip committed Apr 16, 2016
1 parent c71825f commit ecd8fa3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 5 additions & 7 deletions include/rapidjson/internal/regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ class GenericRegex {
bool Eval(Stack<Allocator>& operandStack, Operator op) {
switch (op) {
case kConcatenation:
if (operandStack.GetSize() >= sizeof(Frag) * 2) {
RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2);
{
Frag e2 = *operandStack.template Pop<Frag>(1);
Frag e1 = *operandStack.template Pop<Frag>(1);
Patch(e1.out, e2.start);
*operandStack.template Push<Frag>() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex));
return true;
}
return false;
return true;

case kAlternation:
if (operandStack.GetSize() >= sizeof(Frag) * 2) {
Expand Down Expand Up @@ -430,8 +430,7 @@ class GenericRegex {

bool EvalQuantifier(Stack<Allocator>& operandStack, unsigned n, unsigned m) {
RAPIDJSON_ASSERT(n <= m);
if (operandStack.GetSize() < sizeof(Frag))
return false;
RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag));

if (n == 0) {
if (m == 0) // a{0} not support
Expand Down Expand Up @@ -647,8 +646,7 @@ class GenericRegex {

// Return whether the added states is a match state
bool AddState(Stack<Allocator>& l, SizeType index) const {
if (index == kRegexInvalidState)
return true;
RAPIDJSON_ASSERT(index != kRegexInvalidState);

const State& s = GetState(index);
if (s.out1 != kRegexInvalidState) { // Split
Expand Down
11 changes: 11 additions & 0 deletions test/unittest/regextest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@

using namespace rapidjson::internal;

TEST(Regex, Single) {
Regex re("a");
ASSERT_TRUE(re.IsValid());
EXPECT_TRUE(re.Match("a"));
EXPECT_FALSE(re.Match(""));
EXPECT_FALSE(re.Match("b"));
}

TEST(Regex, Concatenation) {
Regex re("abc");
ASSERT_TRUE(re.IsValid());
Expand Down Expand Up @@ -560,6 +568,9 @@ TEST(Regex, Invalid) {
TEST_INVALID("a{1,0}");
TEST_INVALID("a{-1,0}");
TEST_INVALID("a{-1,1}");
TEST_INVALID("a{4294967296}"); // overflow of unsigned
TEST_INVALID("a{1a}");
TEST_INVALID("[");
TEST_INVALID("[]");
TEST_INVALID("[^]");
TEST_INVALID("[\\a]");
Expand Down

0 comments on commit ecd8fa3

Please sign in to comment.