Skip to content

Commit

Permalink
[STLExtras] Allow for non-member begin/end in append_range
Browse files Browse the repository at this point in the history
This makes `append_range` useable with, C arrays and types with custom
`begin`/`end` functions.

Reviewed By: kazu

Differential Revision: https://reviews.llvm.org/D144420
  • Loading branch information
kuhar committed Feb 21, 2023
1 parent 25dbf8f commit 93a971c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/ADT/STLExtras.h
Expand Up @@ -2014,7 +2014,7 @@ void erase_value(Container &C, ValueType V) {
/// 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
Expand Down
23 changes: 18 additions & 5 deletions llvm/unittests/ADT/STLExtrasTest.cpp
Expand Up @@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/StringRef.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -353,12 +355,23 @@ TEST(STLExtrasTest, EraseIf) {
}

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_THAT(Str, ElementsAre('a', 'b', 'c', '\0'));
append_range(Str, "def");
EXPECT_THAT(Str, ElementsAre('a', 'b', 'c', '\0', 'd', 'e', 'f', '\0'));
}

namespace some_namespace {
Expand Down

0 comments on commit 93a971c

Please sign in to comment.