return Result;
}
- /// Return a new BinaryStreamRef with only the first \p N elements remaining.
- RefType keep_front(uint32_t N) const {
+ /// Return a new BinaryStreamRef with the first \p N elements removed.
+ RefType drop_back(uint32_t N) const {
if (!BorrowedImpl)
return RefType();
+
N = std::min(N, Length);
RefType Result(static_cast<const RefType &>(*this));
- Result.Length = N;
+ Result.Length -= N;
return Result;
}
+ /// Return a new BinaryStreamRef with only the first \p N elements remaining.
+ RefType keep_front(uint32_t N) const {
+ assert(N <= getLength());
+ return drop_back(getLength() - N);
+ }
+
+ /// Return a new BinaryStreamRef with only the last \p N elements remaining.
+ RefType keep_back(uint32_t N) const {
+ assert(N <= getLength());
+ return drop_front(getLength() - N);
+ }
+
+ /// Return a new BinaryStreamRef with the first and last \p N elements
+ /// removed.
+ RefType drop_symmetric(uint32_t N) const {
+ return drop_front(N).drop_back(N);
+ }
+
/// Return a new BinaryStreamRef with the first \p Offset elements removed,
/// and retaining exactly \p Len elements.
RefType slice(uint32_t Offset, uint32_t Len) const {
}
}
+TEST_F(BinaryStreamTest, DropOperations) {
+ std::vector<uint8_t> InputData = {1, 2, 3, 4, 5, 4, 3, 2, 1};
+ auto RefData = makeArrayRef(InputData);
+ initializeInput(InputData, 1);
+
+ ArrayRef<uint8_t> Result;
+ BinaryStreamRef Original(InputData, support::little);
+ ASSERT_EQ(InputData.size(), Original.getLength());
+
+ EXPECT_NO_ERROR(Original.readBytes(0, InputData.size(), Result));
+ EXPECT_EQ(RefData, Result);
+
+ auto Dropped = Original.drop_front(2);
+ EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
+ EXPECT_EQ(RefData.drop_front(2), Result);
+
+ Dropped = Original.drop_back(2);
+ EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
+ EXPECT_EQ(RefData.drop_back(2), Result);
+
+ Dropped = Original.keep_front(2);
+ EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
+ EXPECT_EQ(RefData.take_front(2), Result);
+
+ Dropped = Original.keep_back(2);
+ EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
+ EXPECT_EQ(RefData.take_back(2), Result);
+
+ Dropped = Original.drop_symmetric(2);
+ EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
+ EXPECT_EQ(RefData.drop_front(2).drop_back(2), Result);
+}
+
// Test that we can write to a BinaryStream without a StreamWriter.
TEST_F(BinaryStreamTest, MutableBinaryByteStreamBounds) {
std::vector<uint8_t> InputData = {'T', 'e', 's', 't', '\0'};