csvNextToken

Lower level control over parsing CSV

This function consumes the input. After each call the input will start with either a delimiter or record break (\n, \r\n, \r) which must be removed for subsequent calls.

void
csvNextToken
(
Range
Separator
Output
)
(
ref Range input
,
ref Output ans
,
Separator sep
,
Separator quote
,
bool startQuoted = false
)
if (
isSomeChar!Separator &&
&&
is(immutable ElementType!Range == immutable dchar)
&&
isOutputRange!(Output, dchar)
)

Parameters

input Range

Any CSV input

ans Output

The first field in the input

sep Separator

The character to represent a comma in the specification

quote Separator

The character to represent a quote in the specification

startQuoted bool

Whether the input should be considered to already be in quotes

Throws

IncompleteCellException When a quote is found in an unquoted field, data continues after a closing quote, or the quoted field was not closed before data was empty.

Examples

import std.array : appender;
import std.range.primitives : popFront;

string str = "65,63\n123,3673";

auto a = appender!(char[])();

csvNextToken(str,a,',','"');
assert(a.data == "65");
assert(str == ",63\n123,3673");

str.popFront();
a.shrinkTo(0);
csvNextToken(str,a,',','"');
assert(a.data == "63");
assert(str == "\n123,3673");

str.popFront();
a.shrinkTo(0);
csvNextToken(str,a,',','"');
assert(a.data == "123");
assert(str == ",3673");

Meta