From 44b19ed36c9e83e1916c19ad149552b36468699f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sun, 15 Mar 2026 08:33:11 -0400 Subject: [PATCH] CSV escape processing. --- finnow-api/source/util/csv.d | 7 +++++++ 1 file changed, 7 insertions(+) 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;