std.csv

Reading and writing RFC 4180 CSV data.

Importuse std.csv

Functions

fn escape_field(field: string) -> string#

Quotes field (doubling embedded quotes) when it contains a comma, quote, or newline; returns it unchanged otherwise.

  • field: Field text to quote if it needs it.
println(std.csv.escape_field("moon, language")) // "moon, language"
println(std.csv.to_row(["moon", "language,tour"]))
println(std.csv.parse_row("moon,\"language,tour\""))
fn parse_row(line: string) -> [string]#

Splits one record into fields: commas separate, "..." quotes protect separators, "" inside quotes is a literal quote, bare \r outside quotes is dropped.

  • line: One record, without its terminating newline.
fn to_row(fields: [string]) -> string#

One record: fields joined by commas, each escaped like escape_field.

  • fields: Field values for one record, in column order.
fn parse_csv(text: string) -> [[string]]#

Parses a whole document into records of fields. Records split on newlines outside quotes; empty records are dropped.

  • text: A whole document; records split on newlines outside quotes.
let rows = std.csv.parse_csv("name,year\nmoon,2026")
println(std.csv.row_count(rows))
println(std.csv.col_count(rows))
println(std.csv.get_column(rows, 0))
println(std.csv.to_csv(rows))
fn to_csv(rows: [[string]]) -> string#

Rows joined by \n, each rendered like to_row. No trailing newline.

  • rows: Records to render, each a list of field values.
fn get_column(rows: [[string]], col: int) -> [string]#

Column col from each row that has it (rows too short are skipped).

  • rows: Records to read down.
  • col: Zero-based column index; rows too short are skipped.
fn row_count(rows: [[string]]) -> int#

The number of records.

  • rows: Records to count.
fn col_count(rows: [[string]]) -> int#

The first record's field count (0 when there are no records).

  • rows: Records whose first row sets the width.