TLA 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/capy
8 : //
9 :
10 : #ifndef BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP
11 : #define BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP
12 :
13 : #include <boost/capy/detail/config.hpp>
14 : #include <boost/capy/buffers.hpp>
15 : #include <cstring>
16 : #include <utility>
17 :
18 : namespace boost {
19 : namespace capy {
20 :
21 : /** Copy the contents of a buffer sequence into another buffer sequence.
22 :
23 : @functionobject
24 : */
25 : constexpr struct
26 : {
27 : /** Copy the contents of a buffer sequence into another buffer sequence.
28 :
29 : Copies bytes from the constant buffer sequence `src` into the
30 : mutable buffer sequence `dest`, stopping when any limit is
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.
35 :
36 : @param dest The destination buffer sequence.
37 :
38 : @param src The source buffer sequence.
39 :
40 : @param at_most The maximum bytes to copy. Default copies all
41 : available.
42 :
43 : @return The number of bytes copied.
44 : */
45 : template<
46 : MutableBufferSequence MB,
47 : ConstBufferSequence CB>
48 : std::size_t
49 HIT 10107 : operator()(
50 : MB const& dest,
51 : CB const& src,
52 : std::size_t at_most = std::size_t(-1)) const noexcept
53 : {
54 10107 : std::size_t total = 0;
55 10107 : std::size_t pos0 = 0;
56 10107 : std::size_t pos1 = 0;
57 10107 : auto const end0 = buffer_sequence_end(src);
58 10107 : auto const end1 = buffer_sequence_end(dest);
59 10107 : auto it0 = buffer_sequence_begin(src);
60 10107 : auto it1 = buffer_sequence_begin(dest);
61 10107 : while(
62 33222 : total < at_most &&
63 47276 : it0 != end0 &&
64 8462 : it1 != end1)
65 : {
66 17393 : const_buffer b0 = *it0;
67 17393 : mutable_buffer b1 = *it1;
68 17393 : b0 += pos0;
69 17393 : b1 += pos1;
70 : std::size_t const amount =
71 52179 : [&]
72 : {
73 17393 : std::size_t n = b0.size();
74 17393 : if( n > b1.size())
75 5274 : n = b1.size();
76 17393 : if( n > at_most - total)
77 4903 : n = at_most - total;
78 17393 : if(n != 0)
79 16540 : std::memcpy(
80 : b1.data(),
81 : b0.data(),
82 : n);
83 17393 : return n;
84 17393 : }();
85 17393 : total += amount;
86 17393 : if(amount == b1.size())
87 : {
88 5505 : ++it1;
89 5505 : pos1 = 0;
90 : }
91 : else
92 : {
93 11888 : pos1 += amount;
94 : }
95 17393 : if(amount == b0.size())
96 : {
97 8632 : ++it0;
98 8632 : pos0 = 0;
99 : }
100 : else
101 : {
102 8761 : pos0 += amount;
103 : }
104 : }
105 10107 : return total;
106 : }
107 : } buffer_copy {};
108 :
109 : } // capy
110 : } // boost
111 :
112 : #endif
|