Search results

There are no results.

std.csv.Generator

class pub Generator[I: mut + Write]

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 code
Hide source code
fn pub static new(output: I) -> Generator[I] {
  Generator(output: output, separator_string: ',', separator_byte: COMMA)
}
fn pub static new(output: I: mut) -> Generator[I: mut]

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 code
Hide source code
fn pub mut flush -> Result[Nil, IoError] {
  @output.flush
}
fn pub mut flush -> Result[Nil, Error]

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 code
Hide source code
fn pub mut separator=(separator: ref Separator) {
  @separator_string = separator.to_string
  @separator_byte = separator.to_int
}
fn pub mut separator=(separator: ref 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 code
Hide source code
fn pub mut write(columns: ref Array[String]) -> Result[Nil, IoError] {
  let mut i = 0

  columns.iter.try_each(fn move (col) {
    if i > 0 { try @output.write_string(@separator_string) }

    if quote?(col, @separator_byte) {
      try @output
        .write_string('"')
        .then(fn (_) { @output.write_string(col.replace('"', '""')) })
        .then(fn (_) { @output.write_string('"') })
    } else {
      try @output.write_string(col)
    }

    i += 1
    Result.Ok(nil)
  })

  if columns.size > 0 { try @output.write_string('\r\n') }

  Result.Ok(nil)
}
fn pub mut write(columns: ref Array[String]) -> Result[Nil, Error]

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)