Add initializer_list support to folly::join
authorChip Turner <chip@fb.com>
Mon, 8 Jul 2013 19:36:53 +0000 (12:36 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 18 Jul 2013 18:55:35 +0000 (11:55 -0700)
Summary:
Apparently the templates don't like to match the initlializer
list, so this needed to be added.  This allows for things like:

auto s = join(":", {val1, val2, val3, val4, val4});

Test Plan: run tests

Reviewed By: andrei.alexandrescu@fb.com

FB internal diff: D878032

folly/String.h
folly/test/StringTest.cpp

index 93a4c658363c145046bc5c83f80e20de2d7e0a94..d706a03215b2f3b121fe9d2a664c4eaba618d3c9 100644 (file)
@@ -443,6 +443,13 @@ void join(const Delim& delimiter,
   join(delimiter, container.begin(), container.end(), output);
 }
 
+template <class Delim, class Value, class String>
+void join(const Delim& delimiter,
+          const std::initializer_list<Value>& values,
+          String& output) {
+  join(delimiter, values.begin(), values.end(), output);
+}
+
 template <class Delim, class Container>
 std::string join(const Delim& delimiter,
                  const Container& container) {
@@ -451,6 +458,14 @@ std::string join(const Delim& delimiter,
   return output;
 }
 
+template <class Delim, class Value>
+std::string join(const Delim& delimiter,
+                 const std::initializer_list<Value>& values) {
+  std::string output;
+  join(delimiter, values.begin(), values.end(), output);
+  return output;
+}
+
 } // namespace folly
 
 // Hash functions for string and fbstring usable with e.g. hash_map
index 6a9e2e992fe8f7064f309aff5c265a897e2cbfa6..b0ff972bae0134716dd1f82f8665b25f55c67fd8 100644 (file)
@@ -823,6 +823,9 @@ TEST(String, join) {
   auto input3 = { 'f', 'a', 'c', 'e', 'b', 'o', 'o', 'k' };
   join("", input3, output);
   EXPECT_EQ(output, "facebook");
+
+  join("_", { "", "f", "a", "c", "e", "b", "o", "o", "k", "" }, output);
+  EXPECT_EQ(output, "_f_a_c_e_b_o_o_k_");
 }
 
 TEST(String, hexlify) {