config file allows and ignores empty lines, supports comments

This commit is contained in:
Mitchell Hashimoto
2022-11-20 19:31:34 -08:00
parent 01573819ea
commit 23142e2ea6

View File

@ -333,11 +333,23 @@ pub fn LineIterator(comptime ReaderType: type) type {
pub fn next(self: *Self) ?[]const u8 {
// TODO: detect "--" prefixed lines and give a friendlier error
const buf = self.r.readUntilDelimiterOrEof(self.entry[2..], '\n') catch {
const buf = buf: {
while (true) {
// Read the full line
const entry = self.r.readUntilDelimiterOrEof(self.entry[2..], '\n') catch {
// TODO: handle errors
unreachable;
} orelse return null;
// Trim any whitespace around it
const trim = std.mem.trim(u8, entry, " \t");
if (trim.len != entry.len) std.mem.copy(u8, entry, trim);
// Ignore empty lines
if (entry.len > 0 and entry[0] != '#') break :buf entry[0..trim.len];
}
};
// We need to reslice so that we include our '--' at the beginning
// of our buffer so that we can trick the CLI parser to treat it
// as CLI args.
@ -357,12 +369,30 @@ test "LineIterator" {
\\A
\\B
\\C
\\
\\# A comment
\\D
\\
\\ # An indented comment
\\ E
);
var iter = lineIterator(fbs.reader());
try testing.expectEqualStrings("--A", iter.next().?);
try testing.expectEqualStrings("--B", iter.next().?);
try testing.expectEqualStrings("--C", iter.next().?);
try testing.expectEqualStrings("--D", iter.next().?);
try testing.expectEqualStrings("--E", iter.next().?);
try testing.expectEqual(@as(?[]const u8, null), iter.next());
try testing.expectEqual(@as(?[]const u8, null), iter.next());
}
test "LineIterator end in newline" {
const testing = std.testing;
var fbs = std.io.fixedBufferStream("A\n\n");
var iter = lineIterator(fbs.reader());
try testing.expectEqualStrings("--A", iter.next().?);
try testing.expectEqual(@as(?[]const u8, null), iter.next());
try testing.expectEqual(@as(?[]const u8, null), iter.next());
}