authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2020-12-22 14:30:48+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-23 01:27:12+02:00
logab6183e11999f3d1dbc1e9dca091cd1411256323
treed4a4fe8a0d5d4e0dc7b8057d21d0784c9b44102e
parentcb3198af2af2501c1caf04228736d0b94a371f97

Added std.io.counting_reader


3 files changed, 51 insertions(+), 0 deletions(-)

CMakeLists.txt+1
...@@ -362,6 +362,7 @@ set(ZIG_STAGE2_SOURCES...@@ -362,6 +362,7 @@ set(ZIG_STAGE2_SOURCES
362 "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_writer.zig"362 "${CMAKE_SOURCE_DIR}/lib/std/io/buffered_writer.zig"
363 "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig"363 "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig"
364 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig"364 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig"
365 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig"
365 "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_out_stream.zig"366 "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_out_stream.zig"
366 "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig"367 "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig"
367 "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig"368 "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig"
lib/std/io.zig+2
...@@ -143,6 +143,8 @@ pub const cOutStream = cWriter;...@@ -143,6 +143,8 @@ pub const cOutStream = cWriter;
143143
144pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter;144pub const CountingWriter = @import("io/counting_writer.zig").CountingWriter;
145pub const countingWriter = @import("io/counting_writer.zig").countingWriter;145pub const countingWriter = @import("io/counting_writer.zig").countingWriter;
146pub const CountingReader = @import("io/counting_reader.zig").CountingReader;
147pub const countingReader = @import("io/counting_reader.zig").countingReader;
146/// Deprecated: use `CountingWriter`148/// Deprecated: use `CountingWriter`
147pub const CountingOutStream = CountingWriter;149pub const CountingOutStream = CountingWriter;
148/// Deprecated: use `countingWriter`150/// Deprecated: use `countingWriter`
lib/std/io/counting_reader.zig created+48
...@@ -0,0 +1,48 @@
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 testing = std.testing;
9
10/// A Reader that counts how many bytes has been read from it.
11pub fn CountingReader(comptime ReaderType: anytype) type {
12 return struct {
13 child_reader: ReaderType,
14 bytes_read: u64 = 0,
15
16 pub const Error = ReaderType.Error;
17 pub const Reader = io.Reader(*@This(), Error, read);
18
19 pub fn read(self: *@This(), buf: []u8) Error!usize {
20 const amt = try self.child_reader.read(buf);
21 self.bytes_read += amt;
22 return amt;
23 }
24
25 pub fn reader(self: *@This()) Reader {
26 return .{ .context = self };
27 }
28 };
29}
30
31pub fn countingReader(reader: anytype) CountingReader(@TypeOf(reader)) {
32 return .{ .child_reader = reader, };
33}
34
35test "io.CountingReader" {
36 const bytes = "yay" ** 100;
37 var fbs = io.fixedBufferStream(bytes);
38
39 var counting_stream = countingReader(fbs.reader());
40 const stream = counting_stream.reader();
41
42 //read and discard all bytes
43 while(stream.readByte()) |_| {} else |err| {
44 testing.expect(err == error.EndOfStream);
45 }
46
47 testing.expect(counting_stream.bytes_read == bytes.len);
48}
\ No newline at end of file