100.00% Lines (36/36) 100.00% Functions (2/2)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/capy 7   // Official repository: https://github.com/cppalliance/capy
8   // 8   //
9   9  
10   #ifndef BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP 10   #ifndef BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP
11   #define BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP 11   #define BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP
12   12  
13   #include <boost/capy/detail/config.hpp> 13   #include <boost/capy/detail/config.hpp>
14   #include <boost/capy/buffers.hpp> 14   #include <boost/capy/buffers.hpp>
15   #include <cstring> 15   #include <cstring>
16   #include <utility> 16   #include <utility>
17   17  
18   namespace boost { 18   namespace boost {
19   namespace capy { 19   namespace capy {
20   20  
21   /** Copy the contents of a buffer sequence into another buffer sequence. 21   /** Copy the contents of a buffer sequence into another buffer sequence.
22   22  
23   @functionobject 23   @functionobject
24   */ 24   */
25   constexpr struct 25   constexpr struct
26   { 26   {
27   /** Copy the contents of a buffer sequence into another buffer sequence. 27   /** Copy the contents of a buffer sequence into another buffer sequence.
28   28  
29   Copies bytes from the constant buffer sequence `src` into the 29   Copies bytes from the constant buffer sequence `src` into the
30   mutable buffer sequence `dest`, stopping when any limit is 30   mutable buffer sequence `dest`, stopping when any limit is
31   reached. 31   reached.
32   32  
33   @param dest The destination buffer sequence. 33   @param dest The destination buffer sequence.
34   34  
35   @param src The source buffer sequence. 35   @param src The source buffer sequence.
36   36  
37   @param at_most The maximum bytes to copy. Default copies all 37   @param at_most The maximum bytes to copy. Default copies all
38   available. 38   available.
39   39  
40   @return The number of bytes copied, equal to 40   @return The number of bytes copied, equal to
41   `std::min(size(dest), size(src), at_most)`. 41   `std::min(size(dest), size(src), at_most)`.
42   */ 42   */
43   template< 43   template<
44   MutableBufferSequence MB, 44   MutableBufferSequence MB,
45   ConstBufferSequence CB> 45   ConstBufferSequence CB>
46   std::size_t 46   std::size_t
HITCBC 47   10064 operator()( 47   10059 operator()(
48   MB const& dest, 48   MB const& dest,
49   CB const& src, 49   CB const& src,
50   std::size_t at_most = std::size_t(-1)) const noexcept 50   std::size_t at_most = std::size_t(-1)) const noexcept
51   { 51   {
HITCBC 52   10064 std::size_t total = 0; 52   10059 std::size_t total = 0;
HITCBC 53   10064 std::size_t pos0 = 0; 53   10059 std::size_t pos0 = 0;
HITCBC 54   10064 std::size_t pos1 = 0; 54   10059 std::size_t pos1 = 0;
HITCBC 55 - 10064 auto const end0 = end(src); 55 + 10059 auto const end0 = buffer_sequence_end(src);
HITCBC 56 - 10064 auto const end1 = end(dest); 56 + 10059 auto const end1 = buffer_sequence_end(dest);
HITCBC 57 - 10064 auto it0 = begin(src); 57 + 10059 auto it0 = buffer_sequence_begin(src);
HITCBC 58 - 10064 auto it1 = begin(dest); 58 + 10059 auto it1 = buffer_sequence_begin(dest);
HITCBC 59   10064 while( 59   10059 while(
HITCBC 60   33166 total < at_most && 60   33158 total < at_most &&
HITCBC 61   47152 it0 != end0 && 61   47132 it0 != end0 &&
HITCBC 62   8466 it1 != end1) 62   8462 it1 != end1)
63   { 63   {
HITCBC 64   17352 const_buffer b0 = *it0; 64   17345 const_buffer b0 = *it0;
HITCBC 65   17352 mutable_buffer b1 = *it1; 65   17345 mutable_buffer b1 = *it1;
HITCBC 66   17352 b0 += pos0; 66   17345 b0 += pos0;
HITCBC 67   17352 b1 += pos1; 67   17345 b1 += pos1;
68   std::size_t const amount = 68   std::size_t const amount =
HITCBC 69   52056 [&] 69   52035 [&]
70   { 70   {
HITCBC 71   17352 std::size_t n = b0.size(); 71   17345 std::size_t n = b0.size();
HITCBC 72   17352 if( n > b1.size()) 72   17345 if( n > b1.size())
HITCBC 73   5276 n = b1.size(); 73   5274 n = b1.size();
HITCBC 74   17352 if( n > at_most - total) 74   17345 if( n > at_most - total)
HITCBC 75   4905 n = at_most - total; 75   4903 n = at_most - total;
HITCBC 76   17352 if(n != 0) 76   17345 if(n != 0)
HITCBC 77   16499 std::memcpy( 77   16492 std::memcpy(
78   b1.data(), 78   b1.data(),
79   b0.data(), 79   b0.data(),
80   n); 80   n);
HITCBC 81   17352 return n; 81   17345 return n;
HITCBC 82   17352 }(); 82   17345 }();
HITCBC 83   17352 total += amount; 83   17345 total += amount;
HITCBC 84   17352 if(amount == b1.size()) 84   17345 if(amount == b1.size())
85   { 85   {
HITCBC 86   5477 ++it1; 86   5473 ++it1;
HITCBC 87   5477 pos1 = 0; 87   5473 pos1 = 0;
88   } 88   }
89   else 89   else
90   { 90   {
HITCBC 91   11875 pos1 += amount; 91   11872 pos1 += amount;
92   } 92   }
HITCBC 93   17352 if(amount == b0.size()) 93   17345 if(amount == b0.size())
94   { 94   {
HITCBC 95   8588 ++it0; 95   8584 ++it0;
HITCBC 96   8588 pos0 = 0; 96   8584 pos0 = 0;
97   } 97   }
98   else 98   else
99   { 99   {
HITCBC 100   8764 pos0 += amount; 100   8761 pos0 += amount;
101   } 101   }
102   } 102   }
HITCBC 103   10064 return total; 103   10059 return total;
104   } 104   }
105   } buffer_copy {}; 105   } buffer_copy {};
106   106  
107   } // capy 107   } // capy
108   } // boost 108   } // boost
109   109  
110   #endif 110   #endif