std.csv.Generator
type pub Generator[I: mut + Write[E], E]
A type for generating CSV data.
A Generator
writes its data to any type that implements std.iter.Write
,
allowing you to generate large amounts of CSV data without needing to buffer
the lines into memory. To generate a CSV line, simply call Generator.write
with an array of columns to write to a line.
The default separator used is a comma, this can be changed using the method
Generator.separator=
.
For line endings this type always uses \r\n
to conform to RFC 4180.
Examples
import std.csv (Generator)
import std.stdio (Stdout)
let gen = Generator.new(Stdout.new)
gen.write(['foo', 'bar', 'baz'])
gen.write(['1', '2', '3'])
Static methods
new
Show source codeHide source code
fn pub static new(output: I) -> Self {
Self(output: output, separator_string: ',', separator_byte: COMMA)
}
fn pub static new(output: I: mut) -> Generator[I: mut, E]
Returns a new Generator
that uses a comma as the column separator.
Examples
import std.csv (Generator)
import std.stdio (Stdout)
Generator.new(Stdout.new)
Instance methods
flush
Show source codeHide source code
fn pub mut flush -> Result[Nil, E] {
@output.flush
}
fn pub mut flush -> Result[Nil, E]
Flushes any pending writes for the underlying stream.
Errors
This method returns a std.io.Error
if the underlying stream failed to
flush the data.
Examples
import std.csv (Generator)
import std.stdio (Stdout)
let gen = Generator.new(Stdout.new)
gen.write(['foo', 'bar', 'baz']) # => Result.Ok(nil)
gen.flush # => Result.Ok(nil)
separator=
Show source codeHide source code
fn pub mut separator=(separator: ref Separator) {
@separator_string = separator.to_string
@separator_byte = separator.to_int
}
fn pub mut separator=(separator: Separator)
Changes the separator to use for separating columns to the given
Separator
.
Changing the separator only affects data written after a call to this method.
Examples
import std.csv (Generator, Separator)
import std.stdio (Stdout)
let gen = Generator.new(Stdout.new)
gen.separator = Separator.Tab
write
Show source codeHide source code
fn pub mut write(columns: ref Array[String]) -> Result[Nil, E] {
let mut i = 0
for col in columns.iter {
if i > 0 { try @output.write(@separator_string) }
if quote?(col, @separator_byte) {
try @output
.write('"')
.then(fn (_) { @output.write(col.replace('"', '""')) })
.then(fn (_) { @output.write('"') })
} else {
try @output.write(col)
}
i += 1
}
if columns.size > 0 { try @output.write('\r\n') }
Result.Ok(nil)
}
fn pub mut write(columns: ref Array[String]) -> Result[Nil, E]
Writes a single row of columns to the underlying stream.
This method doesn't explicitly flush any pending writes. If this is needed,
you should call Generator.flush
manually. Dropping a Generator
guarantees pending writes are flushed.
Errors
This method returns a std.io.Error
if the data couldn't be written to the
underlying stream.
Examples
import std.csv (Generator)
import std.stdio (Stdout)
let gen = Generator.new(Stdout.new)
gen.write(['foo', 'bar', 'baz']) # => Result.Ok(nil)