Csv Parser¶
| Spice | |
|---|---|
CsvParser struct¶
Parser for CSV (comma-separated values) input.
Supports quoted fields, embedded delimiters/newlines and escaped quotes (by doubling, e.g. "" inside a quoted field yields a single "). Both \n and \r\n are accepted as line terminators.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a CSV parser with the given control characters
Parameters
| Name | Type | Description |
|---|---|---|
delimiter |
char |
Field delimiter character (default: DEFAULT_DELIMITER) |
quote |
char |
Quote character used to wrap fields (default: DEFAULT_QUOTE) |
Methods¶
parseLine¶
| Spice | |
|---|---|
Parses a single CSV record into its fields. Newlines inside the input are preserved as part of the field they appear in.
Parameters
| Name | Type | Description |
|---|---|---|
input |
const String& |
Single CSV record (without trailing line terminator) |
Returns: Vector<String> — Parsed fields
parse¶
| Spice | |
|---|---|
Parses a whole CSV document into rows, where each row is a vector of fields.
An optional trailing line terminator is not treated as an additional empty record. Empty input yields an empty result.
Parameters
| Name | Type | Description |
|---|---|---|
input |
const String& |
Full CSV document |
Returns: Vector<Vector<String>> — Parsed rows
serializeField¶
| Spice | |
|---|---|
Serializes a single field, quoting it when necessary.
A field is wrapped in quotes when it contains the delimiter, the quote character or a line terminator. Embedded quotes are escaped by doubling, so the result round-trips through parseLine/parse. The empty field is also quoted (as ""): an empty record would otherwise serialize to an empty line, which parse does not reproduce as a row.
Parameters
| Name | Type | Description |
|---|---|---|
field |
const String& |
Field value to serialize |
Returns: String — Serialized (and possibly quoted) field
serializeRow¶
| Spice | |
|---|---|
Serializes a single row into one CSV record. Fields are separated by the configured delimiter; no trailing line terminator is appended.
Parameters
| Name | Type | Description |
|---|---|---|
row |
const Vector<String>& |
Fields of the record |
Returns: String — Serialized CSV record
serialize¶
| Spice | |
|---|---|
Serializes a whole document into CSV text. Records are separated by \r\n as recommended by RFC 4180; no trailing line terminator is appended, so parse(serialize(rows)) reproduces the input rows.
Parameters
| Name | Type | Description |
|---|---|---|
rows |
const Vector<Vector<String>>& |
Rows of the document |
Returns: String — Serialized CSV document