1const std = @import("../std.zig");
2const io = std.io;
3const assert = std.debug.assert;
4
5/// A Writer that returns whether the given character has been written to it.
6/// The contents are not written to anything.
7pub fn FindByteWriter(comptime UnderlyingWriter: type) type {
8    return struct {
9        const Self = @This();
10        pub const Error = UnderlyingWriter.Error;
11        pub const Writer = io.Writer(*Self, Error, write);
12
13        underlying_writer: UnderlyingWriter,
14        byte_found: bool,
15        byte: u8,
16
17        pub fn writer(self: *Self) Writer {
18            return .{ .context = self };
19        }
20
21        fn write(self: *Self, bytes: []const u8) Error!usize {
22            if (!self.byte_found) {
23                self.byte_found = blk: {
24                    for (bytes) |b|
25                        if (b == self.byte) break :blk true;
26                    break :blk false;
27                };
28            }
29            return self.underlying_writer.write(bytes);
30        }
31    };
32}
33
34pub fn findByteWriter(byte: u8, underlying_writer: anytype) FindByteWriter(@TypeOf(underlying_writer)) {
35    return FindByteWriter(@TypeOf(underlying_writer)){
36        .underlying_writer = underlying_writer,
37        .byte = byte,
38        .byte_found = false,
39    };
40}
41