summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--build.zig.zon15
-rw-r--r--src/main.zig43
3 files changed, 46 insertions, 14 deletions
diff --git a/.gitignore b/.gitignore
index ee7098f..dca1103 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
zig-out/
-zig-cache/
+.zig-cache/
diff --git a/build.zig.zon b/build.zig.zon
index 0d0a723..92b2535 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -1,10 +1,13 @@
.{
- .name = "uf",
- .version = "0.0.1",
-
- .dependencies = .{},
-
+ .name = .uf,
+ .version = "0.0.2",
+ .fingerprint = 0xb7405b217406f7f0,
+ .minimum_zig_version = "0.14.1",
+ .dependencies = .{
+ },
.paths = .{
- "",
+ "build.zig",
+ "build.zig.zon",
+ "src",
},
}
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));
+}