diff --git a/finnow-api/source/util/csv.d b/finnow-api/source/util/csv.d index 2458e52..385d613 100644 --- a/finnow-api/source/util/csv.d +++ b/finnow-api/source/util/csv.d @@ -27,7 +27,14 @@ struct CsvStreamWriter(S) if (isByteOutputStream!S) { result = stream.writeToStream([',']); throwIfError(result); } + import std.regex : replaceAll, regex; + import std.string : indexOf; string s = value.to!string(); + bool shouldValueBeQuoted = indexOf(s, '"') != -1 || indexOf(s, ',') != -1; + s = replaceAll(value.to!string(), regex("\""), "\"\""); + if (shouldValueBeQuoted) { + s = "\"" ~ s ~ "\""; + } result = stream.writeToStream(cast(ubyte[]) s); throwIfError(result); atStartOfRow = false;