| author | |
| committer | |
| log | d68adc5382a29687d2e24e27d9877473346a25e1 |
| tree | 7ee84cd567bac20ed769845b0e469fe39e1c2f6c |
| parent | 01d1a8a783883414bd694731c7c2bfbd054b9a46 |
5 files changed, 54 insertions(+), 54 deletions(-)
CMakeLists.txt+1-1| ... | @@ -377,9 +377,9 @@ set(ZIG_STAGE2_SOURCES | ... | @@ -377,9 +377,9 @@ set(ZIG_STAGE2_SOURCES |
| 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_reader.zig" | 378 | "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig" |
| 379 | "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig" | 379 | "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig" |
| 380 | "${CMAKE_SOURCE_DIR}/lib/std/io/early_eof_reader.zig" | ||
| 381 | "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig" | 380 | "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig" |
| 382 | "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig" | 381 | "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig" |
| 382 | "${CMAKE_SOURCE_DIR}/lib/std/io/limited_reader.zig" | ||
| 383 | "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig" | 383 | "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig" |
| 384 | "${CMAKE_SOURCE_DIR}/lib/std/io/seekable_stream.zig" | 384 | "${CMAKE_SOURCE_DIR}/lib/std/io/seekable_stream.zig" |
| 385 | "${CMAKE_SOURCE_DIR}/lib/std/io/writer.zig" | 385 | "${CMAKE_SOURCE_DIR}/lib/std/io/writer.zig" |
lib/std/fs/file.zig+1-1| ... | @@ -726,7 +726,7 @@ pub const File = struct { | ... | @@ -726,7 +726,7 @@ pub const File = struct { |
| 726 | 726 | ||
| 727 | var fifo = std.fifo.LinearFifo(u8, .{ .Static = 4096 }).init(); | 727 | var fifo = std.fifo.LinearFifo(u8, .{ .Static = 4096 }).init(); |
| 728 | if (args.in_len) |len| { | 728 | if (args.in_len) |len| { |
| 729 | var stream = std.io.earlyEOFReader(in_file.reader(), len); | 729 | var stream = std.io.limitedReader(in_file.reader(), len); |
| 730 | try fifo.pump(stream.reader(), self.writer()); | 730 | try fifo.pump(stream.reader(), self.writer()); |
| 731 | } else { | 731 | } else { |
| 732 | try fifo.pump(in_file.reader(), self.writer()); | 732 | try fifo.pump(in_file.reader(), self.writer()); |
lib/std/io.zig+2-2| ... | @@ -125,8 +125,8 @@ pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferS | ... | @@ -125,8 +125,8 @@ pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferS |
| 125 | pub const CWriter = @import("io/c_writer.zig").CWriter; | 125 | pub const CWriter = @import("io/c_writer.zig").CWriter; |
| 126 | pub const cWriter = @import("io/c_writer.zig").cWriter; | 126 | pub const cWriter = @import("io/c_writer.zig").cWriter; |
| 127 | 127 | ||
| 128 | pub const EarlyEOFReader = @import("io/early_eof_reader.zig").EarlyEOFReader; | 128 | pub const LimitedReader = @import("io/limited_reader.zig").LimitedReader; |
| 129 | pub const earlyEOFReader = @import("io/early_eof_reader.zig").earlyEOFReader; | 129 | pub const limitedReader = @import("io/limited_reader.zig").limitedReader; |
| 130 | 130 | ||
| 131 | pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter; | 131 | pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter; |
| 132 | pub const countingWriter = @import("io/counting_writer.zig").countingWriter; | 132 | pub const countingWriter = @import("io/counting_writer.zig").countingWriter; |
lib/std/io/early_eof_reader.zig deleted-50| ... | @@ -1,50 +0,0 @@ | ||
| 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. | ||
| 6 | const std = @import("../std.zig"); | ||
| 7 | const io = std.io; | ||
| 8 | const assert = std.debug.assert; | ||
| 9 | const testing = std.testing; | ||
| 10 | |||
| 11 | pub 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 | ||
| 36 | pub fn earlyEOFReader(inner_reader: anytype, bytes_left: u64) EarlyEOFReader(@TypeOf(inner_reader)) { | ||
| 37 | return .{ .inner_reader = inner_reader, .bytes_left = bytes_left }; | ||
| 38 | } | ||
| 39 | |||
| 40 | test "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 | } | ||
lib/std/io/limited_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. | ||
| 6 | const std = @import("../std.zig"); | ||
| 7 | const io = std.io; | ||
| 8 | const assert = std.debug.assert; | ||
| 9 | const testing = std.testing; | ||
| 10 | |||
| 11 | pub fn LimitedReader(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 `LimitedReader` | ||
| 35 | /// `bytes_left` is a `u64` to be able to take 64 bit file offsets | ||
| 36 | pub fn limitedReader(inner_reader: anytype, bytes_left: u64) LimitedReader(@TypeOf(inner_reader)) { | ||
| 37 | return .{ .inner_reader = inner_reader, .bytes_left = bytes_left }; | ||
| 38 | } | ||
| 39 | |||
| 40 | test "basic usage" { | ||
| 41 | const data = "hello world"; | ||
| 42 | var fbs = std.io.fixedBufferStream(data); | ||
| 43 | var early_stream = limitedReader(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 | } | ||