Commit 1ed040e7 authored by Keith Adams's avatar Keith Adams Committed by Ajit Banerjee

Add convenience functions to serialize to / deserialize from string

Summary: kma asked

Test Plan: tests added

Reviewed By: andrei.bajenov@fb.com

Subscribers: alandau, bmatheny, mshneer, folly-diffs@

FB internal diff: D1756731

Signature: t1:1756731:1419442987:afdccce166ef1f1e609d8894ea587915f6fea8e7
parent ba499bab
......@@ -273,4 +273,17 @@ void IOBufQueue::clear() {
chainLength_ = 0;
}
void IOBufQueue::appendToString(std::string& out) const {
if (!head_) {
return;
}
auto len =
options_.cacheChainLength ? chainLength_ : head_->computeChainDataLength();
out.reserve(out.size() + len);
for (auto range : *head_) {
out.append(reinterpret_cast<const char*>(range.data()), range.size());
}
}
} // folly
......@@ -261,6 +261,11 @@ class IOBufQueue {
*/
void clear();
/**
* Append the queue to a std::string. Non-destructive.
*/
void appendToString(std::string& out) const;
/** Movable */
IOBufQueue(IOBufQueue&&) noexcept;
IOBufQueue& operator=(IOBufQueue&&);
......
......@@ -378,6 +378,15 @@ TEST(IOBufQueue, PopFirst) {
EXPECT_EQ(0, queue.chainLength());
}
TEST(IOBufQueue, AppendToString) {
IOBufQueue queue;
queue.append("hello ", 6);
queue.append("world", 5);
std::string s;
queue.appendToString(s);
EXPECT_EQ("hello world", s);
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment