Skip to content

Commit d806de4

Browse files
<regex>: Perform simplified stack unwinding for lookahead assertions when the asserted pattern matches (#5835)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent e98de6d commit d806de4

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

stl/inc/regex

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3919,7 +3919,27 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N
39193919
}
39203920

39213921
case _N_end_assert:
3922-
_Next = nullptr;
3922+
for (;;) {
3923+
--_Frames_count;
3924+
const auto& _Frame = _Frames[_Frames_count];
3925+
const auto _Code = _Frame._Code;
3926+
if (_Code == _Rx_unwind_ops::_After_assert || _Code == _Rx_unwind_ops::_After_neg_assert) {
3927+
_Tgt_state._Cur = _Frame._Match_state._Cur;
3928+
_Decrease_stack_usage_count();
3929+
if (_Code == _Rx_unwind_ops::_After_assert) {
3930+
_Next = _Frame._Node->_Next;
3931+
} else {
3932+
_Failed = true;
3933+
}
3934+
break;
3935+
} else if (_Code == _Rx_unwind_ops::_Disjunction_eval_alt_on_failure
3936+
|| _Code == _Rx_unwind_ops::_Disjunction_eval_alt_always
3937+
|| _Code == _Rx_unwind_ops::_Loop_greedy //
3938+
|| _Code == _Rx_unwind_ops::_Loop_nongreedy
3939+
|| _Code == _Rx_unwind_ops::_Loop_restore_vals) {
3940+
_Decrease_stack_usage_count();
3941+
}
3942+
}
39233943
break;
39243944

39253945
case _N_capture:
@@ -4184,24 +4204,20 @@ bool _Matcher3<_BidIt, _Elem, _RxTraits, _It, _Alloc>::_Match_pat(_Node_base* _N
41844204

41854205
switch (_Frame._Code) {
41864206
case _Rx_unwind_ops::_After_assert:
4187-
{ // positive assert completed
4207+
{ // matching pattern of positive assert failed
4208+
_STL_INTERNAL_CHECK(_Failed);
41884209
_Decrease_stack_usage_count();
4189-
if (!_Failed) {
4190-
_Tgt_state._Cur = _Frame._Match_state._Cur;
4191-
_Nx = _Frame._Node->_Next;
4192-
}
41934210
break;
41944211
}
41954212

41964213
case _Rx_unwind_ops::_After_neg_assert:
4197-
{ // negative assert completed
4214+
{ // matching pattern of negative assert failed
4215+
_STL_INTERNAL_CHECK(_Failed);
41984216
_Decrease_stack_usage_count();
4199-
if (_Failed) {
4200-
const _Bt_state_t<_It>& _St = _Frame._Match_state;
4201-
_Tgt_state = _St;
4202-
_Nx = _Frame._Node->_Next;
4203-
}
4204-
_Failed = !_Failed;
4217+
const _Bt_state_t<_It>& _St = _Frame._Match_state;
4218+
_Tgt_state = _St;
4219+
_Nx = _Frame._Node->_Next;
4220+
_Failed = false;
42054221
break;
42064222
}
42074223

0 commit comments

Comments
 (0)