| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2025 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_TEST_BUFFER_TO_STRING_HPP
|
10 |
|
#ifndef BOOST_CAPY_TEST_BUFFER_TO_STRING_HPP
|
| 11 |
|
#define BOOST_CAPY_TEST_BUFFER_TO_STRING_HPP
|
11 |
|
#define BOOST_CAPY_TEST_BUFFER_TO_STRING_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 |
|
|
15 |
|
|
| 16 |
|
|
16 |
|
|
| 17 |
|
|
17 |
|
|
| 18 |
|
|
18 |
|
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
/** Convert one or more buffer sequences to a string.
|
22 |
|
/** Convert one or more buffer sequences to a string.
|
| 23 |
|
|
23 |
|
|
| 24 |
|
This function concatenates the bytes from all provided buffer
|
24 |
|
This function concatenates the bytes from all provided buffer
|
| 25 |
|
sequences into a single string. With a single argument, it
|
25 |
|
sequences into a single string. With a single argument, it
|
| 26 |
|
converts that buffer sequence to a string. With multiple
|
26 |
|
converts that buffer sequence to a string. With multiple
|
| 27 |
|
arguments, it concatenates them in order.
|
27 |
|
arguments, it concatenates them in order.
|
| 28 |
|
|
28 |
|
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
// Single buffer sequence
|
31 |
|
// Single buffer sequence
|
| 32 |
|
const_buffer cb( "hello", 5 );
|
32 |
|
const_buffer cb( "hello", 5 );
|
| 33 |
|
std::string s = buffer_to_string( cb ); // "hello"
|
33 |
|
std::string s = buffer_to_string( cb ); // "hello"
|
| 34 |
|
|
34 |
|
|
| 35 |
|
// Multiple buffer sequences (concatenation)
|
35 |
|
// Multiple buffer sequences (concatenation)
|
| 36 |
|
const_buffer b1( "hello", 5 );
|
36 |
|
const_buffer b1( "hello", 5 );
|
| 37 |
|
const_buffer b2( " world", 6 );
|
37 |
|
const_buffer b2( " world", 6 );
|
| 38 |
|
std::string s = buffer_to_string( b1, b2 ); // "hello world"
|
38 |
|
std::string s = buffer_to_string( b1, b2 ); // "hello world"
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
|
42 |
|
|
| 43 |
|
auto [b1, b2] = co_await bg.next();
|
43 |
|
auto [b1, b2] = co_await bg.next();
|
| 44 |
|
BOOST_TEST_EQ( buffer_to_string( b1, b2 ), "hello" );
|
44 |
|
BOOST_TEST_EQ( buffer_to_string( b1, b2 ), "hello" );
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
@param bufs One or more buffer sequences to concatenate.
|
48 |
|
@param bufs One or more buffer sequences to concatenate.
|
| 49 |
|
|
49 |
|
|
| 50 |
|
@return A string containing all bytes from the buffer sequences.
|
50 |
|
@return A string containing all bytes from the buffer sequences.
|
| 51 |
|
|
51 |
|
|
| 52 |
|
template<ConstBufferSequence... Buffers>
|
52 |
|
template<ConstBufferSequence... Buffers>
|
| 53 |
|
|
53 |
|
|
| 54 |
|
buffer_to_string(Buffers const&... bufs)
|
54 |
|
buffer_to_string(Buffers const&... bufs)
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
result.reserve((buffer_size(bufs) + ...));
|
57 |
|
result.reserve((buffer_size(bufs) + ...));
|
| 58 |
|
auto append = [&](auto const& bs) {
|
58 |
|
auto append = [&](auto const& bs) {
|
| 59 |
|
|
59 |
|
|
| 60 |
|
for(auto it = begin(bs); it != e; ++it)
|
60 |
|
for(auto it = begin(bs); it != e; ++it)
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
|
63 |
|
|
| 64 |
|
static_cast<char const*>(b.data()),
|
64 |
|
static_cast<char const*>(b.data()),
|
| 65 |
|
|
65 |
|
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
|
70 |
|
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
|
73 |
|
|
| 74 |
|
|
74 |
|
|
| 75 |
|
|
75 |
|
|
| 76 |
|
|
76 |
|
|