Use StringPiece instead of String
authorVictor Loh <vloh@fb.com>
Wed, 16 Apr 2014 04:57:23 +0000 (21:57 -0700)
committerSara Golemon <sgolemon@fb.com>
Fri, 18 Apr 2014 19:04:15 +0000 (12:04 -0700)
Summary:
Using StringPiece makes it easier than std::string

Facebook: Motivation for this diff is found in D1253595

Test Plan:
Added new unittest

fbconfig folly/io/test && fbmake runtests_opt

Reviewed By: simpkins@fb.com

FB internal diff: D1276185

folly/io/IOBufQueue.h
folly/io/test/IOBufQueueTest.cpp

index 21dd020e4ee4531067b473b85cf459481b397f70..8ec60611bf70da8f4352c9ea92e31daaefaacaee 100644 (file)
@@ -98,8 +98,8 @@ class IOBufQueue {
    * Copy a string to the end of this queue.
    * The caller retains ownership of the source data.
    */
-  void append(const std::string& buf) {
-    append(buf.data(), buf.length());
+  void append(StringPiece sp) {
+    append(sp.data(), sp.size());
   }
 
   /**
index 858f86d3a3ae7b825b1a47375ba2800facbc0997..0b95986bab956cc4b2e182f2fc35238ab8ad3524 100644 (file)
@@ -107,6 +107,21 @@ TEST(IOBufQueue, Append2) {
   EXPECT_EQ(nullptr, queue2.front());
 }
 
+TEST(IOBufQueue, AppendStringPiece) {
+  std::string s("Hello, World");
+  IOBufQueue queue(clOptions);
+  IOBufQueue queue2(clOptions);
+  queue.append(s.data(), s.length());
+  queue2.append(s);
+  checkConsistency(queue);
+  checkConsistency(queue2);
+  const IOBuf* chain = queue.front();
+  const IOBuf* chain2 = queue2.front();
+  EXPECT_EQ(s.length(), chain->computeChainDataLength());
+  EXPECT_EQ(s.length(), chain2->computeChainDataLength());
+  EXPECT_EQ(0, memcmp(chain->data(), chain2->data(), s.length()));
+}
+
 TEST(IOBufQueue, Split) {
   IOBufQueue queue(clOptions);
   queue.append(stringToIOBuf(SCL("Hello")));