forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.d
58 lines (49 loc) · 957 Bytes
/
csv.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module arsd.csv;
import std.string;
import std.array;
string[][] readCsv(string data) {
data = data.replace("\r", "");
auto idx = data.indexOf("\n");
//data = data[idx + 1 .. $]; // skip headers
string[] fields;
string[][] records;
string[] current;
int state = 0;
string field;
foreach(c; data) {
tryit: switch(state) {
default: assert(0);
case 0: // normal
if(c == '"')
state = 1;
else if(c == ',') {
// commit field
current ~= field;
field = null;
} else if(c == '\n') {
// commit record
current ~= field;
records ~= current;
current = null;
field = null;
} else
field ~= c;
break;
case 1: // in quote
if(c == '"')
state = 2;
else
field ~= c;
break;
case 2: // is it a closing quote or an escaped one?
if(c == '"') {
field ~= c;
state = 1;
} else {
state = 0;
goto tryit;
}
}
}
return records;
}