Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions re2/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ bool Regexp::ParseState::IsMarker(RegexpOp op) {
// Processes a left parenthesis in the input.
// Pushes a marker onto the stack.
bool Regexp::ParseState::DoLeftParen(absl::string_view name) {
if (flags_ & NeverCapture)
return DoLeftParenNoCapture();
Regexp* re = new Regexp(kLeftParen, flags_);
re->cap_ = ++ncap_;
if (name.data() != NULL)
Expand Down Expand Up @@ -2313,13 +2315,8 @@ Regexp* Regexp::Parse(absl::string_view s, ParseFlags global_flags,
return NULL;
break;
}
if (ps.flags() & NeverCapture) {
if (!ps.DoLeftParenNoCapture())
return NULL;
} else {
if (!ps.DoLeftParen(absl::string_view()))
return NULL;
}
if (!ps.DoLeftParen(absl::string_view()))
return NULL;
t.remove_prefix(1); // '('
break;

Expand Down
19 changes: 19 additions & 0 deletions re2/testing/re2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,25 @@ TEST(RE2, NeverCapture) {
EXPECT_EQ(0, re.NumberOfCapturingGroups());
}

TEST(RE2, NeverCaptureNamedGroups) {
RE2::Options opt;
opt.set_never_capture(true);
for (const char* pattern : {"(?P<first>r)(e)", "(?<first>r)(e)"}) {
RE2 re(pattern, opt);
ASSERT_TRUE(re.ok());
EXPECT_TRUE(RE2::FullMatch("re", re));
EXPECT_EQ(0, re.NumberOfCapturingGroups());
EXPECT_TRUE(re.NamedCapturingGroups().empty());
EXPECT_TRUE(re.CapturingGroupNames().empty());

RE2 capturing(pattern);
EXPECT_EQ(2, capturing.NumberOfCapturingGroups());
EXPECT_EQ(1, capturing.NamedCapturingGroups().at("first"));
}
opt.set_log_errors(false);
EXPECT_EQ(RE2::ErrorBadNamedCapture, RE2("(?P<a-b>r)", opt).error_code());
}

// Bitstate bug was looking at submatch[0] even if nsubmatch == 0.
// Triggered by a failed DFA search falling back to Bitstate when
// using Match with a NULL submatch set. Bitstate tried to read
Expand Down