authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-11-19 02:26:31+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-11 16:48:30-07:00
loge873668d38733d212eef9a317c9679305ff3b82e
tree0b476ab199b3a3952fca5b0ae9c8d37b885008ba
parent8695b9fbe781ea030bafb4f6272202b9115ba46b

std: add LimitedReader: reader that returns EOF early


3 files changed, 55 insertions(+), 1 deletions(-)

CMakeLists.txt+2-1
...@@ -375,8 +375,9 @@ set(ZIG_STAGE2_SOURCES...@@ -375,8 +375,9 @@ set(ZIG_STAGE2_SOURCES
375 "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_atomic_file.zig"375 "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_atomic_file.zig"
376 "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_writer.zig"376 "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_writer.zig"
377 "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig"377 "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig"
378 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig"
379 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig"378 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig"
379 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig"
380 "${CMAKE_SOURCE_DIR}/lib/std/io/early_eof_reader.zig"
380 "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig"381 "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig"
381 "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig"382 "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig"
382 "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig"383 "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig"
lib/std/io.zig+3
...@@ -125,6 +125,9 @@ pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferS...@@ -125,6 +125,9 @@ pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferS
125pub const CWriter = @import("io/c_writer.zig").CWriter;125pub const CWriter = @import("io/c_writer.zig").CWriter;
126pub const cWriter = @import("io/c_writer.zig").cWriter;126pub const cWriter = @import("io/c_writer.zig").cWriter;
127127
128pub const EarlyEOFReader = @import("io/early_eof_reader.zig").EarlyEOFReader;
129pub const earlyEOFReader = @import("io/early_eof_reader.zig").earlyEOFReader;
130
128pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter;131pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter;
129pub const countingWriter = @import("io/counting_writer.zig").countingWriter;132pub const countingWriter = @import("io/counting_writer.zig").countingWriter;
130pub const CountingReader = @import("io/counting_reader.zig").CountingReader;133pub const CountingReader = @import("io/counting_reader.zig").CountingReader;
lib/std/io/early_eof_reader.zig created+50
...@@ -0,0 +1,50 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2020 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6const std = @import("../std.zig");
7const io = std.io;
8const assert = std.debug.assert;
9const testing = std.testing;
10
11pub fn EarlyEOFReader(comptime ReaderType: type) type {
12 return struct {
13 inner_reader: ReaderType,
14 bytes_left: u64,
15
16 pub const Error = ReaderType.Error;
17 pub const Reader = io.Reader(*Self, Error, read);
18
19 const Self = @This();
20
21 pub fn read(self: *Self, dest: []u8) Error!usize {
22 const max_read = std.math.min(self.bytes_left, dest.len);
23 const n = try self.inner_reader.read(dest[0..max_read]);
24 self.bytes_left -= n;
25 return n;
26 }
27
28 pub fn reader(self: *Self) Reader {
29 return .{ .context = self };
30 }
31 };
32}
33
34/// Returns an initialised `EarlyEOFReader`
35/// `bytes_left` is a `u64` to be able to take 64 bit file offsets
36pub fn earlyEOFReader(inner_reader: anytype, bytes_left: u64) EarlyEOFReader(@TypeOf(inner_reader)) {
37 return .{ .inner_reader = inner_reader, .bytes_left = bytes_left };
38}
39
40test "io.EarlyEOFReader" {
41 const data = "hello world";
42 var fbs = std.io.fixedBufferStream(data);
43 var early_stream = earlyEOFReader(fbs.reader(), 3);
44
45 var buf: [5]u8 = undefined;
46 testing.expectEqual(@as(usize, 3), try early_stream.reader().read(&buf));
47 testing.expectEqualSlices(u8, data[0..3], buf[0..3]);
48 testing.expectEqual(@as(usize, 0), try early_stream.reader().read(&buf));
49 testing.expectError(error.EndOfStream, early_stream.reader().skipBytes(10, .{}));
50}