csv

CSV (Comma-Separated Values) reader and writer. Parse CSV data into rows, extract individual fields as strings or integers, and write rows back to CSV format.

Usage

Reading CSV

import csv

fn main() {
    let data = "name,age,city\nAlice,30,NYC\nBob,25,LA"
    let len = 37

    // Parse all rows
    let rows = csv_parse_all(data, len)
    let count = csv_row_count(rows)
    print(count)   // 3 (including header)

    // Get a specific row
    let row1 = csv_get_row(rows, 1)     // "Alice,30,NYC"
    print(csv_field_count(row1))        // 3

    // Extract fields
    let buf: &i8 = alloc_pages(1)
    let flen = csv_field(row1, 0, buf)  // copies "Alice" to buf
    let age = csv_field_int(row1, 1)    // 30
    print(age)
}

Writing CSV

import csv

fn main() {
    // Prepare field data
    let fields: &i64 = alloc_pages(1)
    let lengths: &i64 = alloc_pages(1)
    fields[0] = "Alice"
    lengths[0] = 5
    fields[1] = "30"
    lengths[1] = 2
    fields[2] = "NYC"
    lengths[2] = 3

    let out: &i8 = alloc_pages(1)
    let n = csv_write_row(fields, lengths, 3, out)
    // out[0..n] contains "Alice,30,NYC"
}

Function Reference

FunctionSignatureDescription
csv_parse_row(src: &i8, len: i64) -> &i64Parse a single CSV row
csv_field_count(row: &i64) -> i64Number of fields in a row
csv_field(row: &i64, idx: i64, dst: &i8) -> i64Copy field to buffer
csv_field_int(row: &i64, idx: i64) -> i64Parse field as integer
csv_parse_all(src: &i8, len: i64) -> &i64Parse all rows from CSV data
csv_row_count(rows: &i64) -> i64Number of parsed rows
csv_get_row(rows: &i64, idx: i64) -> &i64Get row by index
csv_write_row(fields: &i64, lengths: &i64, count: i64, dst: &i8) -> i64Write a CSV row

Detailed API

csv_parse_row

fn csv_parse_row(src: &i8, len: i64) -> &i64

Parse a single CSV row (one line) from the source buffer. Fields are separated by commas. The returned pointer is a row handle used with csv_field_count, csv_field, and csv_field_int.

let row = csv_parse_row("Alice,30,NYC", 12)
print(csv_field_count(row))   // 3

Returns: A CsvRow pointer (opaque handle).

csv_field_count

fn csv_field_count(row: &i64) -> i64

Get the number of fields in a parsed row.

let row = csv_parse_row("a,b,c,d", 7)
print(csv_field_count(row))   // 4

csv_field

fn csv_field(row: &i64, idx: i64, dst: &i8) -> i64

Copy the field at index idx to the destination buffer dst. The caller must ensure dst is large enough.

let row = csv_parse_row("hello,world", 11)
let buf: &i8 = alloc_pages(1)

let len = csv_field(row, 0, buf)
// buf[0..len] contains "hello"
print(len)   // 5

let len2 = csv_field(row, 1, buf)
// buf[0..len2] contains "world"
print(len2)  // 5

Returns: Length of the copied field.

csv_field_int

fn csv_field_int(row: &i64, idx: i64) -> i64

Parse the field at index idx as a decimal integer. Useful for numeric CSV columns.

let row = csv_parse_row("Alice,30,95", 11)
let age = csv_field_int(row, 1)
print(age)    // 30
let score = csv_field_int(row, 2)
print(score)  // 95

Returns: The parsed integer value.

csv_parse_all

fn csv_parse_all(src: &i8, len: i64) -> &i64

Parse an entire CSV document (multiple lines). Each line becomes a row. Use csv_row_count and csv_get_row to iterate.

let data = "name,age\nAlice,30\nBob,25\n"
let rows = csv_parse_all(data, 24)
print(csv_row_count(rows))   // 3

let row0 = csv_get_row(rows, 0)   // header row
let row1 = csv_get_row(rows, 1)   // first data row

Returns: A handle to the parsed rows (vec of CsvRow pointers).

csv_row_count

fn csv_row_count(rows: &i64) -> i64

Get the number of rows in a parsed CSV document.

let rows = csv_parse_all("a\nb\nc\n", 6)
print(csv_row_count(rows))   // 3

csv_get_row

fn csv_get_row(rows: &i64, idx: i64) -> &i64

Get the row at the given index (0-based).

let rows = csv_parse_all("x,y\n1,2\n3,4", 11)
let row2 = csv_get_row(rows, 2)
print(csv_field_int(row2, 0))   // 3
print(csv_field_int(row2, 1))   // 4

Returns: A CsvRow pointer.

csv_write_row

fn csv_write_row(fields: &i64, lengths: &i64, count: i64, dst: &i8) -> i64

Write a CSV row to the destination buffer. fields is an array of &i8 pointers to field data, and lengths is a parallel array of field lengths. Fields are joined with commas.

let fields: &i64 = alloc_pages(1)
let lengths: &i64 = alloc_pages(1)

fields[0] = "Jda"
lengths[0] = 3
fields[1] = "2026"
lengths[1] = 4

let out: &i8 = alloc_pages(1)
let n = csv_write_row(fields, lengths, 2, out)
// out[0..n] contains "Jda,2026"
print(n)   // 8

Returns: Number of bytes written to dst.