[libcxx-commits] [PATCH] D113868: [libcxx] Cast to the right `difference_type` in `std::search` and `std::find_end`

Fabian Wolff via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Nov 14 16:56:18 PST 2021


fwolff created this revision.
fwolff requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Fixes PR#52151.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113868

Files:
  libcxx/include/__algorithm/find_end.h
  libcxx/include/__algorithm/search.h


Index: libcxx/include/__algorithm/search.h
===================================================================
--- libcxx/include/__algorithm/search.h
+++ libcxx/include/__algorithm/search.h
@@ -68,7 +68,8 @@
   const _D1 __len1 = __last1 - __first1;
   if (__len1 < __len2)
     return _VSTD::make_pair(__last1, __last1);
-  const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
+  const _RandomAccessIterator1 __s =
+      __last1 - static_cast<_D1>(__len2 - 1); // Start of pattern match can't go beyond here
 
   while (true) {
     while (true) {
@@ -83,7 +84,7 @@
     _RandomAccessIterator2 __m2 = __first2;
     while (true) {
       if (++__m2 == __last2)
-        return _VSTD::make_pair(__first1, __first1 + __len2);
+        return _VSTD::make_pair(__first1, __first1 + static_cast<_D1>(__len2));
       ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
       if (!__pred(*__m1, *__m2)) {
         ++__first1;
Index: libcxx/include/__algorithm/find_end.h
===================================================================
--- libcxx/include/__algorithm/find_end.h
+++ libcxx/include/__algorithm/find_end.h
@@ -95,14 +95,17 @@
 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 __find_end(
     _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
     _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, random_access_iterator_tag) {
+  typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
+  typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
   // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
-  typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
+  const _D2 __len2 = __last2 - __first2;
   if (__len2 == 0)
     return __last1;
-  typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
+  const _D1 __len1 = __last1 - __first1;
   if (__len1 < __len2)
     return __last1;
-  const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
+  const _RandomAccessIterator1 __s =
+      __first1 + static_cast<_D1>(__len2 - 1); // End of pattern match can't go before here
   _RandomAccessIterator1 __l1 = __last1;
   _RandomAccessIterator2 __l2 = __last2;
   --__l2;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113868.387143.patch
Type: text/x-patch
Size: 2490 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211115/ec79ed50/attachment.bin>


More information about the libcxx-commits mailing list