Native
CSV::Native
A CSV parser and writer with a native fast path on Raku++, and a pure-Raku implementation everywhere else. No dependencies.
The XS pattern: the distribution ships C source, the build step compiles it against Raku++'s extension ABI, and the module uses it when it is there. On Rakudo ā or on a Raku++ without the built extension ā the same specification runs as plain Raku from this file. The same program runs on both.
Synopsis
use CSV::Native;
my @rows = from-csv("a,b\n1,\"x,y\"\n"); # [["a","b"],["1","x,y"]]
my @recs = from-csv("a,b\n1,2\n", :headers); # [{a => "1", b => "2"}]
my @recs = from-csv("data.csv".IO, :headers, :sep<;>);
say to-csv([[1, "two", "3,4"]]); # 1,two,"3,4"\n
say to-csv(@recs, :headers<b a>); # a header line, then rows
say csv-backend; # 'native' or 'raku'
The format
RFC 4180, read strictly and written minimally. Fields are Str, always ā a
CSV file carries no types, and inventing them would be the one thing a
drop-in parser must not do. A quoted field may hold the separator, the quote
(doubled) and line endings; anything after a closing quote other than a
separator or a line ending is an error, and so is a quote inside an unquoted
field. LF, CRLF and a lone CR all end a record; a trailing line ending does
not start an empty record, but a blank line in the middle is one record with
one empty field. A leading UTF-8 byte-order mark is dropped.
Every error names its line. The native and the Raku implementation raise the same message for the same input, which the test suite checks.