-
-
Save soveran/e61407683440872577febaa8f868635c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const warn = std.debug.warn; | |
pub fn main() void { | |
var arg_it = std.process.args(); | |
const name = arg_it.inner.next(); | |
const text = arg_it.inner.next(); | |
if (text) |value| { | |
filterStdIn(value) catch |err| { | |
return; | |
}; | |
} else { | |
warn("usage: {} text\n", name); | |
return; | |
} | |
return; | |
} | |
fn filterStdIn(text: []const u8) !void { | |
var buf: [1024 * 4]u8 = undefined; | |
while (true) { | |
const line = std.io.readLineSlice(buf[0..]) catch |err| { | |
return err; | |
}; | |
try filter(line, text); | |
} | |
} | |
fn filter(line: []u8, text: []const u8) !void { | |
var stdout = try std.io.getStdOut(); | |
if (strstr(line, text)) { | |
try stdout.write(line); | |
try stdout.write("\n"); | |
} | |
} | |
fn strstr(str: []const u8, s: []const u8) bool { | |
if (s.len == 0) { | |
return false; | |
} | |
outer: for (str) |c, i| { | |
if (i + s.len > str.len) { | |
break; | |
} | |
for (s) |sc, si| { | |
if (sc != str[i + si]) { | |
continue :outer; | |
} | |
} | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment