diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-07-19 21:31:04 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-07-19 21:31:04 +0200 |
commit | 40a544bac1f8799d40df377a90fb89d24fc2803c (patch) | |
tree | 4caa0a7b4da74e3eb03a09dd53f3a210082a1874 /src/main.zig | |
parent | bb163d731dbea91246b3882a0ba09f9b6db9ea8a (diff) | |
download | uf-master.tar.gz uf-master.tar.bz2 uf-master.zip |
Also add blank space between line being merged together.
Diffstat (limited to 'src/main.zig')
-rw-r--r-- | src/main.zig | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/src/main.zig b/src/main.zig index 14eb65c..21103ff 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,11 +1,12 @@ 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, out) catch |err| { + Uf.init().streamCopy(in.reader(), out.writer()) catch |err| { try std.io.getStdErr().writer().print("Error {}", .{err}); }; } @@ -22,12 +23,12 @@ const Uf = struct { return Self{}; } - fn flush(_: Self, out: Writer) !void { + fn flush(_: Self, out: anytype) !void { _ = try out.write(out_buf[0..out_index]); out_index = 0; } - fn writeChar(self: Self, out: Writer, c: u8) !void { + fn writeChar(self: Self, out: anytype, c: u8) !void { if (out_index >= buf_size) { try self.flush(out); } @@ -35,10 +36,7 @@ const Uf = struct { out_index += 1; } - fn streamCopy(self: Self, in: File, out: File) !void { - const reader = in.reader(); - const writer = out.writer(); - + fn streamCopy(self: Self, reader: anytype, writer: anytype) !void { var hit = false; var second = false; var in_buf: [buf_size]u8 = undefined; @@ -55,6 +53,8 @@ const Uf = struct { } else if (hit and c != '\n') { if (second) { try self.writeChar(writer, '\n'); + } else { + try self.writeChar(writer, ' '); } hit = false; second = false; @@ -73,3 +73,32 @@ const Uf = struct { } } }; + +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)); +} |