From 1980d3a5889ad2c96de31f874af58181acd775b0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 29 Dec 2021 20:00:03 +0000 Subject: [PATCH] refactoring --- src/main.zig | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main.zig b/src/main.zig index 4a6d4fd..d687376 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4,9 +4,8 @@ const os = builtin.os; const mem = std.mem; const testing = std.testing; const io = std.io; +const heap = std.heap; const fs = std.fs; -const File = fs.File; -const FileReader = File.Reader; // step 1: [x] read in a file from stdin and write out to stdout // step 2: [ ] read in a named file in parameters and write out to stdout @@ -17,33 +16,46 @@ const FileReader = File.Reader; pub fn main() anyerror!void { const stdin = io.getStdIn(); const stdout = io.getStdOut(); - try dumpInput(stdin, stdout); + var buffer: [4096]u8 = undefined; + var fba = heap.FixedBufferAllocator.init(&buffer); + const allocator = fba.allocator(); + try dumpInput(stdin, stdout, allocator); } const maxLineLength = 4096; -fn dumpInput(in: File, out: File) !void { - var buffer: [maxLineLength]u8 = undefined; +fn dumpInput(in: fs.File, out: fs.File, allocator: mem.Allocator) !void { const writer = out.writer(); const reader = in.reader(); - while (true) { - const input = try nextLine(reader, &buffer); - if (input) |line| { - try writer.print("{s}\n", .{ line }); - } else { - break; - } + var it = lineIterator(reader, allocator); + while (it.next()) |line| { + defer allocator.free(line); + + try writer.print("{s}\n", .{ windowsSafe(line) }); } } -fn nextLine(reader: FileReader, buffer: []u8) !?[]const u8 { - var line: []u8 = (try reader.readUntilDelimiterOrEof( - buffer, - '\n', - )) orelse return null; - return windowsSafe(line); +fn lineIterator(reader: fs.File.Reader, allocator: mem.Allocator) LineIterator { + return LineIterator { + .reader = io.bufferedReader(reader), + .delimiter = '\n', + .allocator = allocator + }; } +const LineIterator = struct { + reader: std.io.BufferedReader(4096, fs.File.Reader), + delimiter: u8, + allocator: mem.Allocator, + + const Self = @This(); + + /// Caller owns returned memory + pub fn next(self: *Self) ?[]u8 { + return self.reader.reader().readUntilDelimiterOrEofAlloc(self.allocator, self.delimiter, 4096) catch null; + } +}; + // trim annoying windows-only carriage return character fn windowsSafe(line: []const u8) []const u8 { if (os.tag == .windows) {