authorgravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2021-01-08 15:57:39-05:00
committergravatar for jay@jayschwa.netJay Petacat <jay@jayschwa.net> 2021-01-08 16:54:56-05:00
loge72472d953f5e91d37bc5f1bf1ec7b6fb5b1afe2
tree330c924a9e1fcd46275ca9500bd60e9054251a98
parent1595ce273edb32493231a43216a15cfbeb9ffc12

io: `FindByteOutStream` to `FindByteWriter`

See #4917

5 files changed, 55 insertions(+), 51 deletions(-)

CMakeLists.txt+1-1
......@@ -377,7 +377,7 @@ set(ZIG_STAGE2_SOURCES
377377 "${CMAKE_SOURCE_DIR}/lib/std/io/change_detection_stream.zig"
378378 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_writer.zig"
379379 "${CMAKE_SOURCE_DIR}/lib/std/io/counting_reader.zig"
380 "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_out_stream.zig"
380 "${CMAKE_SOURCE_DIR}/lib/std/io/find_byte_writer.zig"
381381 "${CMAKE_SOURCE_DIR}/lib/std/io/fixed_buffer_stream.zig"
382382 "${CMAKE_SOURCE_DIR}/lib/std/io/reader.zig"
383383 "${CMAKE_SOURCE_DIR}/lib/std/io/seekable_stream.zig"
lib/std/io.zig+6-2
......@@ -145,8 +145,12 @@ pub const autoIndentingStream = @import("io/auto_indenting_stream.zig").autoInde
145145pub const ChangeDetectionStream = @import("io/change_detection_stream.zig").ChangeDetectionStream;
146146pub const changeDetectionStream = @import("io/change_detection_stream.zig").changeDetectionStream;
147147
148pub const FindByteOutStream = @import("io/find_byte_out_stream.zig").FindByteOutStream;
149pub const findByteOutStream = @import("io/find_byte_out_stream.zig").findByteOutStream;
148pub const FindByteWriter = @import("io/find_byte_writer.zig").FindByteWriter;
149pub const findByteWriter = @import("io/find_byte_writer.zig").findByteWriter;
150/// Deprecated: use `FindByteWriter`.
151pub const FindByteOutStream = FindByteWriter;
152/// Deprecated: use `findByteWriter`.
153pub const findByteOutStream = findByteWriter;
150154
151155pub const BufferedAtomicFile = @import("io/buffered_atomic_file.zig").BufferedAtomicFile;
152156
lib/std/io/find_byte_out_stream.zig deleted-46
......@@ -1,46 +0,0 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 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
7const std = @import("../std.zig");
8const io = std.io;
9const assert = std.debug.assert;
10
11/// An OutStream that returns whether the given character has been written to it.
12/// The contents are not written to anything.
13pub fn FindByteOutStream(comptime UnderlyingWriter: type) type {
14 return struct {
15 const Self = @This();
16 pub const Error = UnderlyingWriter.Error;
17 pub const Writer = io.Writer(*Self, Error, write);
18
19 underlying_writer: UnderlyingWriter,
20 byte_found: bool,
21 byte: u8,
22
23 pub fn writer(self: *Self) Writer {
24 return .{ .context = self };
25 }
26
27 fn write(self: *Self, bytes: []const u8) Error!usize {
28 if (!self.byte_found) {
29 self.byte_found = blk: {
30 for (bytes) |b|
31 if (b == self.byte) break :blk true;
32 break :blk false;
33 };
34 }
35 return self.underlying_writer.write(bytes);
36 }
37 };
38}
39
40pub fn findByteOutStream(byte: u8, underlying_writer: anytype) FindByteOutStream(@TypeOf(underlying_writer)) {
41 return FindByteOutStream(@TypeOf(underlying_writer)){
42 .underlying_writer = underlying_writer,
43 .byte = byte,
44 .byte_found = false,
45 };
46}
lib/std/io/find_byte_writer.zig created+46
......@@ -0,0 +1,46 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2021 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
7const std = @import("../std.zig");
8const io = std.io;
9const assert = std.debug.assert;
10
11/// A Writer that returns whether the given character has been written to it.
12/// The contents are not written to anything.
13pub fn FindByteWriter(comptime UnderlyingWriter: type) type {
14 return struct {
15 const Self = @This();
16 pub const Error = UnderlyingWriter.Error;
17 pub const Writer = io.Writer(*Self, Error, write);
18
19 underlying_writer: UnderlyingWriter,
20 byte_found: bool,
21 byte: u8,
22
23 pub fn writer(self: *Self) Writer {
24 return .{ .context = self };
25 }
26
27 fn write(self: *Self, bytes: []const u8) Error!usize {
28 if (!self.byte_found) {
29 self.byte_found = blk: {
30 for (bytes) |b|
31 if (b == self.byte) break :blk true;
32 break :blk false;
33 };
34 }
35 return self.underlying_writer.write(bytes);
36 }
37 };
38}
39
40pub fn findByteWriter(byte: u8, underlying_writer: anytype) FindByteWriter(@TypeOf(underlying_writer)) {
41 return FindByteWriter(@TypeOf(underlying_writer)){
42 .underlying_writer = underlying_writer,
43 .byte = byte,
44 .byte_found = false,
45 };
46}
lib/std/zig/render.zig+2-2
......@@ -790,7 +790,7 @@ fn renderExpression(
790790 const section_exprs = row_exprs[0..section_end];
791791
792792 // Null stream for counting the printed length of each expression
793 var line_find_stream = std.io.findByteOutStream('\n', std.io.null_writer);
793 var line_find_stream = std.io.findByteWriter('\n', std.io.null_writer);
794794 var counting_stream = std.io.countingWriter(line_find_stream.writer());
795795 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, counting_stream.writer());
796796
......@@ -954,7 +954,7 @@ fn renderExpression(
954954 const expr_outputs_one_line = blk: {
955955 // render field expressions until a LF is found
956956 for (field_inits) |field_init| {
957 var find_stream = std.io.findByteOutStream('\n', std.io.null_writer);
957 var find_stream = std.io.findByteWriter('\n', std.io.null_writer);
958958 var auto_indenting_stream = std.io.autoIndentingStream(indent_delta, find_stream.writer());
959959
960960 try renderExpression(allocator, &auto_indenting_stream, tree, field_init, Space.None);