ConstBufferSequence mandates pointer equivalence

fix #865
This commit is contained in:
Vinnie Falco
2017-11-03 19:34:30 -07:00
parent 56f0b63e4d
commit d9d2b23b57
2 changed files with 39 additions and 48 deletions

View File

@ -1,3 +1,9 @@
Version 137:
* ConstBufferSequence mandates pointer equivalence
--------------------------------------------------------------------------------
Version 136: Version 136:
* Tidy up message doc image * Tidy up message doc image

View File

@ -16,6 +16,7 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cstddef> #include <cstddef>
#include <memory>
namespace boost { namespace boost {
namespace beast { namespace beast {
@ -67,46 +68,44 @@ struct is_chunk_extensions<T, beast::detail::void_t<decltype(
*/ */
class chunk_size class chunk_size
{ {
public: template<class OutIter>
// Storage for the longest hex string we might need static
class value_type OutIter
to_hex(OutIter last, std::size_t n)
{ {
friend class chunk_size; if(n == 0)
// First byte holds the length
char buf_[1 + 2 * sizeof(std::size_t)];
template<class = void>
void
prepare(std::size_t n);
template<class OutIter>
static
OutIter
to_hex(OutIter last, std::size_t n)
{ {
if(n == 0) *--last = '0';
{
*--last = '0';
return last;
}
while(n)
{
*--last = "0123456789abcdef"[n&0xf];
n>>=4;
}
return last; return last;
} }
public: while(n)
operator
boost::asio::const_buffer() const
{ {
return { *--last = "0123456789abcdef"[n&0xf];
buf_ + sizeof(buf_) - buf_[0], n>>=4;
static_cast<unsigned>(buf_[0])}; }
return last;
}
struct sequence
{
boost::asio::const_buffer b;
char data[1 + 2 * sizeof(std::size_t)];
explicit
sequence(std::size_t n)
{
char* it0 = data + sizeof(data);
auto it = to_hex(it0, n);
b = {it,
static_cast<std::size_t>(it0 - it)};
} }
}; };
std::shared_ptr<sequence> sp_;
public:
using value_type = boost::asio::const_buffer;
using const_iterator = value_type const*; using const_iterator = value_type const*;
chunk_size(chunk_size const& other) = default; chunk_size(chunk_size const& other) = default;
@ -116,14 +115,14 @@ public:
@param n The number of octets in this chunk. @param n The number of octets in this chunk.
*/ */
chunk_size(std::size_t n) chunk_size(std::size_t n)
: sp_(std::make_shared<sequence>(n))
{ {
value_.prepare(n);
} }
const_iterator const_iterator
begin() const begin() const
{ {
return &value_; return &sp_->b;
} }
const_iterator const_iterator
@ -131,22 +130,8 @@ public:
{ {
return begin() + 1; return begin() + 1;
} }
private:
value_type value_;
}; };
template<class>
void
chunk_size::
value_type::
prepare(std::size_t n)
{
auto const last = &buf_[sizeof(buf_)];
auto it = to_hex(last, n);
buf_[0] = static_cast<char>(last - it);
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/// Returns a buffer sequence holding a CRLF for chunk encoding /// Returns a buffer sequence holding a CRLF for chunk encoding