Ignore extras by only counting line with a token

This commit is contained in:
Paul Campbell 2022-01-15 14:39:08 +00:00
parent f8f6dd4917
commit c3188ae353

View file

@ -144,26 +144,23 @@ fn dumpInput(config: Config, in: fs.File, out: fs.File, allocator: mem.Allocator
var c: usize = 0; var c: usize = 0;
while (c < config.lines) { while (c < config.lines) {
const line = it.next(); const line = it.next();
if (config.line) |match| { if (line) |memory| {
if (line) |memory| { if (config.line) |match| {
if (mem.eql(u8, match, memory)) { if (mem.eql(u8, match, memory)) {
c += 1; c += 1;
} }
} } else {
} else { if (config.token) |token| {
if (config.token) |token| { const occurances = mem.count(u8, memory, token);
if (line) |memory| { if (config.ignoreExtras and occurances > 0) {
if (config.ignoreExtras) {
c += 1; c += 1;
} else { } else {
c += mem.count(u8, memory, token); c += occurances;
} }
} else {
c += 1;
} }
} else {
c += 1;
} }
}
if (line) |memory| {
allocator.free(memory); allocator.free(memory);
} else return; } else return;
} }
@ -182,6 +179,7 @@ test "dumpInput skip 1 line" {
const config = Config{ const config = Config{
.lines = 1, .lines = 1,
.file = file, .file = file,
.ignoreExtras = false,
}; };
try dumpInput(config, file, output, testing.allocator); try dumpInput(config, file, output, testing.allocator);
@ -212,6 +210,7 @@ test "dumpInput skip 2 line 'alpha'" {
.lines = 2, .lines = 2,
.file = file, .file = file,
.line = "alpha", .line = "alpha",
.ignoreExtras = false,
}; };
try dumpInput(config, file, output, testing.allocator); try dumpInput(config, file, output, testing.allocator);