Add convenience method for folly::join
authorAndrew Chalfant <chalfant@fb.com>
Fri, 12 Dec 2014 02:18:26 +0000 (18:18 -0800)
committerJoelMarcey <joelm@fb.com>
Thu, 18 Dec 2014 20:29:40 +0000 (12:29 -0800)
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
folly/test/StringTest.cpp

index efbda2f1bccb5b59db65082dacdc3046bdd63b3b..03d11fe5e82ddb57ea9d860d6f7095ffe8733b65 100644 (file)
@@ -523,6 +523,17 @@ std::string join(const Delim& delimiter,
   return output;
 }
 
+template <class Delim,
+          class Iterator,
+          typename std::enable_if<std::is_same<
+              typename std::iterator_traits<Iterator>::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'].
index 9b4dc8da94b23acb406451c255ec6195936e1bd6..80b9a30d217e50c03fb39618d0015ec92bc6e258 100644 (file)
@@ -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) {