Skip to content

<regex>: Avoid generating group nodes for non-capturing groups#6359

Open
muellerj3 wants to merge 2 commits into
microsoft:mainfrom
muellerj2:regex-avoid-generating-nodes-for-noncapturing-groups
Open

<regex>: Avoid generating group nodes for non-capturing groups#6359
muellerj3 wants to merge 2 commits into
microsoft:mainfrom
muellerj2:regex-avoid-generating-nodes-for-noncapturing-groups

Conversation

@muellerj3

Copy link
Copy Markdown
Contributor

Towards #5962. Also helps a bit with #6347.

This completes the removal of _N_group and _N_end_group node generation from the parser (but have to continue handling such nodes in the matcher for ABI reasons). As a side effect, this also avoids generating any nodes for constructs like (?:)*.

This is achieved by marking the position of a node just before a non-capturing or capturing group and using this mark to generate the repeat nodes around such a group if the group is followed by a quantifier. We store this mark in _Group_start. If _Group_start is null, we know that the quantifier doesn't apply to a group, so the repeat nodes have to surround the node generated last only.

_Group_start is reset after handling the quantifier, so it can't accidentally affect the parsing of following alternatives. We also have to assign the mark to _Group_start after the group has been fully parsed, otherwise the mark might be accidentally used in or reset by the recursive _Alternative() call.

The code that handles a quantifier following a non-group already handles the generation of _N_str nodes correctly. We have to handle this for a repeated group now as well: _Builder3::_Chars might not be empty for a non-capturing group because we do not generate an _N_end_group node anymore that triggers the generation of the _N_str node. This means that _Emit_str_node() must be called first in _Add_group_rep().

@muellerj3 muellerj3 requested a review from a team as a code owner July 12, 2026 13:58
Copilot AI review requested due to automatic review settings July 12, 2026 13:58
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Jul 12, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Removes parser-generated no-op nodes for non-capturing regex groups while preserving ABI handling.

Changes:

  • Tracks group boundaries to generate repetition nodes correctly.
  • Splits grouped and non-grouped repetition handling.
  • Adds regression coverage for grouped quantifiers and captures.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
stl/inc/regex Removes non-capturing group nodes and revises repetition generation.
tests/std/tests/VSO_0000000_regex_use/test.cpp Adds grouped-quantifier regression tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread stl/inc/regex Outdated
template <class _FwdIt, class _Elem, class _RxTraits>
void _Builder3<_FwdIt, _Elem, _RxTraits>::_Generate_rep(_Node_base* _First_node, int _Min, int _Max, bool _Greedy) {
// generates a rep around the range from given start node to current node
if (_Min == 0 && _Max == 1 && _First_node == _Current) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good catch of a very subtle problem (even if the mishandled pattern seems very unlikely to appear in practice).

But I think this only affects positive assertions, because capture groups in negative assertions are never matched after the assertion has been processed.

Copilot AI review requested due to automatic review settings July 12, 2026 14:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@StephanTLavavej StephanTLavavej added performance Must go faster regex meow is a substring of homeowner labels Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Must go faster regex meow is a substring of homeowner

Projects

Status: Initial Review

Development

Successfully merging this pull request may close these issues.

3 participants