aboutsummaryrefslogtreecommitdiff
path: root/lib/std/io/reader.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-08-31 15:49:44 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-08-31 15:49:44 -0700
commitc354f074fa91d3d1672469ba4bbc49a1730e1d01 (patch)
treea48c93c1d19c38ec49d5f3b6412d265a58e4f0b5 /lib/std/io/reader.zig
parent4971400bdcc810766da15c63e3e57a59e49499ca (diff)
parent26140678a5c72604f2baac3cb9d1e5f7b37b6b8d (diff)
downloadzig-c354f074fa91d3d1672469ba4bbc49a1730e1d01.tar.gz
zig-c354f074fa91d3d1672469ba4bbc49a1730e1d01.zip
Merge remote-tracking branch 'origin/master' into llvm11
Diffstat (limited to 'lib/std/io/reader.zig')
-rw-r--r--lib/std/io/reader.zig26
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/std/io/reader.zig b/lib/std/io/reader.zig
index 07327a431d..2ab799046a 100644
--- a/lib/std/io/reader.zig
+++ b/lib/std/io/reader.zig
@@ -231,10 +231,20 @@ pub fn Reader(
return mem.readVarInt(ReturnType, bytes, endian);
}
- pub fn skipBytes(self: Self, num_bytes: u64) !void {
- var i: u64 = 0;
- while (i < num_bytes) : (i += 1) {
- _ = try self.readByte();
+ /// Optional parameters for `skipBytes`
+ pub const SkipBytesOptions = struct {
+ buf_size: usize = 512,
+ };
+
+ /// Reads `num_bytes` bytes from the stream and discards them
+ pub fn skipBytes(self: Self, num_bytes: usize, comptime options: SkipBytesOptions) !void {
+ var buf: [options.buf_size]u8 = undefined;
+ var remaining = num_bytes;
+
+ while (remaining > 0) {
+ const amt = std.math.min(remaining, options.buf_size);
+ try self.readNoEof(buf[0..amt]);
+ remaining -= amt;
}
}
@@ -298,3 +308,11 @@ test "Reader.isBytes" {
testing.expectEqual(true, try reader.isBytes("foo"));
testing.expectEqual(false, try reader.isBytes("qux"));
}
+
+test "Reader.skipBytes" {
+ const reader = std.io.fixedBufferStream("foobar").reader();
+ try reader.skipBytes(3, .{});
+ testing.expect(try reader.isBytes("bar"));
+ try reader.skipBytes(0, .{});
+ testing.expectError(error.EndOfStream, reader.skipBytes(1, .{}));
+}