Use line iterator to read file

This commit is contained in:
Paul Campbell 2021-12-30 07:25:58 +00:00
parent 1980d3a588
commit 7a56c8a662
2 changed files with 22 additions and 1 deletions

View file

@ -43,8 +43,27 @@ fn lineIterator(reader: fs.File.Reader, allocator: mem.Allocator) LineIterator {
}; };
} }
test "lineIterator returns lines in buffer" {
const file = try fs.cwd().openFile("src/test/two-lines.txt", .{ .read = true, .write = false });
defer file.close();
var reader = file.reader();
var it = lineIterator(reader, testing.allocator);
const line1 = it.next().?;
defer testing.allocator.free(line1);
try testing.expectEqualStrings("line 1", line1);
const line2 = it.next().?;
defer testing.allocator.free(line2);
try testing.expectEqualStrings("line 2", line2);
const eof = it.next();
try testing.expect(eof == null);
}
const LineIterator = struct { const LineIterator = struct {
reader: std.io.BufferedReader(4096, fs.File.Reader), reader: io.BufferedReader(4096, fs.File.Reader),
delimiter: u8, delimiter: u8,
allocator: mem.Allocator, allocator: mem.Allocator,

2
src/test/two-lines.txt Normal file
View file

@ -0,0 +1,2 @@
line 1
line 2