@std/data/csv.ms#
Import each required symbol with import Name from "@std/data/csv.ms".
CsvOptions Type#
Delimiter and line ending for parsing or writing CSV, and the row and cell bounds
for parse. Invalid values return an Error with kind EINVAL.
type CsvOptions {
delimiter String = ","
line_ending String = "\n"
max_rows Int? = null
max_cells Int? = null
}CsvOptions.delimiter#
One ASCII delimiter other than NUL, quote, CR or LF; defaults to comma.
delimiter String = ","CsvOptions.line_ending#
Row terminator when writing, either LF or CRLF; defaults to LF. Parsing accepts both.
line_ending String = "\n"CsvOptions.max_cells#
Maximum cells parse returns across all rows; null adds no cell budget.
max_cells Int? = nullCsvOptions.max_rows#
Maximum rows parse returns; null adds no row budget.
max_rows Int? = nullcsv_rows Function#
Reads CSV rows from a byte stream, one List[String] per record, with the cells parse returns.
Chunks may end anywhere, including inside a quoted cell, a CRLF or a UTF-8 scalar; only the
unfinished record is buffered. options.max_rows and options.max_cells are ignored here, so
the result never depends on chunk boundaries. Use max_record_bytes to bound the buffered
record; exceeding it fails the read with EOVERFLOW. A malformed record fails the read with
EBADMSG, and invalid options with EINVAL. Closing the result closes input.
fn csv_rows(move input ReadStream[Bytes], options CsvOptions = CsvOptions(), max_record_bytes Int? = null) ReadStream[List[String]] { ... }csv_writer Function#
Writes rows to a byte stream, each as stringify encodes it (quoting cells that need it, ending with the
options’ line ending). Invalid options fail the first write with EINVAL. Writes delegate to output’s sink, which may wait.
flush().wait() uses that sink’s completion condition; a pipe may retain up to its capacity of unread values. Closing the result closes output.
fn csv_writer(move output WriteStream[Bytes], options CsvOptions = CsvOptions()) WriteStream[List[String]] { ... }each_cell Function#
Calls f(row, column, cell) for every cell of text, in order, without building
rows. Rows and columns count from zero. A cell is a view of text that is valid only
during the call (clone it to keep it), except that a quoted cell holding escapes is a
fresh text. options and errors are as for parse; malformed input fails after the
cells before it were visited.
fn each_cell(text String, f fn(Int, Int, String) Void, options CsvOptions = CsvOptions()) Result[Void, Error] { ... }parse Function#
Parses CSV into rows of cells. Empty lines are skipped, and a quoted cell may hold
the delimiter, quotes (written "") and line breaks. options.max_rows and
options.max_cells bound large inputs and return EOVERFLOW when exceeded. Malformed
input returns an Error with kind EBADMSG, the line and column in details.
A CRLF inside a quoted cell reads as LF, so a cell holds one newline spelling
whatever the file’s line ending was.
fn parse(text String, options CsvOptions = CsvOptions()) Result[List[List[String]], Error] { ... }stringify Function#
Encodes rows as CSV, with comma delimiters and LF endings by default. A cell is
quoted only when it needs it, for example when it holds the delimiter, a quote or a
line break. Every row, including the last, ends with the line ending. max_bytes
bounds the output and returns EOVERFLOW when exceeded.
fn stringify(rows List[List[String]], options CsvOptions = CsvOptions(), max_bytes Int? = null) Result[String, Error] { ... }