2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
9 #define BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
11 #include <boost/asio/buffer.hpp>
20 class chunk_encode_delim
22 boost::asio::const_buffer cb_;
24 // Storage for the longest hex string we might need, plus delimiters.
25 std::array<char, 2 * sizeof(std::size_t) + 2> buf_;
27 template<class = void>
29 copy(chunk_encode_delim const& other);
31 template<class = void>
35 template<class OutIter>
38 to_hex(OutIter last, std::size_t n)
47 *--last = "0123456789abcdef"[n&0xf];
54 using value_type = boost::asio::const_buffer;
56 using const_iterator = value_type const*;
58 chunk_encode_delim(chunk_encode_delim const& other)
64 chunk_encode_delim(std::size_t n)
85 copy(chunk_encode_delim const& other)
88 boost::asio::buffer_size(other.cb_);
90 cb_ = boost::asio::const_buffer(
91 &buf_[buf_.size() - n], n);
99 buf_[buf_.size() - 2] = '\r';
100 buf_[buf_.size() - 1] = '\n';
101 auto it = to_hex(buf_.end() - 2, n);
102 cb_ = boost::asio::const_buffer{&*it,
103 static_cast<std::size_t>(
104 std::distance(it, buf_.end()))};