Skip to content

Commit

Permalink
Add Buffer::EnsureWritableBytes. Fix Buffer::ToText \0 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zieckey committed Apr 5, 2017
1 parent dd2e409 commit 096f422
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions evpp/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 096f422

Please sign in to comment.