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);
};
}

View File

@@ -2,7 +2,8 @@ module csv_test @test;
import std::encoding::csv;
import std::io;
struct TestCase {
struct TestCase
{
String input;
String[] want;
String sep;
@@ -32,17 +33,33 @@ fn void csv_each_row()
","
},
};
foreach (t : tests) {
foreach (t : tests)
{
String[] want = t.want;
CsvReader r;
r.init((ByteReader){}.init(t.input), t.sep);
r.@each_row(; String[] row) {
foreach (i, s : row) {
assert(want.len > 0,
"more records than we want");
assert(s == want[0], "columns do not match; "
"got: '%s', want: '%s'", s, want[0]);
foreach (i, s : row)
{
assert(want.len > 0, "more records than we want");
assert(s == want[0], "columns do not match; got: '%s', want: '%s'", s, want[0]);
want = want[1..];
}
};
assert(want.len == 0, "not enough records found");
}
foreach (t : tests)
{
String[] want = t.want;
CsvReader r;
csv::@each_row((ByteReader){}.init(t.input), t.sep; String[] row)
{
foreach (i, s : row)
{
assert(want.len > 0, "more records than we want");
assert(s == want[0], "columns do not match; got: '%s', want: '%s'", s, want[0]);
want = want[1..];
}
};