Commit 76d60885 authored by Tom Jackson's avatar Tom Jackson Committed by Jordan DeLong

Fix unsplit(", ")

Summary: Otherwise you get errors like `error: array used as initializer ./folly/experimental/StringGen-inl.h: In constructor ‘folly::gen::detail::UnsplitBuffer<Delimiter, OutputBuffer>::UnsplitBuffer(const Delimiter&, OutputBuffer*) [with Delimiter = char [3] ...]`, since literal strings bind as reference to fixed-length character arrays. Providing an explicit overload for `const char*` fixes this.

Test Plan: Unit tests

Reviewed By: tulloch@fb.com

FB internal diff: D737117
parent 1bf1654c
...@@ -73,6 +73,12 @@ Unsplit unsplit(const Delimiter& delimiter) { ...@@ -73,6 +73,12 @@ Unsplit unsplit(const Delimiter& delimiter) {
return Unsplit(delimiter); return Unsplit(delimiter);
} }
template<class Output = folly::fbstring,
class Unsplit = detail::Unsplit<fbstring, Output>>
Unsplit unsplit(const char* delimiter) {
return Unsplit(delimiter);
}
/* /*
* Joins a sequence of tokens into a string, appending them to the output * Joins a sequence of tokens into a string, appending them to the output
* buffer. If the output buffer is empty, an initial delimiter will not be * buffer. If the output buffer is empty, an initial delimiter will not be
...@@ -94,6 +100,12 @@ UnsplitBuffer unsplit(const Delimiter& delimiter, OutputBuffer* outputBuffer) { ...@@ -94,6 +100,12 @@ UnsplitBuffer unsplit(const Delimiter& delimiter, OutputBuffer* outputBuffer) {
return UnsplitBuffer(delimiter, outputBuffer); return UnsplitBuffer(delimiter, outputBuffer);
} }
template<class OutputBuffer,
class UnsplitBuffer = detail::UnsplitBuffer<fbstring, OutputBuffer>>
UnsplitBuffer unsplit(const char* delimiter, OutputBuffer* outputBuffer) {
return UnsplitBuffer(delimiter, outputBuffer);
}
} // namespace gen } // namespace gen
} // namespace folly } // namespace folly
......
...@@ -753,18 +753,27 @@ TEST(StringGen, Unsplit) { ...@@ -753,18 +753,27 @@ TEST(StringGen, Unsplit) {
split(s, ',') | unsplit(',', &buffer); split(s, ',') | unsplit(',', &buffer);
auto expected = folly::to<folly::fbstring>( auto expected = folly::to<folly::fbstring>(
"asdf", s.empty() ? "" : ",", s); "asdf", s.empty() ? "" : ",", s);
EXPECT_EQ(buffer, expected); EXPECT_EQ(expected, buffer);
}; };
auto emptyBuffer = [](const StringPiece& s) { auto emptyBuffer = [](const StringPiece& s) {
std::string buffer; std::string buffer;
split(s, ',') | unsplit(',', &buffer); split(s, ',') | unsplit(',', &buffer);
EXPECT_EQ(s, buffer);
};
auto stringDelim = [](const StringPiece& s) {
EXPECT_EQ(s, split(s, ',') | unsplit(","));
std::string buffer;
split(s, ',') | unsplit(",", &buffer);
EXPECT_EQ(buffer, s); EXPECT_EQ(buffer, s);
}; };
runUnsplitSuite(basicFn); runUnsplitSuite(basicFn);
runUnsplitSuite(existingBuffer); runUnsplitSuite(existingBuffer);
runUnsplitSuite(emptyBuffer); runUnsplitSuite(emptyBuffer);
runUnsplitSuite(stringDelim);
EXPECT_EQ("1, 2, 3", seq(1, 3) | unsplit(", "));
} }
TEST(FileGen, ByLine) { TEST(FileGen, ByLine) {
......
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