Add @each_row for the csv module.

This commit is contained in:
Christoffer Lerno
2025-03-26 18:19:19 +01:00
parent 4538a1f50d
commit 5c46b0c2a0
2 changed files with 36 additions and 13 deletions

View File

@@ -69,13 +69,11 @@ fn void? CsvReader.skip_row(self) @maydiscard => @pool()
(void)io::treadline(self.stream);
}
macro void? CsvReader.@each_row(self, int rows = int.max; @body(String[] row)) @maydiscard
macro void? @each_row(InStream stream, String separator = ",", int max_rows = int.max; @body(String[] row)) @maydiscard
{
InStream stream = self.stream;
String sep = self.separator;
while (rows--)
while (max_rows--)
{
@stack_mem(512; Allocator mem)
@stack_mem(512; mem)
{
String? s = io::readline(mem, stream);
if (catch err = s)
@@ -83,7 +81,15 @@ macro void? CsvReader.@each_row(self, int rows = int.max; @body(String[] row)) @
if (err == io::EOF) return;
return err?;
}
@body(s.split(mem, sep));
@body(s.split(mem, separator));
};
}
}
macro void? CsvReader.@each_row(self, int rows = int.max; @body(String[] row)) @maydiscard
{
return @each_row(self.stream, self.separator, rows; row)
{
@body(row);
};
}