From: Sage Weil Date: Tue, 12 Apr 2016 14:14:56 +0000 (-0400) Subject: buffer: add list::to_str() X-Git-Tag: ses3-milestone4~31^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d56e43950cc4a083d32367746168c2b64e002b42;p=ceph.git buffer: add list::to_str() Signed-off-by: Sage Weil --- diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 63339eaff2f..0bf259530b1 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -1780,6 +1780,19 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; return _buffers.front().c_str(); // good, we're already contiguous. } + string buffer::list::to_str() const { + string s; + s.reserve(length()); + for (std::list::const_iterator p = _buffers.begin(); + p != _buffers.end(); + ++p) { + if (p->length()) { + s.append(p->c_str(), p->length()); + } + } + return s; + } + char *buffer::list::get_contiguous(unsigned orig_off, unsigned len) { if (orig_off + len > length()) diff --git a/src/include/buffer.h b/src/include/buffer.h index 1397ae99709..c786bf27915 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -558,6 +558,8 @@ namespace buffer CEPH_BUFFER_API { */ const char& operator[](unsigned n) const; char *c_str(); + std::string to_str() const; + void substr_of(const list& other, unsigned off, unsigned len); /// return a pointer to a contiguous extent of the buffer, diff --git a/src/test/bufferlist.cc b/src/test/bufferlist.cc index 008b8cded84..b2574077768 100644 --- a/src/test/bufferlist.cc +++ b/src/test/bufferlist.cc @@ -1390,6 +1390,24 @@ TEST(BufferList, buffers) { ASSERT_EQ((unsigned)1, bl.get_num_buffers()); } +TEST(BufferList, to_str) { + { + bufferlist bl; + bl.append("foo"); + ASSERT_EQ(bl.to_str(), string("foo")); + } + { + bufferptr a("foobarbaz", 9); + bufferptr b("123456789", 9); + bufferptr c("ABCDEFGHI", 9); + bufferlist bl; + bl.append(a); + bl.append(b); + bl.append(c); + ASSERT_EQ(bl.to_str(), string("foobarbaz123456789ABCDEFGHI")); + } +} + TEST(BufferList, get_contiguous) { { bufferptr a("foobarbaz", 9);