From 096f422b4a079a75cb7230b7377d0c491c24335f Mon Sep 17 00:00:00 2001 From: weizili Date: Wed, 5 Apr 2017 11:19:27 +0800 Subject: [PATCH] Add Buffer::EnsureWritableBytes. Fix Buffer::ToText \0 bug --- evpp/buffer.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/evpp/buffer.h b/evpp/buffer.h index 69ea975b9..f4c33d019 100644 --- a/evpp/buffer.h +++ b/evpp/buffer.h @@ -86,10 +86,19 @@ class EVPP_EXPORT Buffer { grow(len + reserved_prepend_size_); } - // ToText appends char '\0' to buffer to convert the underlying data to a c string text. + // Make sure there is enough memory space to append more data with length len + void EnsureWritableBytes(size_t len) { + if (WritableBytes() < len) { + grow(len); + } + + assert(WritableBytes() >= len); + } + + // ToText appends char '\0' to buffer to convert the underlying data to a c-style string text. // It will not change the length of buffer. void ToText() { - AppendInt8('0'); + AppendInt8('\0'); UnwriteBytes(1); } @@ -107,7 +116,7 @@ class EVPP_EXPORT Buffer { // Write public: void Write(const void* /*restrict*/ d, size_t len) { - ensureWritableBytes(len); + EnsureWritableBytes(len); memcpy(WriteBegin(), d, len); assert(write_index_ + len <= capacity_); write_index_ += len; @@ -390,14 +399,6 @@ class EVPP_EXPORT Buffer { return buffer_; } - void ensureWritableBytes(size_t len) { - if (WritableBytes() < len) { - grow(len); - } - - assert(WritableBytes() >= len); - } - void grow(size_t len) { if (WritableBytes() + PrependableBytes() < len + reserved_prepend_size_) { //grow the capacity