From de064c41969e21fe5fc0ee96cd7496aa999a8701 Mon Sep 17 00:00:00 2001 From: Chip Turner Date: Mon, 8 Jul 2013 12:36:53 -0700 Subject: [PATCH] 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 --- folly/String.h | 15 +++++++++++++++ folly/test/StringTest.cpp | 3 +++ 2 files changed, 18 insertions(+) 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) { -- 2.34.1