Skip to content

Commit 26808fb

Browse files
feat: add pad helper to write N zeroed bytes
1 parent d468baf commit 26808fb

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

include/BufferStream.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ class BufferStream {
274274
return this->write(obj);
275275
}
276276

277+
template<BufferStreamPODType T = std::byte>
278+
BufferStream& pad(std::uint64_t n) {
279+
for (std::uint64_t i = 0; i < n * sizeof(T); i++) {
280+
this->write<std::byte>({});
281+
}
282+
return *this;
283+
}
284+
277285
template<BufferStreamPODType T, std::uint64_t N>
278286
BufferStream& read(T(&obj)[N]) {
279287
if (this->useExceptions && this->bufferPos + sizeof(T) * N > this->bufferLen) {

include/FileStream.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ class FileStream {
235235
return this->write(obj);
236236
}
237237

238+
template<BufferStreamPODType T = std::byte>
239+
FileStream& pad(std::uint64_t n) {
240+
for (std::uint64_t i = 0; i < n * sizeof(T); i++) {
241+
this->write<std::byte>({});
242+
}
243+
return *this;
244+
}
245+
238246
template<BufferStreamPODType T, std::uint64_t N>
239247
FileStream& read(T(&obj)[N]) {
240248
return this->read(&obj, N);

test/BufferStream.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ TEST(BufferStream, skip) {
154154
EXPECT_EQ(stream.tell(), 3);
155155
}
156156

157+
TEST(BufferStream, pad) {
158+
std::vector<unsigned char> buffer;
159+
BufferStream stream{buffer};
160+
161+
EXPECT_EQ(stream.tell(), 0);
162+
163+
stream.pad(1);
164+
EXPECT_EQ(stream.tell(), 1);
165+
166+
stream.pad(2);
167+
EXPECT_EQ(stream.tell(), 3);
168+
169+
stream.pad<std::int16_t>(1);
170+
EXPECT_EQ(stream.tell(), 3 + sizeof(std::int16_t));
171+
}
172+
157173
TEST(BufferStream, read_int_ref) {
158174
{
159175
int x = 10;

0 commit comments

Comments
 (0)