1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_BUFFERS_TO_STRING_HPP
11 #define BOOST_BEAST_BUFFERS_TO_STRING_HPP
12 
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/buffer_traits.hpp>
15 #include <boost/beast/core/buffers_range.hpp>
16 #include <boost/asio/buffer.hpp>
17 #include <string>
18 
19 namespace boost {
20 namespace beast {
21 
22 /** Return a string representing the contents of a buffer sequence.
23 
24     This function returns a string representing an entire buffer
25     sequence. Nulls and unprintable characters in the buffer
26     sequence are inserted to the resulting string as-is. No
27     character conversions are performed.
28 
29     @param buffers The buffer sequence to convert
30 
31     @par Example
32 
33     This function writes a buffer sequence converted to a string
34     to `std::cout`.
35 
36     @code
37     template<class ConstBufferSequence>
38     void print(ConstBufferSequence const& buffers)
39     {
40         std::cout << buffers_to_string(buffers) << std::endl;
41     }
42     @endcode
43 */
44 template<class ConstBufferSequence>
45 std::string
buffers_to_string(ConstBufferSequence const & buffers)46 buffers_to_string(ConstBufferSequence const& buffers)
47 {
48     static_assert(
49         net::is_const_buffer_sequence<ConstBufferSequence>::value,
50         "ConstBufferSequence type requirements not met");
51     std::string result;
52     result.reserve(buffer_bytes(buffers));
53     for(auto const buffer : buffers_range_ref(buffers))
54         result.append(static_cast<char const*>(
55             buffer.data()), buffer.size());
56     return result;
57 }
58 
59 } // beast
60 } // boost
61 
62 #endif
63