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. The count copied is the smallest of
  32 + `buffer_size(dest)`, `buffer_size(src)`, and `at_most`.
  33 +
  34 + @pre The memory regions of `dest` and `src` do not overlap.
32   35  
33   @param dest The destination buffer sequence. 36   @param dest The destination buffer sequence.
34   37  
35   @param src The source buffer sequence. 38   @param src The source buffer sequence.
36   39  
37   @param at_most The maximum bytes to copy. Default copies all 40   @param at_most The maximum bytes to copy. Default copies all
38   available. 41   available.
39   42  
40 - @return The number of bytes copied, equal to 43 + @return The number of bytes copied.
41 - `std::min(size(dest), size(src), at_most)`.  
42   */ 44   */
43   template< 45   template<
44   MutableBufferSequence MB, 46   MutableBufferSequence MB,
45   ConstBufferSequence CB> 47   ConstBufferSequence CB>
46   std::size_t 48   std::size_t
HITCBC 47   10057 operator()( 49   10107 operator()(
48   MB const& dest, 50   MB const& dest,
49   CB const& src, 51   CB const& src,
50   std::size_t at_most = std::size_t(-1)) const noexcept 52   std::size_t at_most = std::size_t(-1)) const noexcept
51   { 53   {
HITCBC 52   10057 std::size_t total = 0; 54   10107 std::size_t total = 0;
HITCBC 53   10057 std::size_t pos0 = 0; 55   10107 std::size_t pos0 = 0;
HITCBC 54   10057 std::size_t pos1 = 0; 56   10107 std::size_t pos1 = 0;
HITCBC 55   10057 auto const end0 = buffer_sequence_end(src); 57   10107 auto const end0 = buffer_sequence_end(src);
HITCBC 56   10057 auto const end1 = buffer_sequence_end(dest); 58   10107 auto const end1 = buffer_sequence_end(dest);
HITCBC 57   10057 auto it0 = buffer_sequence_begin(src); 59   10107 auto it0 = buffer_sequence_begin(src);
HITCBC 58   10057 auto it1 = buffer_sequence_begin(dest); 60   10107 auto it1 = buffer_sequence_begin(dest);
HITCBC 59   10057 while( 61   10107 while(
HITCBC 60   33155 total < at_most && 62   33222 total < at_most &&
HITCBC 61   47126 it0 != end0 && 63   47276 it0 != end0 &&
HITCBC 62   8462 it1 != end1) 64   8462 it1 != end1)
63   { 65   {
HITCBC 64   17343 const_buffer b0 = *it0; 66   17393 const_buffer b0 = *it0;
HITCBC 65   17343 mutable_buffer b1 = *it1; 67   17393 mutable_buffer b1 = *it1;
HITCBC 66   17343 b0 += pos0; 68   17393 b0 += pos0;
HITCBC 67   17343 b1 += pos1; 69   17393 b1 += pos1;
68   std::size_t const amount = 70   std::size_t const amount =
HITCBC 69   52029 [&] 71   52179 [&]
70   { 72   {
HITCBC 71   17343 std::size_t n = b0.size(); 73   17393 std::size_t n = b0.size();
HITCBC 72   17343 if( n > b1.size()) 74   17393 if( n > b1.size())
HITCBC 73   5274 n = b1.size(); 75   5274 n = b1.size();
HITCBC 74   17343 if( n > at_most - total) 76   17393 if( n > at_most - total)
HITCBC 75   4903 n = at_most - total; 77   4903 n = at_most - total;
HITCBC 76   17343 if(n != 0) 78   17393 if(n != 0)
HITCBC 77   16490 std::memcpy( 79   16540 std::memcpy(
78   b1.data(), 80   b1.data(),
79   b0.data(), 81   b0.data(),
80   n); 82   n);
HITCBC 81   17343 return n; 83   17393 return n;
HITCBC 82   17343 }(); 84   17393 }();
HITCBC 83   17343 total += amount; 85   17393 total += amount;
HITCBC 84   17343 if(amount == b1.size()) 86   17393 if(amount == b1.size())
85   { 87   {
HITCBC 86   5472 ++it1; 88   5505 ++it1;
HITCBC 87   5472 pos1 = 0; 89   5505 pos1 = 0;
88   } 90   }
89   else 91   else
90   { 92   {
HITCBC 91   11871 pos1 += amount; 93   11888 pos1 += amount;
92   } 94   }
HITCBC 93   17343 if(amount == b0.size()) 95   17393 if(amount == b0.size())
94   { 96   {
HITCBC 95   8582 ++it0; 97   8632 ++it0;
HITCBC 96   8582 pos0 = 0; 98   8632 pos0 = 0;
97   } 99   }
98   else 100   else
99   { 101   {
HITCBC 100   8761 pos0 += amount; 102   8761 pos0 += amount;
101   } 103   }
102   } 104   }
HITCBC 103   10057 return total; 105   10107 return total;
104   } 106   }
105   } buffer_copy {}; 107   } buffer_copy {};
106   108  
107   } // capy 109   } // capy
108   } // boost 110   } // boost
109   111  
110   #endif 112   #endif