Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit bb0403b

Browse files
author
Zachary Turner
committed
Add StringRef::take_front and StringRef::take_back
Reviewed By: majnemer, rnk Differential Revision: https://reviews.llvm.org/D23965 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280114 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 298afcc commit bb0403b

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

include/llvm/ADT/ArrayRef.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ namespace llvm {
195195
return slice(0, size() - N);
196196
}
197197

198+
/// \brief Keep the first \p N elements of the array.
199+
LLVM_ATTRIBUTE_UNUSED_RESULT
200+
ArrayRef<T> keep_front(size_t N = 1) const {
201+
if (N >= size())
202+
return *this;
203+
return drop_back(size() - N);
204+
}
205+
206+
/// \brief Keep the last \p N elements of the array.
207+
LLVM_ATTRIBUTE_UNUSED_RESULT
208+
ArrayRef<T> keep_back(size_t N = 1) const {
209+
if (N >= size())
210+
return *this;
211+
return drop_front(size() - N);
212+
}
213+
198214
/// @}
199215
/// @name Operator Overloads
200216
/// @{
@@ -321,6 +337,22 @@ namespace llvm {
321337
return slice(0, this->size() - N);
322338
}
323339

340+
/// \brief Drop everything but the first \p N elements of the array.
341+
LLVM_ATTRIBUTE_UNUSED_RESULT
342+
MutableArrayRef<T> keep_front(size_t N = 1) const {
343+
if (N >= this->size())
344+
return *this;
345+
return drop_back(size() - N);
346+
}
347+
348+
/// \brief Drop everything but the last \p N elements of the array.
349+
LLVM_ATTRIBUTE_UNUSED_RESULT
350+
MutableArrayRef<T> keep_back(size_t N = 1) const {
351+
if (N >= this->size())
352+
return *this;
353+
return drop_front(size() - N);
354+
}
355+
324356
/// @}
325357
/// @name Operator Overloads
326358
/// @{

include/llvm/ADT/StringRef.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,28 @@ namespace llvm {
433433
return StringRef(Data + Start, std::min(N, Length - Start));
434434
}
435435

436+
/// Return a StringRef equal to 'this' but with only the first \p N
437+
/// elements remaining. If \p N is greater than the length of the
438+
/// string, the entire string is returned.
439+
LLVM_ATTRIBUTE_ALWAYS_INLINE
440+
LLVM_ATTRIBUTE_UNUSED_RESULT
441+
StringRef take_front(size_t N = 1) const {
442+
if (N >= size())
443+
return *this;
444+
return drop_back(size() - N);
445+
}
446+
447+
/// Return a StringRef equal to 'this' but with only the first \p N
448+
/// elements remaining. If \p N is greater than the length of the
449+
/// string, the entire string is returned.
450+
LLVM_ATTRIBUTE_ALWAYS_INLINE
451+
LLVM_ATTRIBUTE_UNUSED_RESULT
452+
StringRef take_back(size_t N = 1) const {
453+
if (N >= size())
454+
return *this;
455+
return drop_front(size() - N);
456+
}
457+
436458
/// Return a StringRef equal to 'this' but with the first \p N elements
437459
/// dropped.
438460
LLVM_ATTRIBUTE_ALWAYS_INLINE

unittests/ADT/ArrayRefTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ TEST(ArrayRefTest, DropFront) {
8282
EXPECT_EQ(1U, AR3.drop_front(AR3.size() - 1).size());
8383
}
8484

85+
TEST(ArrayRefTest, KeepBack) {
86+
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
87+
ArrayRef<int> AR1(TheNumbers);
88+
ArrayRef<int> AR2(AR1.end() - 1, 1);
89+
EXPECT_TRUE(AR1.keep_back().equals(AR2));
90+
}
91+
92+
TEST(ArrayRefTest, KeepFront) {
93+
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
94+
ArrayRef<int> AR1(TheNumbers);
95+
ArrayRef<int> AR2(AR1.data(), 2);
96+
EXPECT_TRUE(AR1.keep_front(2).equals(AR2));
97+
}
98+
8599
TEST(ArrayRefTest, Equals) {
86100
static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
87101
ArrayRef<int> AR1(A1);

unittests/ADT/StringRefTest.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,5 +640,48 @@ TEST(StringRefTest, AllocatorCopy) {
640640
EXPECT_NE(Str2.data(), Str2c.data());
641641
}
642642

643+
TEST(StringRefTest, Drop) {
644+
StringRef Test("StringRefTest::Drop");
645+
646+
StringRef Dropped = Test.drop_front(5);
647+
EXPECT_EQ(Dropped, "gRefTest::Drop");
648+
649+
Dropped = Test.drop_back(5);
650+
EXPECT_EQ(Dropped, "StringRefTest:");
651+
652+
Dropped = Test.drop_front(0);
653+
EXPECT_EQ(Dropped, Test);
654+
655+
Dropped = Test.drop_back(0);
656+
EXPECT_EQ(Dropped, Test);
657+
658+
Dropped = Test.drop_front(Test.size());
659+
EXPECT_TRUE(Dropped.empty());
660+
661+
Dropped = Test.drop_back(Test.size());
662+
EXPECT_TRUE(Dropped.empty());
663+
}
664+
665+
TEST(StringRefTest, Take) {
666+
StringRef Test("StringRefTest::Take");
667+
668+
StringRef Taken = Test.take_front(5);
669+
EXPECT_EQ(Taken, "Strin");
670+
671+
Taken = Test.take_back(5);
672+
EXPECT_EQ(Taken, ":Take");
673+
674+
Taken = Test.take_front(Test.size());
675+
EXPECT_EQ(Taken, Test);
676+
677+
Taken = Test.take_back(Test.size());
678+
EXPECT_EQ(Taken, Test);
679+
680+
Taken = Test.take_front(0);
681+
EXPECT_TRUE(Taken.empty());
682+
683+
Taken = Test.take_back(0);
684+
EXPECT_TRUE(Taken.empty());
685+
}
643686

644687
} // end anonymous namespace

0 commit comments

Comments
 (0)