summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 21103fff4a11308d5167d9b708f5ff80fc668808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const std = @import("std");
const File = std.fs.File;
const Reader = std.fs.File.Reader;
const Writer = std.fs.File.Writer;

pub fn main() !void {
    const in = std.io.getStdIn();
    const out = std.io.getStdOut();
    Uf.init().streamCopy(in.reader(), out.writer()) 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: anytype) !void {
        _ = try out.write(out_buf[0..out_index]);
        out_index = 0;
    }

    fn writeChar(self: Self, out: anytype, c: u8) !void {
        if (out_index >= buf_size) {
            try self.flush(out);
        }
        out_buf[out_index] = c;
        out_index += 1;
    }

    fn streamCopy(self: Self, reader: anytype, writer: anytype) !void {
        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');
                    } else {
                        try self.writeChar(writer, ' ');
                    }
                    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;
            }
        }
    }
};

test "Test format" {
    const text_in =
        \\# This is a markdown
        \\
        \\Lorem ipsum dolor sit amet,
        \\consectetur adipiscing elit.
        \\
        \\
        \\
        \\Praesent pharetra sit amet ante sit amet consequat.
    ;

    const text_out =
        \\# This is a markdown
        \\
        \\Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        \\
        \\Praesent pharetra sit amet ante sit amet consequat.
    ;

    var stream = std.io.fixedBufferStream(text_in);

    var list = std.ArrayList(u8).init(std.testing.allocator);
    defer list.deinit();

    try Uf.init().streamCopy(stream.reader(), list.writer());
    try std.testing.expect(std.mem.eql(u8, text_out, list.items));
}