Add BufferWriter::AppendString

Change-Id: Id8a7d09fd76eeb95600e108c70c2e38ddd18a7ab
This commit is contained in:
KongQun Yang 2018-05-07 15:54:59 -07:00
parent 9a9f7d58b2
commit 440ba442f6
3 changed files with 17 additions and 0 deletions

View File

@ -55,6 +55,10 @@ void BufferWriter::AppendVector(const std::vector<uint8_t>& v) {
buf_.insert(buf_.end(), v.begin(), v.end()); buf_.insert(buf_.end(), v.begin(), v.end());
} }
void BufferWriter::AppendString(const std::string& s) {
buf_.insert(buf_.end(), s.begin(), s.end());
}
void BufferWriter::AppendArray(const uint8_t* buf, size_t size) { void BufferWriter::AppendArray(const uint8_t* buf, size_t size) {
buf_.insert(buf_.end(), buf, buf + size); buf_.insert(buf_.end(), buf, buf + size);
} }

View File

@ -48,6 +48,7 @@ class BufferWriter {
void AppendNBytes(uint64_t v, size_t num_bytes); void AppendNBytes(uint64_t v, size_t num_bytes);
void AppendVector(const std::vector<uint8_t>& v); void AppendVector(const std::vector<uint8_t>& v);
void AppendString(const std::string& s);
void AppendArray(const uint8_t* buf, size_t size); void AppendArray(const uint8_t* buf, size_t size);
void AppendBuffer(const BufferWriter& buffer); void AppendBuffer(const BufferWriter& buffer);

View File

@ -110,6 +110,18 @@ TEST_F(BufferWriterTest, AppendVector) {
ASSERT_EQ(v, data_read); ASSERT_EQ(v, data_read);
} }
TEST_F(BufferWriterTest, AppendString) {
const char kTestData[] = "test_data";
writer_->AppendString(kTestData);
// -1 to remove the null terminating character.
ASSERT_EQ(strlen(kTestData), writer_->Size());
CreateReader();
std::string data_read;
ASSERT_TRUE(reader_->ReadToString(&data_read, strlen(kTestData)));
ASSERT_EQ(kTestData, data_read);
}
TEST_F(BufferWriterTest, AppendArray) { TEST_F(BufferWriterTest, AppendArray) {
writer_->AppendArray(kuint8Array, sizeof(kuint8Array)); writer_->AppendArray(kuint8Array, sizeof(kuint8Array));
ASSERT_EQ(sizeof(kuint8Array), writer_->Size()); ASSERT_EQ(sizeof(kuint8Array), writer_->Size());