+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(Split, std_string_fixed) {
+ std::string 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(Split, fixed_convert) {
+ StringPiece a, d;
+ int b;
+ double c = 0;
+
+ EXPECT_TRUE(folly::split(':', "a:13:14.7:b", a, b, c, d));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ(13, b);
+ EXPECT_NEAR(14.7, c, 1e-10);
+ EXPECT_EQ("b", d);
+
+ EXPECT_TRUE(folly::split<false>(':', "b:14:15.3:c", a, b, c, d));
+ EXPECT_EQ("b", a);
+ EXPECT_EQ(14, b);
+ EXPECT_NEAR(15.3, c, 1e-10);
+ EXPECT_EQ("c", d);
+
+ EXPECT_FALSE(folly::split(':', "a:13:14.7:b", a, b, d));
+
+ EXPECT_TRUE(folly::split<false>(':', "a:13:14.7:b", a, b, d));
+ EXPECT_EQ("a", a);
+ EXPECT_EQ(13, b);
+ EXPECT_EQ("14.7:b", d);
+
+
+ // Enable verifying that a line only contains one field
+ EXPECT_TRUE(folly::split(' ', "hello", a));
+ EXPECT_FALSE(folly::split(' ', "hello world", a));
+}
+
+namespace my {
+
+enum class Color {
+ Red,
+ Blue,
+};
+
+enum class ColorErrorCode { INVALID_COLOR };
+
+struct ColorError : std::runtime_error {
+ using std::runtime_error::runtime_error;
+};
+
+ColorError makeConversionError(ColorErrorCode, StringPiece sp) {
+ return ColorError("Invalid my::Color representation : " + sp.str());
+}
+
+Expected<StringPiece, ColorErrorCode> parseTo(
+ StringPiece in,
+ Color& out) noexcept {
+ if (in == "R") {
+ out = Color::Red;
+ } else if (in == "B") {
+ out = Color::Blue;
+ } else {
+ return makeUnexpected(ColorErrorCode::INVALID_COLOR);
+ }
+ return StringPiece(in.end(), in.end());
+}
+}
+
+TEST(Split, fixed_convert_custom) {
+ my::Color c1, c2;
+
+ EXPECT_TRUE(folly::split(',', "R,B", c1, c2));
+ EXPECT_EQ(c1, my::Color::Red);
+ EXPECT_EQ(c2, my::Color::Blue);
+
+ EXPECT_THROW(folly::split(',', "B,G", c1, c2), my::ColorError);
+}
+