From aeecd0993f4c689fa99920b7a7efd72742472970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=BCller?= Date: Sat, 8 Nov 2025 23:38:07 +0100 Subject: [PATCH 1/3] ``: Perform simplified stack unwinding for lookahead assertions when the pattern matches --- stl/inc/regex | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/stl/inc/regex b/stl/inc/regex index 350f11f33d..2d824f3875 100644 --- a/stl/inc/regex +++ b/stl/inc/regex @@ -3919,7 +3919,28 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N } case _N_end_assert: - _Next = nullptr; + { + for (;;) { + --_Frames_count; + const auto& _Frame = _Frames[_Frames_count]; + const auto _Code = _Frame._Code; + if (_Code == _Rx_unwind_ops::_After_assert || _Code == _Rx_unwind_ops::_After_neg_assert) { + _Tgt_state._Cur = _Frame._Match_state._Cur; + _Decrease_stack_usage_count(); + if (_Code == _Rx_unwind_ops::_After_assert) { + _Next = _Frame._Node->_Next; + } else { + _Failed = true; + } + break; + } else if (_Code == _Rx_unwind_ops::_Disjunction_eval_alt_on_failure + || _Code == _Rx_unwind_ops::_Disjunction_eval_alt_always + || _Code == _Rx_unwind_ops::_Loop_greedy || _Code == _Rx_unwind_ops::_Loop_nongreedy + || _Code == _Rx_unwind_ops::_Loop_restore_vals) { + _Decrease_stack_usage_count(); + } + } + } break; case _N_capture: @@ -4184,24 +4205,20 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N switch (_Frame._Code) { case _Rx_unwind_ops::_After_assert: - { // positive assert completed + { // matching pattern of positive assert failed + _STL_INTERNAL_CHECK(_Failed); _Decrease_stack_usage_count(); - if (!_Failed) { - _Tgt_state._Cur = _Frame._Match_state._Cur; - _Nx = _Frame._Node->_Next; - } break; } case _Rx_unwind_ops::_After_neg_assert: - { // negative assert completed + { // matching pattern of negative assert failed + _STL_INTERNAL_CHECK(_Failed); _Decrease_stack_usage_count(); - if (_Failed) { - const _Bt_state_t<_It>& _St = _Frame._Match_state; - _Tgt_state = _St; - _Nx = _Frame._Node->_Next; - } - _Failed = !_Failed; + const _Bt_state_t<_It>& _St = _Frame._Match_state; + _Tgt_state = _St; + _Nx = _Frame._Node->_Next; + _Failed = false; break; } From cb3c283d12794eb70dc8e40942cb0ff2d397c84e Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 10 Nov 2025 15:40:14 -0800 Subject: [PATCH 2/3] Remove unnecessary scope. --- stl/inc/regex | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/stl/inc/regex b/stl/inc/regex index 2d824f3875..973856a67e 100644 --- a/stl/inc/regex +++ b/stl/inc/regex @@ -3919,26 +3919,24 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N } case _N_end_assert: - { - for (;;) { - --_Frames_count; - const auto& _Frame = _Frames[_Frames_count]; - const auto _Code = _Frame._Code; - if (_Code == _Rx_unwind_ops::_After_assert || _Code == _Rx_unwind_ops::_After_neg_assert) { - _Tgt_state._Cur = _Frame._Match_state._Cur; - _Decrease_stack_usage_count(); - if (_Code == _Rx_unwind_ops::_After_assert) { - _Next = _Frame._Node->_Next; - } else { - _Failed = true; - } - break; - } else if (_Code == _Rx_unwind_ops::_Disjunction_eval_alt_on_failure - || _Code == _Rx_unwind_ops::_Disjunction_eval_alt_always - || _Code == _Rx_unwind_ops::_Loop_greedy || _Code == _Rx_unwind_ops::_Loop_nongreedy - || _Code == _Rx_unwind_ops::_Loop_restore_vals) { - _Decrease_stack_usage_count(); + for (;;) { + --_Frames_count; + const auto& _Frame = _Frames[_Frames_count]; + const auto _Code = _Frame._Code; + if (_Code == _Rx_unwind_ops::_After_assert || _Code == _Rx_unwind_ops::_After_neg_assert) { + _Tgt_state._Cur = _Frame._Match_state._Cur; + _Decrease_stack_usage_count(); + if (_Code == _Rx_unwind_ops::_After_assert) { + _Next = _Frame._Node->_Next; + } else { + _Failed = true; } + break; + } else if (_Code == _Rx_unwind_ops::_Disjunction_eval_alt_on_failure + || _Code == _Rx_unwind_ops::_Disjunction_eval_alt_always + || _Code == _Rx_unwind_ops::_Loop_greedy || _Code == _Rx_unwind_ops::_Loop_nongreedy + || _Code == _Rx_unwind_ops::_Loop_restore_vals) { + _Decrease_stack_usage_count(); } } break; From 685121bb60aa2f1195d87fdf341962ec352846e6 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 10 Nov 2025 15:45:27 -0800 Subject: [PATCH 3/3] Make wrapping prettier. --- stl/inc/regex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stl/inc/regex b/stl/inc/regex index 973856a67e..bb16a89813 100644 --- a/stl/inc/regex +++ b/stl/inc/regex @@ -3934,7 +3934,8 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N break; } else if (_Code == _Rx_unwind_ops::_Disjunction_eval_alt_on_failure || _Code == _Rx_unwind_ops::_Disjunction_eval_alt_always - || _Code == _Rx_unwind_ops::_Loop_greedy || _Code == _Rx_unwind_ops::_Loop_nongreedy + || _Code == _Rx_unwind_ops::_Loop_greedy // + || _Code == _Rx_unwind_ops::_Loop_nongreedy || _Code == _Rx_unwind_ops::_Loop_restore_vals) { _Decrease_stack_usage_count(); }