forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.d
88 lines (74 loc) · 1.65 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/// My old csv code. Extremely basic functionality.
module arsd.csv;
import std.string;
import std.array;
/// Returns the array of csv rows from the given in-memory data (the argument is NOT a filename).
string[][] readCsv(string data) {
data = data.replace("\r\n", "\n");
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;
}
}
}
if(field !is null)
current ~= field;
if(current !is null)
records ~= current;
return records;
}
/// Formats the given rows into csv format. Use like `std.file.write(toCsv(...));`
string toCsv(string[][] rows) {
string data;
foreach(ridx, row; rows) {
if(ridx) data ~= "\n";
foreach(idx, cell; row) {
if(idx) data ~= ",";
if(cell.indexOf(",") != -1 || cell.indexOf("\"") != -1 || cell.indexOf("\n") != -1) {
data ~= "\"";
data ~= cell.replace(`"`, `""`);
data ~= "\"";
} else {
data ~= cell;
}
}
}
return data;
}