From c3188ae353071bde257a7d0bc8bfa41ac5fccc4a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 15 Jan 2022 14:39:08 +0000 Subject: [PATCH] Ignore extras by only counting line with a token --- src/main.zig | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index 78213b3..04c9a63 100644 --- a/src/main.zig +++ b/src/main.zig @@ -144,26 +144,23 @@ fn dumpInput(config: Config, in: fs.File, out: fs.File, allocator: mem.Allocator var c: usize = 0; while (c < config.lines) { const line = it.next(); - if (config.line) |match| { - if (line) |memory| { + if (line) |memory| { + if (config.line) |match| { if (mem.eql(u8, match, memory)) { c += 1; } - } - } else { - if (config.token) |token| { - if (line) |memory| { - if (config.ignoreExtras) { + } else { + if (config.token) |token| { + const occurances = mem.count(u8, memory, token); + if (config.ignoreExtras and occurances > 0) { c += 1; } else { - c += mem.count(u8, memory, token); + c += occurances; } + } else { + c += 1; } - } else { - c += 1; } - } - if (line) |memory| { allocator.free(memory); } else return; } @@ -182,6 +179,7 @@ test "dumpInput skip 1 line" { const config = Config{ .lines = 1, .file = file, + .ignoreExtras = false, }; try dumpInput(config, file, output, testing.allocator); @@ -212,6 +210,7 @@ test "dumpInput skip 2 line 'alpha'" { .lines = 2, .file = file, .line = "alpha", + .ignoreExtras = false, }; try dumpInput(config, file, output, testing.allocator);