code wiki / (root) / csv_writer.nx

csv_writer.nx

buildroot/runtime/csv_writer.nx

4739 B144 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind tooltopic csv
docsdependenciesstructsconstsfunctions

about

csv_writer.nx -- RFC 4180 CSV serialiser. Companion to csv.nx (reader). Writes cells with quoting that csv.nx can roundtrip: - Cells without any of \",\\n\\r,\" are emitted raw - Cells containing any of those are wrapped in \"...\" with embedded \" doubled to \"\" - Lines end with CRLF per RFC (not bare LF) Stream API: csvw_field(out, cap, off, data, n) -> off' (writes cell) csvw_end_row(out, cap, off) -> off' (writes CRLF) Caller tracks \"is this the first cell on the line\" by comparing the returned offset to the offset before writing; csvw_field inserts a \",\" separator when writing into the middle of a row. To simplify, we expose csvw_begin_row which resets the comma- separator flag stored on a CsvWriter state struct. Invariants: CW1 Roundtrip safe: csv.nx parsing our output yields the original cell bytes (after csv_decode_quoted for quoted cells). CW2 Empty string fields are OK -- they write as a bare \",\" boundary (or \"\"\"\" at end of row for unambiguous parsing per RFC 4180 ยง2.4). CW3 Output uses CRLF (0x0D 0x0A) line endings.

dependencies 1 imports · 0 importers

syscalls.nx csv_writer.nx

imports: syscalls.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main csvw_init csvw_field cw_needs_quoting csvw_end_row

structs

33struct CsvWriter {

consts

31const CW_ERR_SHORT: i64 = -1

functions

39func csvw_init(w: *CsvWriter) -> i64 {
called by 1: main
45func cw_needs_quoting(data: *u8, n: i64) -> i64 {
called by 1: csvw_field
60func csvw_field(out: *u8, cap: i64, off: i64, w: *CsvWriter,
called by 1: main calls 1: cw_needs_quoting
102func csvw_end_row(out: *u8, cap: i64, off: i64, w: *CsvWriter) -> i64 {
called by 1: main
111func main() -> i64 {