LCOV - code coverage report
Current view: top level - boost/capy/test - buffer_to_string.hpp (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 91.7 % 12 11
Test Date: 2026-01-30 23:43:15 Functions: 100.0 % 8 8

            Line data    Source code
       1              : //
       2              : // Copyright (c) 2025 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_TEST_BUFFER_TO_STRING_HPP
      11              : #define BOOST_CAPY_TEST_BUFFER_TO_STRING_HPP
      12              : 
      13              : #include <boost/capy/detail/config.hpp>
      14              : #include <boost/capy/buffers.hpp>
      15              : 
      16              : #include <string>
      17              : 
      18              : namespace boost {
      19              : namespace capy {
      20              : namespace test {
      21              : 
      22              : /** Convert one or more buffer sequences to a string.
      23              : 
      24              :     This function concatenates the bytes from all provided buffer
      25              :     sequences into a single string. With a single argument, it
      26              :     converts that buffer sequence to a string. With multiple
      27              :     arguments, it concatenates them in order.
      28              : 
      29              :     @par Example
      30              :     @code
      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              : 
      40              :     // With bufgrind splits
      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              : 
      48              :     @param bufs One or more buffer sequences to concatenate.
      49              : 
      50              :     @return A string containing all bytes from the buffer sequences.
      51              : */
      52              : template<ConstBufferSequence... Buffers>
      53              : std::string
      54         1565 : buffer_to_string(Buffers const&... bufs)
      55              : {
      56         1565 :     std::string result;
      57         1565 :     result.reserve((buffer_size(bufs) + ...));
      58         4039 :     auto append = [&](auto const& bs) {
      59         2474 :         auto const e = end(bs);
      60         4958 :         for(auto it = begin(bs); it != e; ++it)
      61              :         {
      62         2484 :             const_buffer b(*it);
      63         4968 :             result.append(
      64         2484 :                 static_cast<char const*>(b.data()),
      65              :                 b.size());
      66              :         }
      67              :     };
      68         1565 :     (append(bufs), ...);
      69         3130 :     return result;
      70            0 : }
      71              : 
      72              : } // test
      73              : } // capy
      74              : } // boost
      75              : 
      76              : #endif
        

Generated by: LCOV version 2.3