//! In-memory CSV table. //! //! Parses the entire input into a 2-D structure of owned strings. //! Suitable for small-to-moderate inputs where the convenience of //! random access outweighs the memory cost. For large inputs, prefer //! `RowIterator` (or one of its variants) or `FieldIterator`. //! //! All allocations made during `parse` live in an internal //! `ArenaAllocator`, so `deinit` is O(1) and mid-parse failures leak //! nothing. const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; const Dialect = @import("dialect.zig").Dialect; const Error = @import("error.zig").Error; const row_iterator_mod = @import("row_iterator.zig"); const RowIterator = row_iterator_mod.RowIterator; /// In-memory CSV table — the entire input parsed into an owned 2-D /// `[][][]const u8`. All memory is held by an internal /// `ArenaAllocator`; `deinit` frees everything in one shot. pub const Table = struct { /// All rows. `rows[i][j]` is the j-th field of the i-th row. All /// slices are owned by the table (lifetime = until `deinit`). rows: []const []const []const u8, /// Owns every byte the table holds; freed in a single call by /// `deinit`. arena: std.heap.ArenaAllocator, /// Default initial size of the per-field byte buffer used during /// parsing. Grown by the parser via `errdefer arena.deinit()`-safe /// reallocation if needed. pub const default_field_buf_size: usize = 4 * 1024; /// Parse the entire contents of `reader` into a `Table`. The /// caller must call `deinit()` to free all memory. On error, /// no memory is leaked. pub fn parse( reader: *Io.Reader, allocator: Allocator, dialect: Dialect, ) Error!Table { try dialect.validate(); var arena = std.heap.ArenaAllocator.init(allocator); errdefer arena.deinit(); const arena_alloc = arena.allocator(); // field_buf lives in the arena so we don't need to free it // separately; it also conveniently outlives the parser scope. const field_buf = try arena_alloc.alloc(u8, default_field_buf_size); var row_iter = try RowIterator.init(reader, field_buf, arena_alloc, dialect); // No need to deinit row_iter explicitly: its storage lives in // the arena. (Calling row_iter.deinit() on an arena-backed list // is a no-op besides; the arena reclaims everything at once.) var rows: std.ArrayListUnmanaged([]const []const u8) = .empty; // rows itself also lives in the arena. while (try row_iter.next()) |row| { const cells = try arena_alloc.alloc([]const u8, row.count()); for (row.fields, 0..) |f, i| { cells[i] = try arena_alloc.dupe(u8, f); } try rows.append(arena_alloc, cells); } const owned_rows = try rows.toOwnedSlice(arena_alloc); return .{ .rows = owned_rows, .arena = arena, }; } /// Releases every byte the table holds. pub fn deinit(self: *Table) void { self.arena.deinit(); } }; // ===== Tests ================================================================= const testing = std.testing; test "Table: empty input yields zero rows" { var reader = Io.Reader.fixed(""); var t = try Table.parse(&reader, testing.allocator, .{}); defer t.deinit(); try testing.expectEqual(@as(usize, 0), t.rows.len); } test "Table: round-trip a simple CSV" { var reader = Io.Reader.fixed("a,b\n1,2\n"); var t = try Table.parse(&reader, testing.allocator, .{}); defer t.deinit(); try testing.expectEqual(@as(usize, 2), t.rows.len); try testing.expectEqualStrings("a", t.rows[0][0]); try testing.expectEqualStrings("b", t.rows[0][1]); try testing.expectEqualStrings("1", t.rows[1][0]); try testing.expectEqualStrings("2", t.rows[1][1]); } test "Table: ragged rows are preserved" { var reader = Io.Reader.fixed("a,b,c\n1\nx,y\n"); var t = try Table.parse(&reader, testing.allocator, .{}); defer t.deinit(); try testing.expectEqual(@as(usize, 3), t.rows.len); try testing.expectEqual(@as(usize, 3), t.rows[0].len); try testing.expectEqual(@as(usize, 1), t.rows[1].len); try testing.expectEqual(@as(usize, 2), t.rows[2].len); } test "Table: quoted fields with embedded structurals" { var reader = Io.Reader.fixed("\"a,b\",\"c\nd\",\"e\"\"f\"\n"); var t = try Table.parse(&reader, testing.allocator, .{}); defer t.deinit(); try testing.expectEqual(@as(usize, 1), t.rows.len); try testing.expectEqualStrings("a,b", t.rows[0][0]); try testing.expectEqualStrings("c\nd", t.rows[0][1]); try testing.expectEqualStrings("e\"f", t.rows[0][2]); } test "Table: mid-parse error leaks nothing (UnterminatedQuotedField)" { var reader = Io.Reader.fixed("a,b\n\"oops"); try testing.expectError( error.UnterminatedQuotedField, Table.parse(&reader, testing.allocator, .{}), ); // testing.allocator's leak detector will fail the test if the // errdefer arena.deinit() didn't run. } test "Table: mid-parse error from invalid dialect" { var reader = Io.Reader.fixed("a,b\n"); try testing.expectError( error.InvalidDialect, Table.parse(&reader, testing.allocator, .{ .delimiter = '"', .quote_char = '"' }), ); } test "Table: returned slices remain valid through arena lifetime" { var reader = Io.Reader.fixed("hello,world\nfoo,bar\nbaz,qux\n"); var t = try Table.parse(&reader, testing.allocator, .{}); defer t.deinit(); // Touch every cell after some arena activity to confirm no // borrowed-buffer aliasing. var sum: usize = 0; for (t.rows) |row| for (row) |cell| { sum += cell.len; }; try testing.expectEqual(@as(usize, 22), sum); // 5+5 + 3+3 + 3+3 }