Line data Source code
1 : //
2 : // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3 : //
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)
6 : //
7 : // Official repository: https://github.com/cppalliance/buffers
8 : //
9 :
10 : #ifndef BOOST_BUFFERS_COPY_HPP
11 : #define BOOST_BUFFERS_COPY_HPP
12 :
13 : #include <boost/buffers/detail/config.hpp>
14 : #include <boost/buffers/buffer.hpp>
15 : #include <cstring>
16 : #include <utility>
17 :
18 : namespace boost {
19 : namespace buffers {
20 :
21 : /** Copy the contents of a buffer sequence into another buffer sequence
22 :
23 : This function copies no more than `at_most` bytes from the constant buffer
24 : sequence denoted by `src` into the mutable buffer sequence denoted by `dest`.
25 :
26 : @par Constraints
27 : @code
28 : requires is_mutable_buffer_sequence_v<decltype(dest)> &&
29 : is_const_buffer_sequence_v<decltype(src)>;
30 : @endcode
31 :
32 : @return The number of bytes actually copied, which will be exactly equal to
33 : `std::min( size(dest), size(src), at_most )`.
34 :
35 : @param dest The destination buffer sequence
36 :
37 : @param src The source buffer sequence
38 : */
39 : constexpr struct copy_mrdocs_workaround_t
40 : {
41 : template<
42 : class MutableBufferSequence,
43 : class ConstBufferSequence>
44 : auto
45 599514 : operator()(
46 : MutableBufferSequence const& dest,
47 : ConstBufferSequence const& src,
48 : std::size_t at_most = std::size_t(-1)) const noexcept -> typename std::enable_if<
49 : is_mutable_buffer_sequence<MutableBufferSequence>::value &&
50 : is_const_buffer_sequence<ConstBufferSequence>::value, std::size_t>::type
51 : {
52 599514 : std::size_t total = 0;
53 599514 : std::size_t pos0 = 0;
54 599514 : std::size_t pos1 = 0;
55 599514 : auto const end0 = end(src);
56 599514 : auto const end1 = end(dest);
57 599514 : auto it0 = begin(src);
58 599514 : auto it1 = begin(dest);
59 599514 : while(
60 1617646 : total < at_most &&
61 2935171 : it0 != end0 &&
62 2 : it1 != end1)
63 : {
64 1024594 : const_buffer b0 = *it0;
65 1024594 : mutable_buffer b1 = *it1;
66 1024594 : b0 += pos0;
67 1024594 : b1 += pos1;
68 : std::size_t const amount =
69 3073782 : [&]
70 : {
71 1024594 : std::size_t n = b0.size();
72 1024594 : if( n > b1.size())
73 23653 : n = b1.size();
74 1024594 : if( n > at_most - total)
75 4878 : n = at_most - total;
76 1024594 : if(n != 0)
77 771415 : std::memcpy(
78 : b1.data(),
79 : b0.data(),
80 : n);
81 1024594 : return n;
82 1024594 : }();
83 1024594 : total += amount;
84 1024594 : if(amount == b1.size())
85 : {
86 32093 : ++it1;
87 32093 : pos1 = 0;
88 : }
89 : else
90 : {
91 992501 : pos1 += amount;
92 : }
93 1024594 : if(amount == b0.size())
94 : {
95 997479 : ++it0;
96 997479 : pos0 = 0;
97 : }
98 : else
99 : {
100 27115 : pos0 += amount;
101 : }
102 : }
103 599516 : return total;
104 2 : }
105 : } copy {};
106 :
107 : } // buffers
108 : } // boost
109 :
110 : #endif
|