Skip to content

Commit e7b8f64

Browse files
committed
Fix regression on optional regex submatches
This regression was introduced in commit a7ff906 (Regex: use iterators for match results).
1 parent ee740d2 commit e7b8f64

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Regex.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ FindRegexMatch::FindRegexMatch(const boost::regex &regexImpl, const std::string
3737
if (i->matched) {
3838
RegexSubmatch s = {*i, i->first - expression.begin()};
3939
submatches.push_back(s);
40+
} else {
41+
submatches.push_back(RegexSubmatch());
4042
}
4143
}
4244
}

tests/unit/RegexTest.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ TEST(RegexTest, matchesRegexWithSubmatches) {
4646
EXPECT_EQ("69", match->getSubmatches()[1].value);
4747
}
4848

49+
TEST(RegexTest, matchesRegexWithOptionalSubmatches) {
50+
Regex sum("^(\\d+)\\+(\\d+)(?:\\+(\\d+))?=(\\d+)$");
51+
52+
shared_ptr<RegexMatch> match(sum.find("1+2+3=6"));
53+
EXPECT_TRUE(match->matches());
54+
ASSERT_EQ(4, match->getSubmatches().size());
55+
56+
match = shared_ptr<RegexMatch>(sum.find("42+27=69"));
57+
EXPECT_TRUE(match->matches());
58+
ASSERT_EQ(4, match->getSubmatches().size());
59+
EXPECT_EQ("42", match->getSubmatches()[0].value);
60+
EXPECT_EQ("27", match->getSubmatches()[1].value);
61+
EXPECT_EQ("", match->getSubmatches()[2].value);
62+
EXPECT_EQ("69", match->getSubmatches()[3].value);
63+
}
64+
4965
TEST(RegexTest, findAllDoesNotMatchIfNoTokens) {
5066
Regex sum("([^,]+)(?:,|$)");
5167
shared_ptr<RegexMatch> match(sum.findAll(""));

0 commit comments

Comments
 (0)