const std = @import("std"); const File = std.fs.File; const Writer = std.fs.File.Writer; pub fn main() !void { const in = std.io.getStdIn(); const out = std.io.getStdOut(); Uf.init().streamCopy(in, out) catch |err| { try std.io.getStdErr().writer().print("Error {}", .{err}); }; } const buf_size = 1_000_000; const Uf = struct { const Self = @This(); var out_buf: [buf_size]u8 = undefined; var out_index: u32 = 0; fn init() Self { return Self{}; } fn flush(_: Self, out: Writer) !void { _ = try out.write(out_buf[0..out_index]); out_index = 0; } fn writeChar(self: Self, out: Writer, c: u8) !void { if (out_index >= buf_size) { try self.flush(out); } out_buf[out_index] = c; out_index += 1; } fn streamCopy(self: Self, in: File, out: File) !void { const reader = in.reader(); const writer = out.writer(); var hit = false; var second = false; var in_buf: [buf_size]u8 = undefined; while (true) { const size = try reader.read(&in_buf); for (in_buf[0..size]) |c| { if (hit and c == '\n') { if (second == false) { second = true; } else { continue; } } else if (hit and c != '\n') { if (second) { try self.writeChar(writer, '\n'); } hit = false; second = false; } else if (c == '\n') { hit = true; continue; } try self.writeChar(writer, c); } if (size != in_buf.len) { try self.flush(writer); return; } } } };