From: Victor Loh Date: Wed, 16 Apr 2014 04:57:23 +0000 (-0700) Subject: Use StringPiece instead of String X-Git-Tag: v0.22.0~603 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=30a71566b2a38c80f979c41d2d5672f7d7a3d244;p=folly.git Use StringPiece instead of String 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 --- diff --git a/folly/io/IOBufQueue.h b/folly/io/IOBufQueue.h index 21dd020e..8ec60611 100644 --- a/folly/io/IOBufQueue.h +++ b/folly/io/IOBufQueue.h @@ -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()); } /** diff --git a/folly/io/test/IOBufQueueTest.cpp b/folly/io/test/IOBufQueueTest.cpp index 858f86d3..0b95986b 100644 --- a/folly/io/test/IOBufQueueTest.cpp +++ b/folly/io/test/IOBufQueueTest.cpp @@ -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")));