Skip to content

ddn.data.csv

High-performance CSV reader/writer with RFC 4180 compliance.

Module Declaration

module ddn.data.csv;

Enums

NewlinePolicy

public enum NewlinePolicy {
    DETECT,
    FORCE_CRLF,
    FORCE_LF
}

Newline policy controls how record boundaries are detected and how newlines are emitted by the writer.

Value Description
DETECT Detect CRLF and LF during reading; writer uses CRLF by default
FORCE_CRLF Force CRLF (\r\n) handling; writer emits CRLF
FORCE_LF Force LF (\n) handling; writer emits LF

EscapeStyle

public enum EscapeStyle {
    NONE,
    BACKSLASH
}

Escape style for non-RFC datasets (optional extension).

Value Description
NONE Only RFC 4180 double-quote escaping inside quoted fields
BACKSLASH Treat backslash as escape for delimiter/quote/newline in unquoted fields

ErrorMode

public enum ErrorMode {
    PERMISSIVE,
    FAIL_FAST
}

Reader error handling mode.

Value Description
PERMISSIVE Malformed rows are skipped and errors are counted; iteration continues
FAIL_FAST Stop iteration at the first error

Manifest Constants

DEFAULT_DELIMITER

enum char DEFAULT_DELIMITER = ',';

Default delimiter (RFC 4180).


DEFAULT_QUOTE

enum char DEFAULT_QUOTE = '"';

Default quote character (RFC 4180).


DEFAULT_DOUBLE_QUOTE

enum bool DEFAULT_DOUBLE_QUOTE = true;

Default: interpret doubled quotes inside quoted fields.


DEFAULT_TRIM_WHITESPACE

enum bool DEFAULT_TRIM_WHITESPACE = false;

Default: do not trim whitespace in unquoted fields.


DEFAULT_NEWLINE_POLICY

enum NewlinePolicy DEFAULT_NEWLINE_POLICY = NewlinePolicy.DETECT;

Default newline policy: detect CRLF and LF on read.


DEFAULT_ESCAPE_STYLE

enum EscapeStyle DEFAULT_ESCAPE_STYLE = EscapeStyle.NONE;

Default escape style: RFC only.


DEFAULT_HEADER

enum bool DEFAULT_HEADER = false;

Default: header row absent.


Structs

CsvDialect

public struct CsvDialect

Configuration options for CSV parsing and writing.

Fields

Field Type Default Description
delimiter char ',' Field delimiter character
quote char '"' Quote character for fields
doubleQuote bool true Escape quotes by doubling them
trimWhitespace bool false Trim whitespace in unquoted fields
newlinePolicy NewlinePolicy DETECT How to handle line endings
escapeStyle EscapeStyle NONE Escape style (RFC or backslash)
header bool false First row is header
strictFieldCount bool false Enforce consistent field count
errorMode ErrorMode PERMISSIVE Error handling mode
collectDiagnostics bool false Collect detailed error diagnostics

Constructor

this(char delimiter, char quote = '"', bool doubleQuote = true, 
     bool trimWhitespace = false, NewlinePolicy newlinePolicy = NewlinePolicy.DETECT,
     EscapeStyle escapeStyle = EscapeStyle.NONE, bool header = false);

Example:

// Custom dialect with semicolon delimiter
auto dialect = CsvDialect(';');

// Full configuration
auto dialect = CsvDialect(
    ',',                      // delimiter
    '"',                      // quote character
    true,                     // doubleQuote
    false,                    // trimWhitespace
    NewlinePolicy.DETECT,     // newline detection
    EscapeStyle.NONE,         // RFC 4180 escaping only
    true                      // header row present
);

FieldView

struct FieldView

A view into a single CSV field. Provides zero-copy access to field data.

Methods

toString
string toString() const;

Returns the field value as a string.

Returns: The field content as a string.


RowView

struct RowView

A view into a CSV row. Provides access to individual fields.

Methods

opIndex
FieldView opIndex(size_t index) const;

Access a field by index.

Parameter Description
index The zero-based field index

Returns: A FieldView for the specified field.


length
size_t length() const;

Returns the number of fields in the row.

Returns: The field count.


Aliases

CsvField

public alias CsvField = FieldView;

Shorthand for a parsed CSV field view.


CsvRow

public alias CsvRow = RowView;

Shorthand for a parsed CSV row view.


CsvResultT

public alias CsvResultT(T) = CsvResult!T;

Shorthand for CsvResult!T.


CsvReaderOf

public alias CsvReaderOf(R) = CsvReader!R;

Shorthand for CsvReader!R.


CsvWriterTo

public alias CsvWriterTo(S) = CsvWriter!S;

Shorthand for CsvWriter!S.


Templates

CsvReader

struct CsvReader(R)

CSV reader that parses CSV data from a range or string.

Constructor

this(R data, CsvDialect dialect = CsvDialect.init);
Parameter Description
data The CSV data source
dialect The CSV dialect configuration

Properties

empty
bool empty() const;

Returns true if there are no more rows to read.


front
RowView front();

Returns the current row.


stats
auto stats() const;

Returns parsing statistics including error count.


Methods

popFront
void popFront();

Advances to the next row.


CsvWriter

struct CsvWriter(S)

CSV writer that outputs CSV data to a sink.

Methods

putRow
void putRow(R)(R fields);

Writes a row of fields.

Parameter Description
fields A range of field values

Example:

auto writer = csvWriter();
writer.putRow(["name", "age", "city"]);
writer.putRow(["Alice", "30", "New York"]);

data
string data();

Returns the accumulated CSV data (for string-based writers).

Returns: The CSV output as a string.


CsvResult

struct CsvResult(T)

Result type for CSV field conversion operations.

Properties

isOk
bool isOk() const;

Returns true if the conversion succeeded.


value
T value() const;

Returns the converted value (only valid if isOk is true).


Functions

byRows

auto byRows(R)(R data, CsvDialect dialect = CsvDialect.init);

Creates a CSV reader that iterates over rows.

Parameter Description
data The CSV data source
dialect The CSV dialect configuration

Returns: A range of RowView objects.

Example:

const csv = "name,age,city\nAlice,30,New York\nBob,25,Los Angeles\n";

auto reader = byRows(csv);
reader.popFront();  // Skip header

while (!reader.empty) {
    auto row = reader.front;
    writeln(row[0].toString());  // Print name
    reader.popFront();
}

csvWriter

auto csvWriter(CsvDialect dialect = CsvDialect.init);

Creates a CSV writer with string output.

Parameter Description
dialect The CSV dialect configuration

Returns: A CsvWriter instance.

Example:

auto writer = csvWriter();
writer.putRow(["name", "age", "city"]);
writer.putRow(["Alice", "30", "New York"]);
string result = writer.data;

fromCsv

CsvResult!T fromCsv(T)(FieldView field);

Converts a CSV field to a typed value.

Parameter Description
field The field to convert

Returns: A CsvResult!T containing the converted value or an error.

Example:

auto row = reader.front;
auto intResult = fromCsv!int(row[0]);
auto floatResult = fromCsv!double(row[1]);
auto boolResult = fromCsv!bool(row[2]);

if (intResult.isOk) {
    int value = intResult.value;
}

RFC 4180 Compliance

The module implements RFC 4180 with the following behaviors:

  • Record Delimiters: CRLF per RFC; reader detects CRLF, LF, and legacy CR
  • Header Row: Optional; same field count as data rows when enabled
  • Field Delimiter: Comma by default; configurable to other single-byte delimiters
  • Quoted Fields: Fields containing delimiter, quote, or newline are quoted
  • Quote Escaping: Doubled quotes within quoted fields
  • Embedded Newlines: Supported inside quoted fields
  • Whitespace: Spaces are data; optional trimming for unquoted fields

See Also

  • RFC 4180 — Common Format and MIME Type for CSV Files