[PATCH] D144420: [STLExtras] Allow for non-member `begin`/`end` in `append_range`

Jakub Kuderski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 20 11:42:38 PST 2023


kuhar created this revision.
kuhar added reviewers: dblaikie, kazu, beanz.
Herald added a subscriber: hanchung.
Herald added a project: All.
kuhar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This makes `append_range` useable with, C arrays and types with custom
`begin`/`end` functions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144420

Files:
  llvm/include/llvm/ADT/STLExtras.h
  llvm/unittests/ADT/STLExtrasTest.cpp


Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/Sequence.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
@@ -353,12 +354,23 @@
 }
 
 TEST(STLExtrasTest, AppendRange) {
-  auto AppendVals = {3};
   std::vector<int> V = {1, 2};
-  append_range(V, AppendVals);
-  EXPECT_EQ(1, V[0]);
-  EXPECT_EQ(2, V[1]);
-  EXPECT_EQ(3, V[2]);
+  auto AppendVals1 = {3};
+  append_range(V, AppendVals1);
+  EXPECT_THAT(V, ElementsAre(1, 2, 3));
+
+  int AppendVals2[] = {4, 5};
+  append_range(V, AppendVals2);
+  EXPECT_THAT(V, ElementsAre(1, 2, 3, 4, 5));
+
+  append_range(V, llvm::seq(6, 8));
+  EXPECT_THAT(V, ElementsAre(1, 2, 3, 4, 5, 6, 7));
+
+  std::string Str;
+  append_range(Str, "abc");
+  EXPECT_EQ(Str, "abc");
+  append_range(Str, "def");
+  EXPECT_EQ(Str, "abcdef");
 }
 
 namespace some_namespace {
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -2012,7 +2012,7 @@
 /// C.insert(C.end(), R.begin(), R.end());
 template <typename Container, typename Range>
 inline void append_range(Container &C, Range &&R) {
-  C.insert(C.end(), R.begin(), R.end());
+  C.insert(C.end(), adl_begin(R), adl_end(R));
 }
 
 /// Given a sequence container Cont, replace the range [ContIt, ContEnd) with


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144420.498920.patch
Type: text/x-patch
Size: 1634 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230220/e57c04df/attachment.bin>


More information about the llvm-commits mailing list