nx_csv_v1.nx
buildroot/runtime/nx_csv_v1.nx
about
csv.nx -- RFC 4180 comma-separated values parser.
Companion to tsv.nx. CSV is messier than TSV because fields
may be quoted, quotes inside quotes are doubled, and the line
separator is CRLF per spec (most real-world files use plain
LF -- we accept both).
RFC 4180 grammar (simplified):
file = header (CRLF record)* CRLF?
header/rec = field (, field)*
field = non_escaped | escaped
non_escaped = TEXTDATA*
escaped = DQUOTE (TEXTDATA | , | CR | LF | DQUOTE DQUOTE)* DQUOTE
DQUOTE = 0x22
We emit a FIELD CALLBACK at each cell boundary and a ROW
CALLBACK at each newline. This avoids allocating a 2D table
and lets caller stream megabyte+ files.
Invariants:
CSV1 Quoted fields handle embedded commas + newlines.
CSV2 Doubled quotes inside quoted fields ("") decode to one
literal DQUOTE -- caller handles via the callback (we
emit the raw quoted range; decoder below does the
unescape).
CSV3 Trailing CR before LF is stripped.
CSV4 Empty file -> 0 rows, 0 cells.
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 45 | struct CsvCell |
consts
| 36 | const CSV_MAGIC_1024: i64 = 1024 |
| 38 | const CSV_ERR_OVERFLOW: i64 = -1 |
| 39 | const CSV_ERR_UNTERM: i64 = -2 |
functions
| 55 | func csv_parse(buf: *u8, n: i64, called by 1: main |
| 147 | func csv_decode_quoted(buf: *u8, start: i64, end: i64, called by 1: main |
| 172 | func main() -> i64 |