refactoring

This commit is contained in:
Paul Campbell 2021-12-29 18:36:02 +00:00
parent 2f5da29803
commit 3b4c64eaa5

View file

@ -1,6 +1,12 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const File = std.fs.File; const os = builtin.os;
const mem = std.mem;
const testing = std.testing;
const io = std.io;
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 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 // step 2: [ ] read in a named file in parameters and write out to stdout
@ -9,35 +15,39 @@ const File = std.fs.File;
// step 5: [ ] skip a number of tokens // step 5: [ ] skip a number of tokens
pub fn main() anyerror!void { pub fn main() anyerror!void {
const stdin = std.io.getStdIn(); const stdin = io.getStdIn();
const stdout = std.io.getStdOut(); const stdout = io.getStdOut();
try dumpInput(stdin, stdout); try dumpInput(stdin, stdout);
} }
const maxLineLength = 100;
fn dumpInput(in: File, out: File) !void { fn dumpInput(in: File, out: File) !void {
var buffer: [100]u8 = undefined; var buffer: [maxLineLength]u8 = undefined;
const writer = out.writer();
const reader = in.reader();
while (true) { while (true) {
const input = (try nextLine(in.reader(), &buffer)); const input = try nextLine(reader, &buffer);
if (input) |line| { if (input) |line| {
try out.writer().print("{s}\n", .{ line }); try writer.print("{s}\n", .{ line });
} else { } else {
break; break;
} }
} }
} }
fn nextLine(reader: anytype, buffer: []u8) !?[]const u8 { fn nextLine(reader: FileReader, buffer: []u8) !?[]const u8 {
var line = (try reader.readUntilDelimiterOrEof( var line = (try reader.readUntilDelimiterOrEof(
buffer, buffer,
'\n', '\n',
)) orelse return null; )) orelse return null;
// trim annoying windows-only carriage return character // trim annoying windows-only carriage return character
if (builtin.os.tag == .windows) { if (os.tag == .windows) {
line = std.mem.trimRight(u8, line, "\r"); line = mem.trimRight(u8, line, "\r");
} }
return line; return line;
} }
test "basic test" { test "basic test" {
try std.testing.expectEqual(10, 3 + 7); try testing.expectEqual(10, 3 + 7);
} }