csv_writer.nx
buildroot/runtime/csv_writer.nx
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
imports: syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 33 | struct CsvWriter { |
consts
| 31 | const CW_ERR_SHORT: i64 = -1 |
functions
| 39 | func csvw_init(w: *CsvWriter) -> i64 {
called by 1: main |
| 45 | func cw_needs_quoting(data: *u8, n: i64) -> i64 {
called by 1: csvw_field |
| 60 | func csvw_field(out: *u8, cap: i64, off: i64, w: *CsvWriter, |
| 102 | func csvw_end_row(out: *u8, cap: i64, off: i64, w: *CsvWriter) -> i64 {
called by 1: main |
| 111 | func main() -> i64 { |