ddn.data.csv¶
High-performance CSV reader/writer with RFC 4180 compliance.
Module Declaration¶
Enums¶
NewlinePolicy¶
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¶
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¶
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¶
Default delimiter (RFC 4180).
DEFAULT_QUOTE¶
Default quote character (RFC 4180).
DEFAULT_DOUBLE_QUOTE¶
Default: interpret doubled quotes inside quoted fields.
DEFAULT_TRIM_WHITESPACE¶
Default: do not trim whitespace in unquoted fields.
DEFAULT_NEWLINE_POLICY¶
Default newline policy: detect CRLF and LF on read.
DEFAULT_ESCAPE_STYLE¶
Default escape style: RFC only.
DEFAULT_HEADER¶
Default: header row absent.
Structs¶
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¶
A view into a single CSV field. Provides zero-copy access to field data.
Methods¶
toString¶
Returns the field value as a string.
Returns: The field content as a string.
RowView¶
A view into a CSV row. Provides access to individual fields.
Methods¶
opIndex¶
Access a field by index.
| Parameter | Description |
|---|---|
index |
The zero-based field index |
Returns: A FieldView for the specified field.
length¶
Returns the number of fields in the row.
Returns: The field count.
Aliases¶
CsvField¶
Shorthand for a parsed CSV field view.
CsvRow¶
Shorthand for a parsed CSV row view.
CsvResultT¶
Shorthand for CsvResult!T.
CsvReaderOf¶
Shorthand for CsvReader!R.
CsvWriterTo¶
Shorthand for CsvWriter!S.
Templates¶
CsvReader¶
CSV reader that parses CSV data from a range or string.
Constructor¶
| Parameter | Description |
|---|---|
data |
The CSV data source |
dialect |
The CSV dialect configuration |
Properties¶
empty¶
Returns true if there are no more rows to read.
front¶
Returns the current row.
stats¶
Returns parsing statistics including error count.
Methods¶
popFront¶
Advances to the next row.
CsvWriter¶
CSV writer that outputs CSV data to a sink.
Methods¶
putRow¶
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¶
Returns the accumulated CSV data (for string-based writers).
Returns: The CSV output as a string.
CsvResult¶
Result type for CSV field conversion operations.
Properties¶
isOk¶
Returns true if the conversion succeeded.
value¶
Returns the converted value (only valid if isOk is true).
Functions¶
byRows¶
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¶
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¶
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