llvm.org GIT mirror llvm / f0bbaf1
Add some helpers for manipulating BinaryStreamRefs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303297 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 3 years ago
4 changed file(s) with 65 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
257257 /// \returns the next byte in the stream.
258258 uint8_t peek() const;
259259
260 Error padToAlignment(uint32_t Align);
261
260262 std::pair
261263 split(uint32_t Offset) const;
262264
5656 return Result;
5757 }
5858
59 /// Return a new BinaryStreamRef with the first \p N elements removed.
60 RefType drop_back(uint32_t N) const {
61 if (!BorrowedImpl)
62 return RefType();
63
64 N = std::min(N, Length);
65 RefType Result(static_cast(*this));
66 Result.Length -= N;
67 return Result;
68 }
69
5970 /// Return a new BinaryStreamRef with only the first \p N elements remaining.
6071 RefType keep_front(uint32_t N) const {
61 if (!BorrowedImpl)
62 return RefType();
63 N = std::min(N, Length);
64 RefType Result(static_cast(*this));
65 Result.Length = N;
66 return Result;
72 assert(N <= getLength());
73 return drop_back(getLength() - N);
74 }
75
76 /// Return a new BinaryStreamRef with only the last \p N elements remaining.
77 RefType keep_back(uint32_t N) const {
78 assert(N <= getLength());
79 return drop_front(getLength() - N);
80 }
81
82 /// Return a new BinaryStreamRef with the first and last \p N elements
83 /// removed.
84 RefType drop_symmetric(uint32_t N) const {
85 return drop_front(N).drop_back(N);
6786 }
6887
6988 /// Return a new BinaryStreamRef with the first \p Offset elements removed,
9494 return Error::success();
9595 }
9696
97 Error BinaryStreamReader::padToAlignment(uint32_t Align) {
98 uint32_t NewOffset = alignTo(Offset, Align);
99 return skip(NewOffset - Offset);
100 }
101
97102 uint8_t BinaryStreamReader::peek() const {
98103 ArrayRef Buffer;
99104 auto EC = Stream.readBytes(Offset, 1, Buffer);
286286 ASSERT_NO_ERROR(Ref.readLongestContiguousChunk(2, Buffer));
287287 EXPECT_EQ(makeArrayRef(InputData).take_back(), Buffer);
288288 }
289 }
290
291 TEST_F(BinaryStreamTest, DropOperations) {
292 std::vector InputData = {1, 2, 3, 4, 5, 4, 3, 2, 1};
293 auto RefData = makeArrayRef(InputData);
294 initializeInput(InputData, 1);
295
296 ArrayRef Result;
297 BinaryStreamRef Original(InputData, support::little);
298 ASSERT_EQ(InputData.size(), Original.getLength());
299
300 EXPECT_NO_ERROR(Original.readBytes(0, InputData.size(), Result));
301 EXPECT_EQ(RefData, Result);
302
303 auto Dropped = Original.drop_front(2);
304 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
305 EXPECT_EQ(RefData.drop_front(2), Result);
306
307 Dropped = Original.drop_back(2);
308 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
309 EXPECT_EQ(RefData.drop_back(2), Result);
310
311 Dropped = Original.keep_front(2);
312 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
313 EXPECT_EQ(RefData.take_front(2), Result);
314
315 Dropped = Original.keep_back(2);
316 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
317 EXPECT_EQ(RefData.take_back(2), Result);
318
319 Dropped = Original.drop_symmetric(2);
320 EXPECT_NO_ERROR(Dropped.readBytes(0, Dropped.getLength(), Result));
321 EXPECT_EQ(RefData.drop_front(2).drop_back(2), Result);
289322 }
290323
291324 // Test that we can write to a BinaryStream without a StreamWriter.