-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix better_match check used by regex search routine #1547
Changes from 4 commits
f45ed70
e7bec21
af26bd4
7cf7f37
de9c799
2f37a47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
#pragma once | ||
#include <cstdio> | ||
#include <numeric> | ||
#include <regex> | ||
#include <string> | ||
|
||
|
@@ -116,6 +117,56 @@ class regex_fixture { | |
} | ||
} | ||
|
||
void should_capture_groups( | ||
const std::string& subject, const std::string& pattern, const std::vector<std::string>& groups) { | ||
try { | ||
const std::regex r(pattern); | ||
std::smatch m; | ||
|
||
if (!std::regex_match(subject, m, r)) { | ||
printf(R"(Expected regex("%s") to match "%s".)" | ||
"\n", | ||
pattern.c_str(), subject.c_str()); | ||
fail_regex(); | ||
return; | ||
} | ||
|
||
if (m[0] != subject) { | ||
printf(R"(should_capture("%s", "%s"): m[0] == "%s")" | ||
"\n", | ||
subject.c_str(), pattern.c_str(), m[0].str().c_str()); | ||
fail_regex(); | ||
} | ||
|
||
if (m.size() != groups.size() + 1) { | ||
printf(R"(should_capture("%s", "%s"): captures: %zd != %zd)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"\n", | ||
subject.c_str(), pattern.c_str(), m.size() - 1, groups.size()); | ||
fail_regex(); | ||
} | ||
|
||
for (size_t i = 0; i < groups.size(); ++i) { | ||
if (m[i + 1] != groups[i]) { | ||
printf(R"(should_capture("%s", "%s"): index %zd)" | ||
"\nexpected: \"%s\"" | ||
"\nfound: \"%s\"\n", | ||
subject.c_str(), pattern.c_str(), i, m[i + 1].str().c_str(), groups[i].c_str()); | ||
fail_regex(); | ||
} | ||
} | ||
} catch (const std::regex_error& e) { | ||
std::string groups_string = std::accumulate(groups.cbegin(), groups.cend(), std::string(), | ||
[](const std::string& result, const std::string& value) -> std::string { | ||
return !result.empty() ? result + ";" + value : value; | ||
}); | ||
|
||
printf(R"(should_capture("%s", "%s", "%s"): regex_error: "%s")" | ||
"\n", | ||
subject.c_str(), pattern.c_str(), groups_string.c_str(), e.what()); | ||
fail_regex(); | ||
} | ||
} | ||
|
||
void should_replace_to(const std::string& subject, const std::string& pattern, const std::string& fmt, | ||
const std::regex_constants::match_flag_type match_flags, const std::string& expected) { | ||
try { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -546,6 +546,14 @@ void test_VSO_226914_word_boundaries() { | |
aWordAny.should_search_fail("aa", match_not_bow | match_not_eow); | ||
} | ||
|
||
void test_gh_731() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be followed by a comment mentioning the bug's title (like |
||
std::vector<std::string> groups{"AAA", "BBB", ""}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is already There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably include |
||
g_regexTester.should_capture_groups("AAA BBB", R"((A+)\s*(B+)?\s*B*(B*)?)", groups); | ||
|
||
groups.assign({"AAA", "BBB"}); | ||
g_regexTester.should_capture_groups("AAA BBB", R"((A+)\s*(B+)?\s*B*)", groups); | ||
} | ||
|
||
void test_gh_993() { | ||
// GH-993 regex::icase is not handled correctly for some input. | ||
{ | ||
|
@@ -607,6 +615,7 @@ int main() { | |
test_VSO_225160_match_bol_flag(); | ||
test_VSO_225160_match_eol_flag(); | ||
test_VSO_226914_word_boundaries(); | ||
test_gh_731(); | ||
test_gh_993(); | ||
|
||
return g_regexTester.result(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We conventionally add newlines between "non-chained"
if
-statements.