Implement P0253R1, Fixing a design mistake in the searchers interface in Library...
authorVille Voutilainen <ville.voutilainen@gmail.com>
Mon, 12 Sep 2016 15:48:32 +0000 (18:48 +0300)
committerVille Voutilainen <ville@gcc.gnu.org>
Mon, 12 Sep 2016 15:48:32 +0000 (18:48 +0300)
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
libstdc++-v3/include/std/functional
libstdc++-v3/testsuite/20_util/function_objects/searchers.cc

index 594cf1336d19165292e5911ddce7a9c666292d0d..7ae3433cfd269ebec7cb7b31eea8a55105b965ad 100644 (file)
@@ -1,3 +1,13 @@
+2016-09-12  Ville Voutilainen  <ville.voutilainen@gmail.com>
+
+       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  <ville.voutilainen@gmail.com>
 
        Implement C++17 string searchers.
index 3f0c1a8b2ef1184c8f1a5d80cd05758138021bb0..c750a8374bc1daca953ee6c88a11b962230d0ee1 100644 (file)
@@ -62,6 +62,7 @@
 #include <unordered_map>
 #include <vector>
 #include <array>
+#include <utility>
 #include <bits/stl_algo.h>
 #endif
 
@@ -2217,12 +2218,17 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
       { }
 
       template<typename _ForwardIterator2>
-       _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<typename _RandomAccessIterator2>
-       _RandomAccessIterator2
+        pair<_RandomAccessIterator2, _RandomAccessIterator2>
        operator()(_RandomAccessIterator2 __first,
                   _RandomAccessIterator2 __last) const;
 
@@ -2389,26 +2395,27 @@ _GLIBCXX_MEM_FN_TRAITS(&&, false_type, true_type)
       { }
 
       template<typename _RandomAccessIterator2>
-       _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<typename _RAIter, typename _Hash, typename _BinaryPredicate>
   template<typename _RandomAccessIterator2>
-    _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
index 62a636a69f20b41a4ecf61fe5b87363b9b4c4ce3..1c7276207d9fb99fa4eddd77171b28b0cea351f9 100644 (file)
@@ -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