cli: strip CR in line iterator

This commit is contained in:
Gregory Anders
2024-01-05 09:21:34 -06:00
parent 20300f46da
commit 5fe2d03e96

View File

@ -712,8 +712,8 @@ pub fn LineIterator(comptime ReaderType: type) type {
unreachable; unreachable;
} orelse return null; } orelse return null;
// Trim any whitespace around it // Trim any whitespace (including CR) around it
const trim = std.mem.trim(u8, entry, whitespace); const trim = std.mem.trim(u8, entry, whitespace ++ "\r");
if (trim.len != entry.len) { if (trim.len != entry.len) {
std.mem.copyForwards(u8, entry, trim); std.mem.copyForwards(u8, entry, trim);
entry = entry[0..trim.len]; entry = entry[0..trim.len];
@ -833,3 +833,14 @@ test "LineIterator spaces around '='" {
try testing.expectEqual(@as(?[]const u8, null), iter.next()); try testing.expectEqual(@as(?[]const u8, null), iter.next());
try testing.expectEqual(@as(?[]const u8, null), iter.next()); try testing.expectEqual(@as(?[]const u8, null), iter.next());
} }
test "LineIterator with CRLF line endings" {
const testing = std.testing;
var fbs = std.io.fixedBufferStream("A\r\nB = C\r\n");
var iter = lineIterator(fbs.reader());
try testing.expectEqualStrings("--A", iter.next().?);
try testing.expectEqualStrings("--B=C", iter.next().?);
try testing.expectEqual(@as(?[]const u8, null), iter.next());
try testing.expectEqual(@as(?[]const u8, null), iter.next());
}