From efd7d05bd1743049f9e49315382b2f920240712c Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Tue, 20 Jun 2017 20:04:20 -0400 Subject: [PATCH] write csv function --- csv.d | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/csv.d b/csv.d index 0060fd0..72fe093 100644 --- a/csv.d +++ b/csv.d @@ -58,3 +58,25 @@ string[][] readCsv(string data) { return records; } + +/// +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; +}