91.67% Lines (11/12) 100.00% Functions (2/2)
TLA Baseline Branch
Line Hits Code Line Hits Code
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   #include <string> 16   #include <string>
17   17  
18   namespace boost { 18   namespace boost {
19   namespace capy { 19   namespace capy {
20   namespace test { 20   namespace test {
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   @par Example 29   @par Example
30 - @code 30 + @par !example example
31 - // Single buffer sequence  
32 - const_buffer cb( "hello", 5 );  
33 - std::string s = buffer_to_string( cb ); // "hello"  
34 -  
35 - // Multiple buffer sequences (concatenation)  
36 - const_buffer b1( "hello", 5 );  
37 - const_buffer b2( " world", 6 );  
38 - std::string s = buffer_to_string( b1, b2 ); // "hello world"  
39 - // With bufgrind splits: each half is itself a buffer sequence,  
40 - // so pass it directly -- there is no .data() to unwrap.  
41 - bufgrind bg( cb );  
42 - while( bg ) {  
43 - auto [b1, b2] = co_await bg.next();  
44 - BOOST_TEST_EQ( buffer_to_string( b1, b2 ), "hello" );  
45 - }  
46 - @endcode  
47   31  
48   32  
49   @param bufs One or more buffer sequences to concatenate. 33   @param bufs One or more buffer sequences to concatenate.
50   34  
51   @return A string containing all bytes from the buffer sequences. 35   @return A string containing all bytes from the buffer sequences.
52   */ 36   */
53   template<ConstBufferSequence... Buffers> 37   template<ConstBufferSequence... Buffers>
54   std::string 38   std::string
HITCBC 55   176 buffer_to_string(Buffers const&... bufs) 39   176 buffer_to_string(Buffers const&... bufs)
56   { 40   {
HITCBC 57   176 std::string result; 41   176 std::string result;
HITCBC 58   176 result.reserve((buffer_size(bufs) + ...)); 42   176 result.reserve((buffer_size(bufs) + ...));
HITCBC 59   488 auto append = [&](auto const& bs) { 43   488 auto append = [&](auto const& bs) {
HITCBC 60   312 auto const e = end(bs); 44   312 auto const e = end(bs);
HITCBC 61   634 for(auto it = begin(bs); it != e; ++it) 45   634 for(auto it = begin(bs); it != e; ++it)
62   { 46   {
HITCBC 63   319 const_buffer b(*it); 47   319 const_buffer b(*it);
HITCBC 64   644 result.append( 48   644 result.append(
HITCBC 65   322 static_cast<char const*>(b.data()), 49   322 static_cast<char const*>(b.data()),
66   b.size()); 50   b.size());
67   } 51   }
68   }; 52   };
HITCBC 69   176 (append(bufs), ...); 53   176 (append(bufs), ...);
HITCBC 70   352 return result; 54   352 return result;
MISUBC 71   } 55   }
72   56  
73   } // test 57   } // test
74   } // capy 58   } // capy
75   } // boost 59   } // boost
76   60  
77   #endif 61   #endif