}
inline char prepareDelim(char c) { return c; }
+template<bool exact,
+ class Delim>
+bool splitFixed(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& out) {
+ if (exact && UNLIKELY(std::string::npos != input.find(delimiter))) {
+ return false;
+ }
+ out = input;
+ return true;
+}
+
+template<bool exact,
+ class Delim,
+ class... StringPieces>
+bool splitFixed(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& outHead,
+ StringPieces&... outTail) {
+ size_t cut = input.find(delimiter);
+ if (UNLIKELY(cut == std::string::npos)) {
+ return false;
+ }
+ StringPiece head(input.begin(), input.begin() + cut);
+ StringPiece tail(input.begin() + cut + detail::delimSize(delimiter),
+ input.end());
+ if (LIKELY(splitFixed<exact>(delimiter, tail, outTail...))) {
+ outHead = head;
+ return true;
+ }
+ return false;
+}
+
}
//////////////////////////////////////////////////////////////////////
ignoreEmpty);
}
+template<bool exact,
+ class Delim,
+ class... StringPieces>
+bool split(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& outHead,
+ StringPieces&... outTail) {
+ return detail::splitFixed<exact>(
+ detail::prepareDelim(delimiter),
+ input,
+ outHead,
+ outTail...);
+}
+
namespace detail {
template <class Iterator>
OutputIterator out,
bool ignoreEmpty = false);
+/*
+ * Split a string into a fixed number of pieces by delimiter. Returns 'true' if
+ * the fields were all successfully populated.
+ *
+ * Example:
+ *
+ * folly::StringPiece name, key, value;
+ * if (folly::split('\t', line, name, key, value))
+ * ...
+ *
+ * The 'exact' template paremeter specifies how the function behaves when too
+ * many fields are present in the input string. When 'exact' is set to its
+ * default value of 'true', a call to split will fail if the number of fields in
+ * the input string does not exactly match the number of output parameters
+ * passed. If 'exact' is overridden to 'false', all remaining fields will be
+ * stored, unsplit, in the last field, as shown below:
+ *
+ * folly::StringPiece x, y.
+ * if (folly::split<false>(':', "a:b:c", x, y))
+ * assert(x == "a" && y == "b:c");
+ */
+template<bool exact = true,
+ class Delim,
+ class... StringPieces>
+bool split(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& outHead,
+ StringPieces&... outTail);
+
/*
* Join list of tokens.
*
piecesTest<folly::fbvector>();
}
+TEST(Split, fixed) {
+ StringPiece a, b, c, d;
+
+ EXPECT_TRUE(folly::split<false>('.', "a.b.c.d", a, b, c, d));
+ EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
+ EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
+ EXPECT_TRUE(folly::split<false>('.', "a", a));
+
+ EXPECT_TRUE(folly::split('.', "a.b.c.d", a, b, c, d));
+ EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
+ EXPECT_TRUE(folly::split('.', "a.b", a, b));
+ EXPECT_TRUE(folly::split('.', "a", a));
+
+ EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ("b", b);
+ EXPECT_EQ("c", c);
+ EXPECT_FALSE(folly::split<false>('.', "a.b", a, b, c));
+ EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ("b.c", b);
+
+ EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ("b", b);
+ EXPECT_EQ("c", c);
+ EXPECT_FALSE(folly::split('.', "a.b.c", a, b));
+ EXPECT_FALSE(folly::split('.', "a.b", a, b, c));
+
+ EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ("b", b);
+ EXPECT_FALSE(folly::split<false>('.', "a", a, b));
+ EXPECT_TRUE(folly::split<false>('.', "a.b", a));
+ EXPECT_EQ("a.b", a);
+
+ EXPECT_TRUE(folly::split('.', "a.b", a, b));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ("b", b);
+ EXPECT_FALSE(folly::split('.', "a", a, b));
+ EXPECT_FALSE(folly::split('.', "a.b", a));
+}
+
TEST(String, join) {
string output;
}
}
+BENCHMARK(splitOnSingleCharFixed, iters) {
+ static const std::string line = "one:two:three:four";
+ for (int i = 0; i < iters << 4; ++i) {
+ StringPiece a, b, c, d;
+ folly::split(':', line, a, b, c, d);
+ }
+}
+
+BENCHMARK(splitOnSingleCharFixedAllowExtra, iters) {
+ static const std::string line = "one:two:three:four";
+ for (int i = 0; i < iters << 4; ++i) {
+ StringPiece a, b, c, d;
+ folly::split<false>(':', line, a, b, c, d);
+ }
+}
+
BENCHMARK(splitStr, iters) {
static const std::string line = "one-*-two-*-three-*-four";
for (int i = 0; i < iters << 4; ++i) {
}
}
+BENCHMARK(splitStrFixed, iters) {
+ static const std::string line = "one-*-two-*-three-*-four";
+ for (int i = 0; i < iters << 4; ++i) {
+ StringPiece a, b, c, d;
+ folly::split("-*-", line, a, b, c, d);
+ }
+}
+
BENCHMARK(boost_splitOnSingleChar, iters) {
static const std::string line = "one:two:three:four";
for (int i = 0; i < iters << 4; ++i) {