From 23142e2ea6740d4e1be628acd33589f42c18076b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 20 Nov 2022 19:31:34 -0800 Subject: [PATCH] config file allows and ignores empty lines, supports comments --- src/cli_args.zig | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/cli_args.zig b/src/cli_args.zig index 4167b6085..c36087cfe 100644 --- a/src/cli_args.zig +++ b/src/cli_args.zig @@ -333,10 +333,22 @@ 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 { - // TODO: handle errors - unreachable; - } orelse return null; + 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 @@ -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()); }