From a33734e4d10197cf085bacf2c3c32ccd9fb0e119 Mon Sep 17 00:00:00 2001 From: Andrew Chalfant Date: Thu, 11 Dec 2014 18:18:26 -0800 Subject: [PATCH] Add convenience method for folly::join Summary: While folly has join methods that return a string (and thus don't need a string referenced passed), it does not support this idiom with iterators. This diff adds support for calls like folly::join("/", itr.begin(), itr.end() - 1). Test Plan: Unit test Reviewed By: njormrod@fb.com Subscribers: philipp, trunkagent, njormrod, folly-diffs@, yfeldblum FB internal diff: D1702892 Tasks: 5691439 Signature: t1:1702892:1418344206:9c1736f5d8e41be1df71a415e3803fe846b387b7 --- folly/String.h | 11 +++++++++++ folly/test/StringTest.cpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/folly/String.h b/folly/String.h index efbda2f1..03d11fe5 100644 --- a/folly/String.h +++ b/folly/String.h @@ -523,6 +523,17 @@ std::string join(const Delim& delimiter, return output; } +template ::iterator_category, + std::random_access_iterator_tag>::value>::type* = nullptr> +std::string join(const Delim& delimiter, Iterator begin, Iterator end) { + std::string output; + join(delimiter, begin, end, output); + return output; +} + /** * Returns a subpiece with all whitespace removed from the front of @sp. * Whitespace means any of [' ', '\n', '\r', '\t']. diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 9b4dc8da..80b9a30d 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -1072,6 +1072,9 @@ TEST(String, join) { join("_", { "", "f", "a", "c", "e", "b", "o", "o", "k", "" }, output); EXPECT_EQ(output, "_f_a_c_e_b_o_o_k_"); + + output = join("", input3.begin(), input3.end()); + EXPECT_EQ(output, "facebook"); } TEST(String, hexlify) { -- 2.34.1