From 0ab8ae944cd2b48c783c4ca8963997b6af207d5e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 19 Nov 2020 22:50:56 +1100 Subject: std: reader.skipBytes's num_bytes should be a u64 --- lib/std/io/reader.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/std/io') diff --git a/lib/std/io/reader.zig b/lib/std/io/reader.zig index 705eb9e816..e174051518 100644 --- a/lib/std/io/reader.zig +++ b/lib/std/io/reader.zig @@ -271,8 +271,9 @@ pub fn Reader( buf_size: usize = 512, }; + // `num_bytes` is a `u64` to match `off_t` /// Reads `num_bytes` bytes from the stream and discards them - pub fn skipBytes(self: Self, num_bytes: usize, comptime options: SkipBytesOptions) !void { + pub fn skipBytes(self: Self, num_bytes: u64, comptime options: SkipBytesOptions) !void { var buf: [options.buf_size]u8 = undefined; var remaining = num_bytes; -- cgit v1.2.3 From e873668d38733d212eef9a317c9679305ff3b82e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 19 Nov 2020 02:26:31 +1100 Subject: std: add LimitedReader: reader that returns EOF early --- CMakeLists.txt | 3 ++- lib/std/io.zig | 3 +++ lib/std/io/early_eof_reader.zig | 50 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lib/std/io/early_eof_reader.zig (limited to 'lib/std/io') diff --git a/CMakeLists.txt b/CMakeLists.txt index 49dde70f5e..3d02749d26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -375,8 +375,9 @@ set(ZIG_STAGE2_SOURCES "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_atomic_file.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_writer.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig" - "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig" + "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig" + "${CMAKE_SOURCE_DIR}/lib/std/io/early_eof_reader.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig" diff --git a/lib/std/io.zig b/lib/std/io.zig index fb5f00805c..fa0d5d8698 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -125,6 +125,9 @@ pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferS pub const CWriter = @import("io/c_writer.zig").CWriter; pub const cWriter = @import("io/c_writer.zig").cWriter; +pub const EarlyEOFReader = @import("io/early_eof_reader.zig").EarlyEOFReader; +pub const earlyEOFReader = @import("io/early_eof_reader.zig").earlyEOFReader; + pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter; pub const countingWriter = @import("io/counting_writer.zig").countingWriter; pub const CountingReader = @import("io/counting_reader.zig").CountingReader; diff --git a/lib/std/io/early_eof_reader.zig b/lib/std/io/early_eof_reader.zig new file mode 100644 index 0000000000..5ddb340455 --- /dev/null +++ b/lib/std/io/early_eof_reader.zig @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2015-2020 Zig Contributors +// This file is part of [zig](https://ziglang.org/), which is MIT licensed. +// The MIT license requires this copyright notice to be included in all copies +// and substantial portions of the software. +const std = @import("../std.zig"); +const io = std.io; +const assert = std.debug.assert; +const testing = std.testing; + +pub fn EarlyEOFReader(comptime ReaderType: type) type { + return struct { + inner_reader: ReaderType, + bytes_left: u64, + + pub const Error = ReaderType.Error; + pub const Reader = io.Reader(*Self, Error, read); + + const Self = @This(); + + pub fn read(self: *Self, dest: []u8) Error!usize { + const max_read = std.math.min(self.bytes_left, dest.len); + const n = try self.inner_reader.read(dest[0..max_read]); + self.bytes_left -= n; + return n; + } + + pub fn reader(self: *Self) Reader { + return .{ .context = self }; + } + }; +} + +/// Returns an initialised `EarlyEOFReader` +/// `bytes_left` is a `u64` to be able to take 64 bit file offsets +pub fn earlyEOFReader(inner_reader: anytype, bytes_left: u64) EarlyEOFReader(@TypeOf(inner_reader)) { + return .{ .inner_reader = inner_reader, .bytes_left = bytes_left }; +} + +test "io.EarlyEOFReader" { + const data = "hello world"; + var fbs = std.io.fixedBufferStream(data); + var early_stream = earlyEOFReader(fbs.reader(), 3); + + var buf: [5]u8 = undefined; + testing.expectEqual(@as(usize, 3), try early_stream.reader().read(&buf)); + testing.expectEqualSlices(u8, data[0..3], buf[0..3]); + testing.expectEqual(@as(usize, 0), try early_stream.reader().read(&buf)); + testing.expectError(error.EndOfStream, early_stream.reader().skipBytes(10, .{})); +} -- cgit v1.2.3 From d68adc5382a29687d2e24e27d9877473346a25e1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 11 Jan 2021 16:51:56 -0700 Subject: std.EarlyEOFReader: rename to LimitedReader --- CMakeLists.txt | 2 +- lib/std/fs/file.zig | 2 +- lib/std/io.zig | 4 ++-- lib/std/io/early_eof_reader.zig | 50 ----------------------------------------- lib/std/io/limited_reader.zig | 50 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 54 deletions(-) delete mode 100644 lib/std/io/early_eof_reader.zig create mode 100644 lib/std/io/limited_reader.zig (limited to 'lib/std/io') diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d02749d26..0dfec5a953 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -377,9 +377,9 @@ set(ZIG_STAGE2_SOURCES "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig" - "${CMAKE_SOURCE_DIR}/lib/std/io/early_eof_reader.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig" + "${CMAKE_SOURCE_DIR}/lib/std/io/limited_reader.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/seekable_stream.zig" "${CMAKE_SOURCE_DIR}/lib/std/io/writer.zig" diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 45cb5e3218..6002770f55 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -726,7 +726,7 @@ pub const File = struct { var fifo = std.fifo.LinearFifo(u8, .{ .Static = 4096 }).init(); if (args.in_len) |len| { - var stream = std.io.earlyEOFReader(in_file.reader(), len); + var stream = std.io.limitedReader(in_file.reader(), len); try fifo.pump(stream.reader(), self.writer()); } else { try fifo.pump(in_file.reader(), self.writer()); diff --git a/lib/std/io.zig b/lib/std/io.zig index fa0d5d8698..e9a03445f6 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -125,8 +125,8 @@ pub const fixedBufferStream = @import("io/fixed_buffer_stream.zig").fixedBufferS pub const CWriter = @import("io/c_writer.zig").CWriter; pub const cWriter = @import("io/c_writer.zig").cWriter; -pub const EarlyEOFReader = @import("io/early_eof_reader.zig").EarlyEOFReader; -pub const earlyEOFReader = @import("io/early_eof_reader.zig").earlyEOFReader; +pub const LimitedReader = @import("io/limited_reader.zig").LimitedReader; +pub const limitedReader = @import("io/limited_reader.zig").limitedReader; pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter; pub const countingWriter = @import("io/counting_writer.zig").countingWriter; diff --git a/lib/std/io/early_eof_reader.zig b/lib/std/io/early_eof_reader.zig deleted file mode 100644 index 5ddb340455..0000000000 --- a/lib/std/io/early_eof_reader.zig +++ /dev/null @@ -1,50 +0,0 @@ -// SPDX-License-Identifier: MIT -// Copyright (c) 2015-2020 Zig Contributors -// This file is part of [zig](https://ziglang.org/), which is MIT licensed. -// The MIT license requires this copyright notice to be included in all copies -// and substantial portions of the software. -const std = @import("../std.zig"); -const io = std.io; -const assert = std.debug.assert; -const testing = std.testing; - -pub fn EarlyEOFReader(comptime ReaderType: type) type { - return struct { - inner_reader: ReaderType, - bytes_left: u64, - - pub const Error = ReaderType.Error; - pub const Reader = io.Reader(*Self, Error, read); - - const Self = @This(); - - pub fn read(self: *Self, dest: []u8) Error!usize { - const max_read = std.math.min(self.bytes_left, dest.len); - const n = try self.inner_reader.read(dest[0..max_read]); - self.bytes_left -= n; - return n; - } - - pub fn reader(self: *Self) Reader { - return .{ .context = self }; - } - }; -} - -/// Returns an initialised `EarlyEOFReader` -/// `bytes_left` is a `u64` to be able to take 64 bit file offsets -pub fn earlyEOFReader(inner_reader: anytype, bytes_left: u64) EarlyEOFReader(@TypeOf(inner_reader)) { - return .{ .inner_reader = inner_reader, .bytes_left = bytes_left }; -} - -test "io.EarlyEOFReader" { - const data = "hello world"; - var fbs = std.io.fixedBufferStream(data); - var early_stream = earlyEOFReader(fbs.reader(), 3); - - var buf: [5]u8 = undefined; - testing.expectEqual(@as(usize, 3), try early_stream.reader().read(&buf)); - testing.expectEqualSlices(u8, data[0..3], buf[0..3]); - testing.expectEqual(@as(usize, 0), try early_stream.reader().read(&buf)); - testing.expectError(error.EndOfStream, early_stream.reader().skipBytes(10, .{})); -} diff --git a/lib/std/io/limited_reader.zig b/lib/std/io/limited_reader.zig new file mode 100644 index 0000000000..734558b1e6 --- /dev/null +++ b/lib/std/io/limited_reader.zig @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2015-2020 Zig Contributors +// This file is part of [zig](https://ziglang.org/), which is MIT licensed. +// The MIT license requires this copyright notice to be included in all copies +// and substantial portions of the software. +const std = @import("../std.zig"); +const io = std.io; +const assert = std.debug.assert; +const testing = std.testing; + +pub fn LimitedReader(comptime ReaderType: type) type { + return struct { + inner_reader: ReaderType, + bytes_left: u64, + + pub const Error = ReaderType.Error; + pub const Reader = io.Reader(*Self, Error, read); + + const Self = @This(); + + pub fn read(self: *Self, dest: []u8) Error!usize { + const max_read = std.math.min(self.bytes_left, dest.len); + const n = try self.inner_reader.read(dest[0..max_read]); + self.bytes_left -= n; + return n; + } + + pub fn reader(self: *Self) Reader { + return .{ .context = self }; + } + }; +} + +/// Returns an initialised `LimitedReader` +/// `bytes_left` is a `u64` to be able to take 64 bit file offsets +pub fn limitedReader(inner_reader: anytype, bytes_left: u64) LimitedReader(@TypeOf(inner_reader)) { + return .{ .inner_reader = inner_reader, .bytes_left = bytes_left }; +} + +test "basic usage" { + const data = "hello world"; + var fbs = std.io.fixedBufferStream(data); + var early_stream = limitedReader(fbs.reader(), 3); + + var buf: [5]u8 = undefined; + testing.expectEqual(@as(usize, 3), try early_stream.reader().read(&buf)); + testing.expectEqualSlices(u8, data[0..3], buf[0..3]); + testing.expectEqual(@as(usize, 0), try early_stream.reader().read(&buf)); + testing.expectError(error.EndOfStream, early_stream.reader().skipBytes(10, .{})); +} -- cgit v1.2.3