From 5e8037ba97f4b333d7e55aad3f76bfe1c7d28239 Mon Sep 17 00:00:00 2001 From: Ville Voutilainen Date: Mon, 12 Sep 2016 18:48:32 +0300 Subject: [PATCH] Implement P0253R1, Fixing a design mistake in the searchers interface in Library Fundamentals. Implement P0253R1, Fixing a design mistake in the searchers interface in Library Fundamentals. * include/std/functional: (utility): New include in C++17 mode. (default_searcher): Use a pair as return type, adjust the definition. (boyer_moore_searcher): Likewise. (boyer_moore_horspool_searcher): Likewise. * testsuite/20_util/function_objects/searchers.cc: Adjust. From-SVN: r240094 --- libstdc++-v3/ChangeLog | 10 ++++++ libstdc++-v3/include/std/functional | 34 ++++++++++++------- .../20_util/function_objects/searchers.cc | 18 +++++----- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 594cf1336d1..7ae3433cfd2 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +2016-09-12 Ville Voutilainen + + Implement P0253R1, Fixing a design mistake in the searchers + interface in Library Fundamentals. + * include/std/functional: (utility): New include in C++17 mode. + (default_searcher): Use a pair as return type, adjust the definition. + (boyer_moore_searcher): Likewise. + (boyer_moore_horspool_searcher): Likewise. + * testsuite/20_util/function_objects/searchers.cc: Adjust. + 2016-09-12 Ville Voutilainen Implement C++17 string searchers. diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 3f0c1a8b2ef..c750a8374bc 100644 --- a/libstdc++-v3/include/std/functional +++ b/libstdc++-v3/include/std/functional @@ -62,6 +62,7 @@ #include #include #include +#include #include #endif @@ -2217,12 +2218,17 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) { } template - _ForwardIterator2 + pair<_ForwardIterator2, _ForwardIterator2> operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const { - return std::search(__first, __last, - std::get<0>(_M_m), std::get<1>(_M_m), - std::get<2>(_M_m)); + _ForwardIterator2 __first_ret = + std::search(__first, __last, + std::get<0>(_M_m), std::get<1>(_M_m), + std::get<2>(_M_m)); + _ForwardIterator2 __second_ret = __first_ret == __last ? + __last : std::next(__first_ret, std::distance(std::get<0>(_M_m), + std::get<1>(_M_m))); + return std::make_pair(__first_ret, __second_ret); } private: @@ -2328,7 +2334,7 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) _BinaryPredicate __pred = _BinaryPredicate()); template - _RandomAccessIterator2 + pair<_RandomAccessIterator2, _RandomAccessIterator2> operator()(_RandomAccessIterator2 __first, _RandomAccessIterator2 __last) const; @@ -2389,26 +2395,27 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) { } template - _RandomAccessIterator2 + pair<_RandomAccessIterator2, _RandomAccessIterator2> operator()(_RandomAccessIterator2 __first, _RandomAccessIterator2 __last) const { const auto& __pred = this->_M_pred(); auto __patlen = _M_pat_end - _M_pat; if (__patlen == 0) - return __first; + return std::make_pair(__first, __first); auto __len = __last - __first; while (__len >= __patlen) { for (auto __scan = __patlen - 1; __pred(__first[__scan], _M_pat[__scan]); --__scan) if (__scan == 0) - return __first; + return std::make_pair(__first, + std::next(__first, __patlen)); auto __shift = _M_bad_char_shift(__first[__patlen - 1]); __len -= __shift; __first += __shift; } - return __last; + return std::make_pair(__last, __last); } private: @@ -2479,14 +2486,14 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) template template - _RandomAccessIterator2 + pair<_RandomAccessIterator2, _RandomAccessIterator2> boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>:: operator()(_RandomAccessIterator2 __first, _RandomAccessIterator2 __last) const { auto __patlen = _M_pat_end - _M_pat; if (__patlen == 0) - return __first; + return std::make_pair(__first, __first); const auto& __pred = this->_M_pred(); __diff_type __i = __patlen - 1; auto __stringlen = __last - __first; @@ -2499,11 +2506,12 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type) --__j; } if (__j < 0) - return __first + __i + 1; + return std::make_pair(__first + __i + 1, std::next(__first, + __patlen)); __i += std::max(_M_bad_char_shift(__first[__i]), _M_good_suffix[__j]); } - return __last; + return std::make_pair(__last, __last); } #endif diff --git a/libstdc++-v3/testsuite/20_util/function_objects/searchers.cc b/libstdc++-v3/testsuite/20_util/function_objects/searchers.cc index 62a636a69f2..1c7276207d9 100644 --- a/libstdc++-v3/testsuite/20_util/function_objects/searchers.cc +++ b/libstdc++-v3/testsuite/20_util/function_objects/searchers.cc @@ -52,11 +52,11 @@ test01() auto he = h + std::strlen(h); auto res = std::search(h, he, n, ne); auto d_res = d(h, he); - VERIFY( d_res == res ); + VERIFY( d_res.first == res ); auto bm_res = bm(h, he); - VERIFY( bm_res == res ); + VERIFY( bm_res.first == res ); auto bmh_res = bmh(h, he); - VERIFY( bmh_res == res ); + VERIFY( bmh_res.first == res ); } } } @@ -85,11 +85,11 @@ test02() auto he = h + std::wcslen(h); auto res = std::search(h, he, n, ne); auto d_res = d(h, he); - VERIFY( d_res == res ); + VERIFY( d_res.first == res ); auto bm_res = bm(h, he); - VERIFY( bm_res == res ); + VERIFY( bm_res.first == res ); auto bmh_res = bmh(h, he); - VERIFY( bmh_res == res ); + VERIFY( bmh_res.first == res ); } } #endif @@ -122,11 +122,11 @@ test03() auto res = std::search(haystack, he, needle, ne, eq); auto d_res = d(haystack, he); - VERIFY( d_res == res ); + VERIFY( d_res.first == res ); auto bm_res = bm(haystack, he); - VERIFY( bm_res == res ); + VERIFY( bm_res.first == res ); auto bmh_res = bmh(haystack, he); - VERIFY( bmh_res == res ); + VERIFY( bmh_res.first == res ); } int -- 2.30.2