[PATCH] D85698: [ADT] Add an version of llvm::join that takes a callable for non-string iterable
Yuanfang Chen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 10 18:29:32 PDT 2020
ychen updated this revision to Diff 284544.
ychen added a comment.
- update test
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D85698/new/
https://reviews.llvm.org/D85698
Files:
llvm/include/llvm/ADT/StringExtras.h
llvm/unittests/ADT/StringExtrasTest.cpp
Index: llvm/unittests/ADT/StringExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/StringExtrasTest.cpp
+++ llvm/unittests/ADT/StringExtrasTest.cpp
@@ -47,6 +47,17 @@
Items = {"foo", "bar", "baz"};
EXPECT_EQ("foo <sep> bar <sep> baz",
join(Items.begin(), Items.end(), " <sep> "));
+
+ std::vector<unsigned> Integers = {1, 2, 3};
+ EXPECT_EQ("1 <sep> 2 <sep> 3",
+ join(Integers.begin(), Integers.end(), " <sep> ",
+ [](auto I) { return StringRef(utostr(I)); }));
+
+ Integers = {4, 5, 6};
+ EXPECT_EQ("4 <sep> 5 <sep> 6",
+ join(Integers, " <sep> ", [](auto I) { return utostr(I); }));
+ EXPECT_EQ("4 <sep> 5 <sep> 6", join(std::move(Integers), " <sep> ",
+ [](auto I) { return utostr(I); }));
}
TEST(StringExtrasTest, JoinItems) {
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -312,23 +312,36 @@
std::string convertToCamelFromSnakeCase(StringRef input,
bool capitalizeFirst = false);
-namespace detail {
-
-template <typename IteratorT>
-inline std::string join_impl(IteratorT Begin, IteratorT End,
- StringRef Separator, std::input_iterator_tag) {
+template <typename IteratorT, typename UnaryFunction>
+inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator,
+ UnaryFunction F) {
std::string S;
if (Begin == End)
return S;
- S += (*Begin);
+ S += F(*Begin);
while (++Begin != End) {
S += Separator;
- S += (*Begin);
+ S += F(*Begin);
}
return S;
}
+/// Joins the strings in the range [R.begin(), R.end()), adding Separator
+/// between the elements.
+template <typename Range, typename UnaryFunction>
+inline std::string join(Range &&R, StringRef Separator, UnaryFunction F) {
+ return join(R.begin(), R.end(), Separator, F);
+}
+
+namespace detail {
+
+template <typename IteratorT>
+inline std::string join_impl(IteratorT Begin, IteratorT End,
+ StringRef Separator, std::input_iterator_tag) {
+ return join(Begin, End, Separator, [](auto &&S) { return std::forward(S); });
+}
+
template <typename IteratorT>
inline std::string join_impl(IteratorT Begin, IteratorT End,
StringRef Separator, std::forward_iterator_tag) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85698.284544.patch
Type: text/x-patch
Size: 2550 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200811/0a787b9a/attachment.bin>
More information about the llvm-commits
mailing list