]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer: add list::to_str()
authorSage Weil <sage@redhat.com>
Tue, 12 Apr 2016 14:14:56 +0000 (10:14 -0400)
committerSage Weil <sage@redhat.com>
Tue, 12 Apr 2016 15:15:14 +0000 (11:15 -0400)
Signed-off-by: Sage Weil <sage@redhat.com>
src/common/buffer.cc
src/include/buffer.h
src/test/bufferlist.cc

index 63339eaff2f371841409448cf53cb6f0b5994294..0bf259530b155509a1d7c126d0050cc9d7a21041 100644 (file)
@@ -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<ptr>::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())
index 1397ae99709ad792fd1159bc3a487bfd8c5c58c9..c786bf27915a13880af31f0d66421c8d521720f1 100644 (file)
@@ -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,
index 008b8cded84bb911892e4b8cfedd097c0521a210..b25740777682a073a6eef53c0bc5684c2806c261 100644 (file)
@@ -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);