buffers_to_string.hpp 1.7 KB

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