Skip to content
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

<regex>: Revise caret parsing in basic and grep mode #5165

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions stl/inc/regex
Original file line number Diff line number Diff line change
Expand Up @@ -1699,8 +1699,8 @@ private:
void _Error(regex_constants::error_type);

bool _Is_esc() const;
void _Trans();
void _Next();
void _Trans(bool _Beg_char_class);
void _Next(bool _Beg_char_class = false);
void _Expect(_Meta_type, regex_constants::error_type);

// parsing
Expand Down Expand Up @@ -3823,7 +3823,7 @@ bool _Parser<_FwdIt, _Elem, _RxTraits>::_Is_esc() const { // assumes _Pat != _En
}

template <class _FwdIt, class _Elem, class _RxTraits>
void _Parser<_FwdIt, _Elem, _RxTraits>::_Trans() { // map character to meta-character
void _Parser<_FwdIt, _Elem, _RxTraits>::_Trans(const bool _Beg_char_class) { // map character to meta-character
static constexpr char _Meta_map[] = {_Meta_lpar, _Meta_rpar, _Meta_dlr, _Meta_caret, _Meta_dot, _Meta_star,
_Meta_plus, _Meta_query, _Meta_lsq, _Meta_rsq, _Meta_bar, _Meta_esc, _Meta_dash, _Meta_lbr, _Meta_rbr,
_Meta_comma, _Meta_colon, _Meta_equal, _Meta_exc, _Meta_nl, _Meta_cr, _Meta_bsp, 0}; // array of meta chars
Expand Down Expand Up @@ -3874,7 +3874,7 @@ void _Parser<_FwdIt, _Elem, _RxTraits>::_Trans() { // map character to meta-char
break;

case _Meta_caret:
if ((_L_flags & _L_anch_rstr) && !_Nfa._Beg_expr()) {
if (!_Beg_char_class && (_L_flags & _L_anch_rstr) && !_Nfa._Beg_expr()) {
_Mchar = _Meta_chr;
}
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -3911,15 +3911,16 @@ void _Parser<_FwdIt, _Elem, _RxTraits>::_Trans() { // map character to meta-char
}

template <class _FwdIt, class _Elem, class _RxTraits>
void _Parser<_FwdIt, _Elem, _RxTraits>::_Next() { // advance to next input character
void _Parser<_FwdIt, _Elem, _RxTraits>::_Next(
const bool _Beg_char_class /* = false */) { // advance to next input character
if (_Pat != _End) { // advance
if (*_Pat == _Meta_esc && _Is_esc()) {
++_Pat;
}

++_Pat;
}
_Trans();
_Trans(_Beg_char_class);
}

template <class _FwdIt, class _Elem, class _RxTraits>
Expand Down Expand Up @@ -4428,7 +4429,7 @@ bool _Parser<_FwdIt, _Elem, _RxTraits>::_Alternative() { // check for valid alte
_AtomEscape();
}
} else if (_Mchar == _Meta_lsq) { // add bracket expression
_Next();
_Next(true);
_CharacterClass();
_Expect(_Meta_rsq, regex_constants::error_brack);
} else if (_Mchar == _Meta_lpar) { // check for valid group
Expand Down Expand Up @@ -4614,7 +4615,7 @@ _Parser<_FwdIt, _Elem, _RxTraits>::_Parser(
_Nfa._Setlong();
}

_Trans();
_Trans(false);
}

#if _HAS_TR1_NAMESPACE
Expand Down
13 changes: 13 additions & 0 deletions tests/std/tests/VSO_0000000_regex_use/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,18 @@ void test_gh_5160() {
neg_regex.should_search_fail(L"xxxYxx\x2009xxxZxxx"); // U+2009 THIN SPACE
}

void test_gh_5165() {
// GH-5165: circumflex ^ should negate character classes in basic regular expressions
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
g_regexTester.should_match("yz", "y[^x]", basic);
g_regexTester.should_match("yz", "y[^x]", grep);
g_regexTester.should_match("y^", "y[^x]", basic);
g_regexTester.should_match("y^", "y[^x]", grep);
g_regexTester.should_not_match("yx", "y[^x]", basic);
g_regexTester.should_not_match("yx", "y[^x]", grep);
g_regexTester.should_not_match("y^", "y[^x^]", basic);
g_regexTester.should_not_match("y^", "y[^x^]", grep);
}

int main() {
test_dev10_449367_case_insensitivity_should_work();
test_dev11_462743_regex_collate_should_not_disable_regex_icase();
Expand Down Expand Up @@ -699,6 +711,7 @@ int main() {
test_gh_4995();
test_gh_5058();
test_gh_5160();
test_gh_5165();

return g_regexTester.result();
}