From 60c176fb459c7780f9cb711e2427e41dca12a54a Mon Sep 17 00:00:00 2001 From: Tim Shen Date: Mon, 19 Jan 2015 22:56:04 +0000 Subject: re PR libstdc++/64584 (basic_regex::assign breaks *this if it throws regex_error) PR libstdc++/64584 PR libstdc++/64585 * include/bits/regex.h (basic_regex<>::basic_regex, basic_regex<>::assign, basic_regex<>::imbue, basic_regex<>::swap, basic_regex<>::mark_count): Drop NFA after imbuing basic_regex; Make assign() transactional against exception. * include/bits/regex_compiler.h (__compile_nfa<>): Add back __compile_nfa SFINAE. * include/std/regex: Adjust include order to avoid __compile_nfa forward declaration. * testsuite/28_regex/basic_regex/assign/char/string.cc: New testcase. * testsuite/28_regex/basic_regex/imbue/string.cc: New testcase. From-SVN: r219865 --- libstdc++-v3/include/bits/regex.h | 52 +++++++++++++++------------------------ 1 file changed, 20 insertions(+), 32 deletions(-) (limited to 'libstdc++-v3/include/bits/regex.h') diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index 52c2384..6de883a 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -62,13 +62,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template class _Executor; - template - inline std::shared_ptr<_NFA<_TraitsT>> - __compile_nfa(const typename _TraitsT::char_type* __first, - const typename _TraitsT::char_type* __last, - const typename _TraitsT::locale_type& __loc, - regex_constants::syntax_option_type __flags); - _GLIBCXX_END_NAMESPACE_VERSION } @@ -433,7 +426,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * character sequence. */ basic_regex() - : _M_flags(ECMAScript), _M_loc(), _M_original_str(), _M_automaton(nullptr) + : _M_flags(ECMAScript), _M_loc(), _M_automaton(nullptr) { } /** @@ -497,7 +490,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 basic_regex(const std::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s, flag_type __f = ECMAScript) - : basic_regex(__s.begin(), __s.end(), __f) + : basic_regex(__s.data(), __s.data() + __s.size(), __f) { } /** @@ -516,14 +509,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 template basic_regex(_FwdIter __first, _FwdIter __last, flag_type __f = ECMAScript) - : _M_flags(__f), - _M_loc(), - _M_original_str(__first, __last), - _M_automaton(__detail::__compile_nfa<_Rx_traits>( - _M_original_str.c_str(), - _M_original_str.c_str() + _M_original_str.size(), - _M_loc, - _M_flags)) + : basic_regex(std::move(__first), std::move(__last), locale_type(), __f) { } /** @@ -657,15 +643,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 assign(const basic_string<_Ch_type, _Ch_traits, _Alloc>& __s, flag_type __flags = ECMAScript) { - _M_flags = __flags; - _M_original_str.assign(__s.begin(), __s.end()); - auto __p = _M_original_str.c_str(); - _M_automaton = __detail::__compile_nfa<_Rx_traits>( - __p, - __p + _M_original_str.size(), - _M_loc, - _M_flags); - return *this; + return this->assign(basic_regex(__s.data(), __s.data() + __s.size(), + _M_loc, _M_flags)); } /** @@ -709,7 +688,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ unsigned int mark_count() const - { return _M_automaton->_M_sub_count() - 1; } + { + if (_M_automaton) + return _M_automaton->_M_sub_count() - 1; + return 0; + } /** * @brief Gets the flags used to construct the regular expression @@ -729,8 +712,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 imbue(locale_type __loc) { std::swap(__loc, _M_loc); - if (_M_automaton != nullptr) - this->assign(_M_original_str, _M_flags); + _M_automaton = nullptr; return __loc; } @@ -753,7 +735,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { std::swap(_M_flags, __rhs._M_flags); std::swap(_M_loc, __rhs._M_loc); - std::swap(_M_original_str, __rhs._M_original_str); std::swap(_M_automaton, __rhs._M_automaton); } @@ -764,7 +745,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 #endif private: - typedef std::shared_ptr<__detail::_NFA<_Rx_traits>> _AutomatonPtr; + typedef std::shared_ptr> _AutomatonPtr; + + template + basic_regex(_FwdIter __first, _FwdIter __last, locale_type __loc, + flag_type __f) + : _M_flags(__f), _M_loc(std::move(__loc)), + _M_automaton(__detail::__compile_nfa<_FwdIter, _Rx_traits>( + std::move(__first), std::move(__last), _M_loc, _M_flags)) + { } template @@ -778,7 +767,6 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 flag_type _M_flags; locale_type _M_loc; - basic_string<_Ch_type> _M_original_str; _AutomatonPtr _M_automaton; }; -- cgit v1.1