1const std = @import("std");
2const File = std.fs.File;
3const Writer = std.fs.File.Writer;
4
5pub fn main() !void {
6 const in = std.io.getStdIn();
7 const out = std.io.getStdOut();
8 Uf.init().streamCopy(in, out) catch |err| {
9 try std.io.getStdErr().writer().print("Error {}", .{err});
10 };
11}
12
13const buf_size = 1_000_000;
14
15const Uf = struct {
16 const Self = @This();
17
18 var out_buf: [buf_size]u8 = undefined;
19 var out_index: u32 = 0;
20
21 fn init() *const Self {
22 return &Self{};
23 }
24
25 fn flush(_: *const Self, out: Writer) !void {
26 _ = try out.write(out_buf[0..out_index]);
27 out_index = 0;
28 }
29
30 fn writeChar(self: *const Self, out: Writer, c: u8) !void {
31 if (out_index >= buf_size) {
32 try self.flush(out);
33 }
34 out_buf[out_index] = c;
35 out_index += 1;
36 }
37
38 fn streamCopy(self: *const Self, in: File, out: File) !void {
39 const reader = in.reader();
40 const writer = out.writer();
41
42 var hit = false;
43 var second = false;
44 var in_buf: [buf_size]u8 = undefined;
45 while (true) {
46 const size = try reader.read(&in_buf);
47
48 for (in_buf[0..size]) |c| {
49 if (hit and c == '\n') {
50 if (second == false) {
51 second = true;
52 } else {
53 continue;
54 }
55 } else if (hit and c != '\n') {
56 if (second) {
57 try self.writeChar(writer, '\n');
58 }
59 hit = false;
60 second = false;
61 } else if (c == '\n') {
62 hit = true;
63 continue;
64 }
65
66 try self.writeChar(writer, c);
67 }
68
69 if (size != in_buf.len) {
70 try self.flush(writer);
71 return;
72 }
73 }
74 }
75};