From: Chip Turner Date: Mon, 8 Jul 2013 19:36:53 +0000 (-0700) Subject: Add initializer_list support to folly::join X-Git-Tag: v0.22.0~926 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=de064c41969e21fe5fc0ee96cd7496aa999a8701;p=folly.git Add initializer_list support to folly::join 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 --- diff --git a/folly/String.h b/folly/String.h index 93a4c658..d706a032 100644 --- a/folly/String.h +++ b/folly/String.h @@ -443,6 +443,13 @@ void join(const Delim& delimiter, join(delimiter, container.begin(), container.end(), output); } +template +void join(const Delim& delimiter, + const std::initializer_list& values, + String& output) { + join(delimiter, values.begin(), values.end(), output); +} + template std::string join(const Delim& delimiter, const Container& container) { @@ -451,6 +458,14 @@ std::string join(const Delim& delimiter, return output; } +template +std::string join(const Delim& delimiter, + const std::initializer_list& 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 diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 6a9e2e99..b0ff972b 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -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) {