From e112d53acae3d4ce8783d8fd5f396cc45443c010 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 2 Jul 2018 21:31:11 +0100 Subject: [PATCH] Optimize std::sub_match comparisons using string_view-like type Avoid creation of unnecessary basic_string objects by using a simplified string_view type and performing comparisons on that type instead. A temporary basic_string object is still used when the sub_match's iterators are not contiguous, in order to get an object that the __string_view can reference. * include/bits/regex.h (sub_match::operator string_type): Call str(). (sub_match::compare): Use _M_str() instead of str(). (sub_match::_M_compare): New public function. (sub_match::__string_view): New helper type. (sub_match::_M_str): New overloaded functions to avoid creating a string_type object when not needed. (operator==, operator!=, operator<, operator>, operator<=, operator>=): Use sub_match::_M_compare instead of creating string_type objects. Fix Doxygen comments. * include/bits/regex_compiler.h (__has_contiguous_iter): Remove. (__is_contiguous_normal_iter): Rename to __is_contiguous_iter and simplify. (__enable_if_contiguous_iter, __disable_if_contiguous_iter): Use __enable_if_t. * include/std/type_traits (__enable_if_t): Define for C++11. * testsuite/28_regex/sub_match/compare.cc: New. * testsuite/util/testsuite_iterators.h (remove_cv): Add transformation trait. (input_iterator_wrapper): Use remove_cv for value_type argument of std::iterator base class. From-SVN: r262318 --- libstdc++-v3/ChangeLog | 23 ++ libstdc++-v3/include/bits/regex.h | 244 +++++++------- libstdc++-v3/include/bits/regex_compiler.h | 39 +-- libstdc++-v3/include/std/type_traits | 11 +- .../testsuite/28_regex/sub_match/compare.cc | 303 ++++++++++++++++++ .../testsuite/util/testsuite_iterators.h | 8 +- 6 files changed, 489 insertions(+), 139 deletions(-) create mode 100644 libstdc++-v3/testsuite/28_regex/sub_match/compare.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7645fa1f027..b2958e43745 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,26 @@ +2018-07-02 Jonathan Wakely + + * include/bits/regex.h (sub_match::operator string_type): Call str(). + (sub_match::compare): Use _M_str() instead of str(). + (sub_match::_M_compare): New public function. + (sub_match::__string_view): New helper type. + (sub_match::_M_str): New overloaded functions to avoid creating a + string_type object when not needed. + (operator==, operator!=, operator<, operator>, operator<=, operator>=): + Use sub_match::_M_compare instead of creating string_type objects. + Fix Doxygen comments. + * include/bits/regex_compiler.h (__has_contiguous_iter): Remove. + (__is_contiguous_normal_iter): Rename to __is_contiguous_iter and + simplify. + (__enable_if_contiguous_iter, __disable_if_contiguous_iter): Use + __enable_if_t. + * include/std/type_traits (__enable_if_t): Define for C++11. + * testsuite/28_regex/sub_match/compare.cc: New. + * testsuite/util/testsuite_iterators.h (remove_cv): Add transformation + trait. + (input_iterator_wrapper): Use remove_cv for value_type argument of + std::iterator base class. + 2018-06-29 Jonathan Wakely * testsuite/20_util/add_rvalue_reference/requirements/alias_decl.cc: diff --git a/libstdc++-v3/include/bits/regex.h b/libstdc++-v3/include/bits/regex.h index 6b6501e98ae..af6fe3f0d79 100644 --- a/libstdc++-v3/include/bits/regex.h +++ b/libstdc++-v3/include/bits/regex.h @@ -42,8 +42,7 @@ _GLIBCXX_END_NAMESPACE_CXX11 namespace __detail { - enum class _RegexExecutorPolicy : int - { _S_auto, _S_alternate }; + enum class _RegexExecutorPolicy : int { _S_auto, _S_alternate }; templatematched ? std::distance(this->first, this->second) : 0; } @@ -893,11 +890,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * from the unwary. */ operator string_type() const - { - return this->matched - ? string_type(this->first, this->second) - : string_type(); - } + { return str(); } /** * @brief Gets the matching sequence as a string. @@ -923,9 +916,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ int compare(const sub_match& __s) const - { return this->str().compare(__s.str()); } + { return this->_M_str().compare(__s._M_str()); } /** + * @{ * @brief Compares this sub_match to a string. * * @param __s A string to compare to this sub_match. @@ -936,20 +930,72 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 */ int compare(const string_type& __s) const - { return this->str().compare(__s); } + { return this->_M_str().compare(__s); } - /** - * @brief Compares this sub_match to a C-style string. - * - * @param __s A C-style string to compare to this sub_match. - * - * @retval <0 this matched sequence will collate before @p __s. - * @retval =0 this matched sequence is equivalent to @p __s. - * @retval <0 this matched sequence will collate after @p __s. - */ int compare(const value_type* __s) const - { return this->str().compare(__s); } + { return this->_M_str().compare(__s); } + // @} + + // Non-standard, used by comparison operators + int + _M_compare(const value_type* __s, size_t __n) const + { return this->_M_str().compare({__s, __n}); } + + private: + // Simplified basic_string_view for C++11 + struct __string_view + { + using traits_type = typename string_type::traits_type; + + __string_view() = default; + + __string_view(const value_type* __s, size_t __n) noexcept + : _M_data(__s), _M_len(__n) { } + + __string_view(const value_type* __s) noexcept + : _M_data(__s), _M_len(traits_type::length(__s)) { } + + __string_view(const string_type& __s) noexcept + : _M_data(__s.data()), _M_len(__s.length()) { } + + int + compare(__string_view __s) const noexcept + { + if (const size_t __n = std::min(_M_len, __s._M_len)) + if (int __ret = traits_type::compare(_M_data, __s._M_data, __n)) + return __ret; + const difference_type __diff = _M_len - __s._M_len; + if (__diff > std::numeric_limits::max()) + return std::numeric_limits::max(); + if (__diff < std::numeric_limits::min()) + return std::numeric_limits::min(); + return static_cast(__diff); + } + + private: + const value_type* _M_data = nullptr; + size_t _M_len = 0; + }; + + // Create a __string_view over the iterator range. + template + __enable_if_t<__detail::__is_contiguous_iter<_Iter>::value, + __string_view> + _M_str() const noexcept + { + if (this->matched) + if (auto __len = this->second - this->first) + return { std::__addressof(*this->first), __len }; + return {}; + } + + // Create a temporary string that can be converted to __string_view. + template + __enable_if_t::value, + string_type> + _M_str() const + { return str(); } }; @@ -1035,7 +1081,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 operator>(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs) { return __lhs.compare(__rhs) > 0; } - // Alias for sub_match'd string. + // Alias for a basic_string that can be compared to a sub_match. template using __sub_match_string = basic_string< typename iterator_traits<_Bi_iter>::value_type, @@ -1052,10 +1098,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline bool operator==(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __rhs.compare(string_type(__lhs.data(), __lhs.size())) == 0; - } + { return __rhs._M_compare(__lhs.data(), __lhs.size()) == 0; } /** * @brief Tests the inequivalence of a string and a regular expression @@ -1080,10 +1123,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline bool operator<(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs, const sub_match<_Bi_iter>& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __rhs.compare(string_type(__lhs.data(), __lhs.size())) > 0; - } + { return __rhs._M_compare(__lhs.data(), __lhs.size()) > 0; } /** * @brief Tests the ordering of a string and a regular expression submatch. @@ -1132,10 +1172,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline bool operator==(const sub_match<_Bi_iter>& __lhs, const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __lhs.compare(string_type(__rhs.data(), __rhs.size())) == 0; - } + { return __lhs._M_compare(__rhs.data(), __rhs.size()) == 0; } /** * @brief Tests the inequivalence of a regular expression submatch and a @@ -1156,14 +1193,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __rhs A string. * @returns true if @a __lhs precedes @a __rhs, false otherwise. */ - template + template inline bool operator<(const sub_match<_Bi_iter>& __lhs, const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __lhs.compare(string_type(__rhs.data(), __rhs.size())) < 0; - } + { return __lhs._M_compare(__rhs.data(), __rhs.size()) < 0; } /** * @brief Tests the ordering of a regular expression submatch and a string. @@ -1171,7 +1205,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __rhs A string. * @returns true if @a __lhs succeeds @a __rhs, false otherwise. */ - template + template inline bool operator>(const sub_match<_Bi_iter>& __lhs, const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs) @@ -1183,7 +1217,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __rhs A string. * @returns true if @a __lhs does not precede @a __rhs, false otherwise. */ - template + template inline bool operator>=(const sub_match<_Bi_iter>& __lhs, const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs) @@ -1195,7 +1229,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __rhs A string. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise. */ - template + template inline bool operator<=(const sub_match<_Bi_iter>& __lhs, const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __rhs) @@ -1204,7 +1238,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 /** * @brief Tests the equivalence of a C string and a regular expression * submatch. - * @param __lhs A C string. + * @param __lhs A null-terminated string. * @param __rhs A regular expression submatch. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise. */ @@ -1215,10 +1249,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __rhs.compare(__lhs) == 0; } /** - * @brief Tests the inequivalence of an iterator value and a regular + * @brief Tests the inequivalence of a C string and a regular * expression submatch. - * @param __lhs A regular expression submatch. - * @param __rhs A string. + * @param __lhs A null-terminated string. + * @param __rhs A regular expression submatch. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise. */ template @@ -1228,8 +1262,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs == __rhs); } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a C string and a regular expression submatch. + * @param __lhs A null-terminated string. * @param __rhs A regular expression submatch. * @returns true if @a __lhs precedes @a __rhs, false otherwise. */ @@ -1240,8 +1274,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __rhs.compare(__lhs) > 0; } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a C string and a regular expression submatch. + * @param __lhs A null-terminated string. * @param __rhs A regular expression submatch. * @returns true if @a __lhs succeeds @a __rhs, false otherwise. */ @@ -1252,8 +1286,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __rhs < __lhs; } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a C string and a regular expression submatch. + * @param __lhs A null-terminated string. * @param __rhs A regular expression submatch. * @returns true if @a __lhs does not precede @a __rhs, false otherwise. */ @@ -1264,8 +1298,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs < __rhs); } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a C string and a regular expression submatch. + * @param __lhs A null-terminated string. * @param __rhs A regular expression submatch. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise. */ @@ -1276,10 +1310,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__rhs < __lhs); } /** - * @brief Tests the equivalence of a regular expression submatch and a + * @brief Tests the equivalence of a regular expression submatch and a C * string. * @param __lhs A regular expression submatch. - * @param __rhs A pointer to a string? + * @param __rhs A null-terminated string. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise. */ template @@ -1292,7 +1326,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @brief Tests the inequivalence of a regular expression submatch and a * string. * @param __lhs A regular expression submatch. - * @param __rhs A pointer to a string. + * @param __rhs A null-terminated string. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise. */ template @@ -1302,9 +1336,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs == __rhs); } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a C string. * @param __lhs A regular expression submatch. - * @param __rhs A string. + * @param __rhs A null-terminated string. * @returns true if @a __lhs precedes @a __rhs, false otherwise. */ template @@ -1314,9 +1348,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __lhs.compare(__rhs) < 0; } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a C string. * @param __lhs A regular expression submatch. - * @param __rhs A string. + * @param __rhs A null-terminated string. * @returns true if @a __lhs succeeds @a __rhs, false otherwise. */ template @@ -1326,9 +1360,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __rhs < __lhs; } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a C string. * @param __lhs A regular expression submatch. - * @param __rhs A string. + * @param __rhs A null-terminated string. * @returns true if @a __lhs does not precede @a __rhs, false otherwise. */ template @@ -1338,9 +1372,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs < __rhs); } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a C string. * @param __lhs A regular expression submatch. - * @param __rhs A string. + * @param __rhs A null-terminated string. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise. */ template @@ -1350,9 +1384,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__rhs < __lhs); } /** - * @brief Tests the equivalence of a string and a regular expression + * @brief Tests the equivalence of a character and a regular expression * submatch. - * @param __lhs A string. + * @param __lhs A character. * @param __rhs A regular expression submatch. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise. */ @@ -1360,15 +1394,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline bool operator==(typename iterator_traits<_Bi_iter>::value_type const& __lhs, const sub_match<_Bi_iter>& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __rhs.compare(string_type(1, __lhs)) == 0; - } + { return __rhs._M_compare(std::__addressof(__lhs), 1) == 0; } /** - * @brief Tests the inequivalence of a string and a regular expression + * @brief Tests the inequivalence of a character and a regular expression * submatch. - * @param __lhs A string. + * @param __lhs A character. * @param __rhs A regular expression submatch. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise. */ @@ -1379,8 +1410,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs == __rhs); } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a character and a regular expression + * submatch. + * @param __lhs A character. * @param __rhs A regular expression submatch. * @returns true if @a __lhs precedes @a __rhs, false otherwise. */ @@ -1388,14 +1420,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 inline bool operator<(typename iterator_traits<_Bi_iter>::value_type const& __lhs, const sub_match<_Bi_iter>& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __rhs.compare(string_type(1, __lhs)) > 0; - } + { return __rhs._M_compare(std::__addressof(__lhs), 1) > 0; } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a character and a regular expression + * submatch. + * @param __lhs A character. * @param __rhs A regular expression submatch. * @returns true if @a __lhs succeeds @a __rhs, false otherwise. */ @@ -1406,8 +1436,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __rhs < __lhs; } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a character and a regular expression + * submatch. + * @param __lhs A character. * @param __rhs A regular expression submatch. * @returns true if @a __lhs does not precede @a __rhs, false otherwise. */ @@ -1418,8 +1449,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs < __rhs); } /** - * @brief Tests the ordering of a string and a regular expression submatch. - * @param __lhs A string. + * @brief Tests the ordering of a character and a regular expression + * submatch. + * @param __lhs A character. * @param __rhs A regular expression submatch. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise. */ @@ -1431,25 +1463,22 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 /** * @brief Tests the equivalence of a regular expression submatch and a - * string. + * character. * @param __lhs A regular expression submatch. - * @param __rhs A const string reference. + * @param __rhs A character. * @returns true if @a __lhs is equivalent to @a __rhs, false otherwise. */ template inline bool operator==(const sub_match<_Bi_iter>& __lhs, typename iterator_traits<_Bi_iter>::value_type const& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __lhs.compare(string_type(1, __rhs)) == 0; - } + { return __lhs._M_compare(std::__addressof(__rhs), 1) == 0; } /** * @brief Tests the inequivalence of a regular expression submatch and a - * string. + * character. * @param __lhs A regular expression submatch. - * @param __rhs A const string reference. + * @param __rhs A character. * @returns true if @a __lhs is not equivalent to @a __rhs, false otherwise. */ template @@ -1459,24 +1488,23 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs == __rhs); } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a + * character. * @param __lhs A regular expression submatch. - * @param __rhs A const string reference. + * @param __rhs A character. * @returns true if @a __lhs precedes @a __rhs, false otherwise. */ template inline bool operator<(const sub_match<_Bi_iter>& __lhs, typename iterator_traits<_Bi_iter>::value_type const& __rhs) - { - typedef typename sub_match<_Bi_iter>::string_type string_type; - return __lhs.compare(string_type(1, __rhs)) < 0; - } + { return __lhs._M_compare(std::__addressof(__rhs), 1) < 0; } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a + * character. * @param __lhs A regular expression submatch. - * @param __rhs A const string reference. + * @param __rhs A character. * @returns true if @a __lhs succeeds @a __rhs, false otherwise. */ template @@ -1486,9 +1514,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return __rhs < __lhs; } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a + * character. * @param __lhs A regular expression submatch. - * @param __rhs A const string reference. + * @param __rhs A character. * @returns true if @a __lhs does not precede @a __rhs, false otherwise. */ template @@ -1498,9 +1527,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { return !(__lhs < __rhs); } /** - * @brief Tests the ordering of a regular expression submatch and a string. + * @brief Tests the ordering of a regular expression submatch and a + * character. * @param __lhs A regular expression submatch. - * @param __rhs A const string reference. + * @param __rhs A character. * @returns true if @a __lhs does not succeed @a __rhs, false otherwise. */ template diff --git a/libstdc++-v3/include/bits/regex_compiler.h b/libstdc++-v3/include/bits/regex_compiler.h index 6eee9cb9072..f34b148742b 100644 --- a/libstdc++-v3/include/bits/regex_compiler.h +++ b/libstdc++-v3/include/bits/regex_compiler.h @@ -154,42 +154,25 @@ namespace __detail }; template - struct __has_contiguous_iter : std::false_type { }; - - template - struct __has_contiguous_iter> - : std::true_type - { }; - - template - struct __has_contiguous_iter> - : std::true_type - { }; - - template - struct __is_contiguous_normal_iter : std::false_type { }; - - template - struct __is_contiguous_normal_iter<_CharT*> : std::true_type { }; + struct __is_contiguous_iter : is_pointer<_Tp>::type { }; template struct - __is_contiguous_normal_iter<__gnu_cxx::__normal_iterator<_Tp, _Cont>> - : __has_contiguous_iter<_Cont>::type - { }; + __is_contiguous_iter<__gnu_cxx::__normal_iterator<_Tp*, _Cont>> + : true_type { }; template - using __enable_if_contiguous_normal_iter - = typename enable_if< __is_contiguous_normal_iter<_Iter>::value, - std::shared_ptr> >::type; + using __enable_if_contiguous_iter + = __enable_if_t< __is_contiguous_iter<_Iter>::value, + std::shared_ptr> >; template - using __disable_if_contiguous_normal_iter - = typename enable_if< !__is_contiguous_normal_iter<_Iter>::value, - std::shared_ptr> >::type; + using __disable_if_contiguous_iter + = __enable_if_t< !__is_contiguous_iter<_Iter>::value, + std::shared_ptr> >; template - inline __enable_if_contiguous_normal_iter<_FwdIter, _TraitsT> + inline __enable_if_contiguous_iter<_FwdIter, _TraitsT> __compile_nfa(_FwdIter __first, _FwdIter __last, const typename _TraitsT::locale_type& __loc, regex_constants::syntax_option_type __flags) @@ -201,7 +184,7 @@ namespace __detail } template - inline __disable_if_contiguous_normal_iter<_FwdIter, _TraitsT> + inline __disable_if_contiguous_iter<_FwdIter, _TraitsT> __compile_nfa(_FwdIter __first, _FwdIter __last, const typename _TraitsT::locale_type& __loc, regex_constants::syntax_option_type __flags) diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 01972d125c7..0c7e97286ca 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -2331,7 +2331,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : public __invoke_result<_Functor, _ArgTypes...> { }; -#if __cplusplus > 201103L +#if __cplusplus >= 201402L /// Alias template for aligned_storage template::__type)> @@ -2363,11 +2363,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Alias template for result_of template using result_of_t = typename result_of<_Tp>::type; -#endif +#endif // C++14 + // __enable_if_t (std::enable_if_t for C++11) + template + using __enable_if_t = typename enable_if<_Cond, _Tp>::type; + + // __void_t (std::void_t for C++11) template using __void_t = void; -#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 +#if __cplusplus >= 201703L || !defined(__STRICT_ANSI__) // c++17 or gnu++11 #define __cpp_lib_void_t 201411 /// A metafunction that always yields void, used for detecting valid types. template using void_t = void; diff --git a/libstdc++-v3/testsuite/28_regex/sub_match/compare.cc b/libstdc++-v3/testsuite/28_regex/sub_match/compare.cc new file mode 100644 index 00000000000..e6093afcfe8 --- /dev/null +++ b/libstdc++-v3/testsuite/28_regex/sub_match/compare.cc @@ -0,0 +1,303 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include +#include + +using __gnu_test::test_container; +using __gnu_test::bidirectional_iterator_wrapper; + +template struct traits : std::char_traits { }; + +void +test01() +{ + const std::basic_string> s0, s1 = "1"; + const std::ssub_match sm, sm2; + + VERIFY( sm.compare(sm) == 0 ); + VERIFY( sm.compare(sm2) == 0 ); + VERIFY( sm.compare(sm.str()) == 0 ); + VERIFY( sm.compare(sm.str().c_str()) == 0 ); + VERIFY( sm.compare(sm2.str()) == 0 ); + VERIFY( sm.compare(sm2.str().c_str()) == 0 ); + VERIFY( sm.compare(std::string(s1.c_str())) == -1 ); + VERIFY( sm.compare(s1.c_str()) == -1 ); + + VERIFY( sm == sm2 ); + VERIFY( !(sm != sm2) ); + VERIFY( !(sm < sm2) ); + VERIFY( !(sm > sm2) ); + VERIFY( sm <= sm2 ); + VERIFY( sm >= sm2 ); + + VERIFY( sm == s0 ); + VERIFY( !(sm != s0) ); + VERIFY( !(sm < s0) ); + VERIFY( !(sm > s0) ); + VERIFY( sm <= s0 ); + VERIFY( sm >= s0 ); + + VERIFY( s0 == sm ); + VERIFY( !(s0 != sm) ); + VERIFY( !(s0 < sm) ); + VERIFY( !(s0 > sm) ); + VERIFY( s0 <= sm ); + VERIFY( s0 >= sm ); + + VERIFY( sm == s0.c_str() ); + VERIFY( !(sm != s0.c_str()) ); + VERIFY( !(sm < s0.c_str()) ); + VERIFY( !(sm > s0.c_str()) ); + VERIFY( sm <= s0.c_str() ); + VERIFY( sm >= s0.c_str() ); + + VERIFY( s0.c_str() == sm ); + VERIFY( !(s0.c_str() != sm) ); + VERIFY( !(s0.c_str() < sm) ); + VERIFY( !(s0.c_str() > sm) ); + VERIFY( s0.c_str() <= sm ); + VERIFY( s0.c_str() >= sm ); + + VERIFY( !(sm == s1) ); + VERIFY( sm != s1 ); + VERIFY( sm < s1 ); + VERIFY( !(sm > s1) ); + VERIFY( sm <= s1 ); + VERIFY( !(sm >= s1) ); + + VERIFY( !(sm == s1.c_str()) ); + VERIFY( sm != s1.c_str() ); + VERIFY( sm < s1.c_str() ); + VERIFY( !(sm > s1.c_str()) ); + VERIFY( sm <= s1.c_str() ); + VERIFY( !(sm >= s1.c_str()) ); + + VERIFY( !(s1.c_str() == sm) ); + VERIFY( s1.c_str() != sm ); + VERIFY( !(s1.c_str() < sm) ); + VERIFY( s1.c_str() > sm ); + VERIFY( !(s1.c_str() <= sm) ); + VERIFY( s1.c_str() >= sm ); + + VERIFY( !(sm == s1[0]) ); + VERIFY( sm != s1[0] ); + VERIFY( sm < s1[0] ); + VERIFY( !(sm > s1[0]) ); + VERIFY( sm <= s1[0] ); + VERIFY( !(sm >= s1[0]) ); + + VERIFY( !(s1[0] == sm) ); + VERIFY( s1[0] != sm ); + VERIFY( !(s1[0] < sm) ); + VERIFY( s1[0] > sm ); + VERIFY( !(s1[0] <= sm) ); + VERIFY( s1[0] >= sm ); +} + +void +test02() +{ + const std::basic_string> s0, s1 = "1"; + std::csub_match sm; + const std::csub_match sm2; + const char c[] = "1"; + sm.matched = true; + sm.first = c; + sm.second = c+1; + + VERIFY( sm.compare(sm) == 0 ); + VERIFY( sm.compare(sm2) == 1 ); + VERIFY( sm.compare(sm.str()) == 0 ); + VERIFY( sm.compare(sm.str().c_str()) == 0 ); + VERIFY( sm.compare(sm2.str()) == 1 ); + VERIFY( sm.compare(sm2.str().c_str()) == 1 ); + VERIFY( sm.compare(std::string(s1.c_str())) == 0 ); + VERIFY( sm.compare(s1.c_str()) == 0 ); + + VERIFY( !(sm == sm2) ); + VERIFY( sm != sm2 ); + VERIFY( !(sm < sm2) ); + VERIFY( sm > sm2 ); + VERIFY( !(sm <= sm2) ); + VERIFY( sm >= sm2 ); + + VERIFY( !(sm2 == sm) ); + VERIFY( sm2 != sm ); + VERIFY( sm2 < sm ); + VERIFY( !(sm2 > sm) ); + VERIFY( sm2 <= sm ); + VERIFY( !(sm2 >= sm) ); + + VERIFY( !(sm == s0) ); + VERIFY( sm != s0 ); + VERIFY( !(sm < s0) ); + VERIFY( sm > s0 ); + VERIFY( !(sm <= s0) ); + VERIFY( sm >= s0 ); + + VERIFY( !(sm == s0.c_str()) ); + VERIFY( sm != s0.c_str() ); + VERIFY( !(sm < s0.c_str()) ); + VERIFY( sm > s0.c_str() ); + VERIFY( !(sm <= s0.c_str()) ); + VERIFY( sm >= s0.c_str() ); + + VERIFY( !(s0.c_str() == sm) ); + VERIFY( s0.c_str() != sm ); + VERIFY( s0.c_str() < sm ); + VERIFY( !(s0.c_str() > sm) ); + VERIFY( s0.c_str() <= sm ); + VERIFY( !(s0.c_str() >= sm) ); + + VERIFY( s1 == sm ); + VERIFY( !(s1 != sm) ); + VERIFY( !(s1 < sm) ); + VERIFY( !(s1 > sm) ); + VERIFY( s1 <= sm ); + VERIFY( s1 >= sm ); + + VERIFY( sm == s1.c_str() ); + VERIFY( !(sm != s1.c_str()) ); + VERIFY( !(sm < s1.c_str()) ); + VERIFY( !(sm > s1.c_str()) ); + VERIFY( sm <= s1.c_str() ); + VERIFY( sm >= s1.c_str() ); + + VERIFY( s1.c_str() == sm ); + VERIFY( !(s1.c_str() != sm) ); + VERIFY( !(s1.c_str() < sm) ); + VERIFY( !(s1.c_str() > sm) ); + VERIFY( s1.c_str() <= sm ); + VERIFY( s1.c_str() >= sm ); + + VERIFY( sm == s1[0] ); + VERIFY( !(sm != s1[0]) ); + VERIFY( !(sm < s1[0]) ); + VERIFY( !(sm > s1[0]) ); + VERIFY( sm <= s1[0] ); + VERIFY( sm >= s1[0] ); + + VERIFY( s1[0] == sm ); + VERIFY( !(s1[0] != sm) ); + VERIFY( !(s1[0] < sm) ); + VERIFY( !(s1[0] > sm) ); + VERIFY( s1[0] <= sm ); + VERIFY( s1[0] >= sm ); +} + +void +test03() +{ + const std::basic_string> s0, s1 = "1"; + const char c[] = "1"; + test_container tc(c, c+1); + std::sub_match> sm; + const std::sub_match> sm2; + sm.matched = true; + sm.first = tc.begin(); + sm.second = tc.end(); + + VERIFY( sm.compare(sm) == 0 ); + VERIFY( sm.compare(sm2) == 1 ); + VERIFY( sm.compare(sm.str()) == 0 ); + VERIFY( sm.compare(sm.str().c_str()) == 0 ); + VERIFY( sm.compare(sm2.str()) == 1 ); + VERIFY( sm.compare(sm2.str().c_str()) == 1 ); + VERIFY( sm.compare(std::string(s1.c_str())) == 0 ); + VERIFY( sm.compare(s1.c_str()) == 0 ); + + VERIFY( !(sm == sm2) ); + VERIFY( sm != sm2 ); + VERIFY( !(sm < sm2) ); + VERIFY( sm > sm2 ); + VERIFY( !(sm <= sm2) ); + VERIFY( sm >= sm2 ); + + VERIFY( !(sm2 == sm) ); + VERIFY( sm2 != sm ); + VERIFY( sm2 < sm ); + VERIFY( !(sm2 > sm) ); + VERIFY( sm2 <= sm ); + VERIFY( !(sm2 >= sm) ); + + VERIFY( !(sm == s0) ); + VERIFY( sm != s0 ); + VERIFY( !(sm < s0) ); + VERIFY( sm > s0 ); + VERIFY( !(sm <= s0) ); + VERIFY( sm >= s0 ); + + VERIFY( !(sm == s0.c_str()) ); + VERIFY( sm != s0.c_str() ); + VERIFY( !(sm < s0.c_str()) ); + VERIFY( sm > s0.c_str() ); + VERIFY( !(sm <= s0.c_str()) ); + VERIFY( sm >= s0.c_str() ); + + VERIFY( !(s0.c_str() == sm) ); + VERIFY( s0.c_str() != sm ); + VERIFY( s0.c_str() < sm ); + VERIFY( !(s0.c_str() > sm) ); + VERIFY( s0.c_str() <= sm ); + VERIFY( !(s0.c_str() >= sm) ); + + VERIFY( s1 == sm ); + VERIFY( !(s1 != sm) ); + VERIFY( !(s1 < sm) ); + VERIFY( !(s1 > sm) ); + VERIFY( s1 <= sm ); + VERIFY( s1 >= sm ); + + VERIFY( sm == s1.c_str() ); + VERIFY( !(sm != s1.c_str()) ); + VERIFY( !(sm < s1.c_str()) ); + VERIFY( !(sm > s1.c_str()) ); + VERIFY( sm <= s1.c_str() ); + VERIFY( sm >= s1.c_str() ); + + VERIFY( s1.c_str() == sm ); + VERIFY( !(s1.c_str() != sm) ); + VERIFY( !(s1.c_str() < sm) ); + VERIFY( !(s1.c_str() > sm) ); + VERIFY( s1.c_str() <= sm ); + VERIFY( s1.c_str() >= sm ); + + VERIFY( sm == s1[0] ); + VERIFY( !(sm != s1[0]) ); + VERIFY( !(sm < s1[0]) ); + VERIFY( !(sm > s1[0]) ); + VERIFY( sm <= s1[0] ); + VERIFY( sm >= s1[0] ); + + VERIFY( s1[0] == sm ); + VERIFY( !(s1[0] != sm) ); + VERIFY( !(s1[0] < sm) ); + VERIFY( !(s1[0] > sm) ); + VERIFY( s1[0] <= sm ); + VERIFY( s1[0] >= sm ); +} + +int main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/util/testsuite_iterators.h b/libstdc++-v3/testsuite/util/testsuite_iterators.h index 74b6b6064f9..4100514879d 100644 --- a/libstdc++-v3/testsuite/util/testsuite_iterators.h +++ b/libstdc++-v3/testsuite/util/testsuite_iterators.h @@ -185,6 +185,11 @@ namespace __gnu_test void operator,(const T&, const output_iterator_wrapper&) = delete; #endif + template struct remove_cv { typedef T type; }; + template struct remove_cv { typedef T type; }; + template struct remove_cv { typedef T type; }; + template struct remove_cv { typedef T type; }; + /** * @brief input_iterator wrapper for pointer * @@ -194,7 +199,8 @@ namespace __gnu_test */ template class input_iterator_wrapper - : public std::iterator + : public std::iterator::type, + std::ptrdiff_t, T*, T&> { protected: input_iterator_wrapper() -- 2.30.2