From 7e38c7106bbed43d6dc39777186d574722d476e8 Mon Sep 17 00:00:00 2001 From: Jan Polensky Date: Wed, 11 Jun 2025 19:07:23 +0200 Subject: [PATCH] libutil: Avoid quoting CSV header cells unless FMT_QUOTEALL is set This patch modifies the default behavior of CSV header generation: header cells are no longer enclosed in double quotes unless the FMT_QUOTEALL flag is explicitly set. According to RFC 4180, quoting is only required when a cell contains control characters, commas, or double quotes. The goal of this change is to produce cleaner and more readable CSV output by default, and to avoid unnecessary quoting in header rows. It also simplifies algorithms that rely on FMT_UTIL and improves compatibility with downstream tools expecting unquoted headers. Tools that expect unquoted headers include: * SQL tools: Headers optional, usually unquoted * R (read.csv): Uses header=TRUE, no quotes needed * Pandas: Assumes headers, quoting not required * Excel: Detects headers; quotes only for special chars Signed-off-by: Jan Polensky Reviewed-by: Peter Oberparleiter Signed-off-by: Steffen Eiden --- libutil/util_fmt.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libutil/util_fmt.c b/libutil/util_fmt.c index c7d78ea9..c0898fbf 100644 --- a/libutil/util_fmt.c +++ b/libutil/util_fmt.c @@ -307,9 +307,13 @@ static void add_key(const char *name, bool persist) key.persist = persist; util_add_array(&f.keys, &f.num_keys, key); if (f.type == FMT_CSV) { - hdr = csv_quote(name); - util_rec_def(f.csv_rec, name, UTIL_REC_ALIGN_LEFT, 0, hdr); - free(hdr); + if (f.quote_all) { + hdr = csv_quote(name); + util_rec_def(f.csv_rec, name, UTIL_REC_ALIGN_LEFT, 0, hdr); + free(hdr); + } else { + util_rec_def(f.csv_rec, name, UTIL_REC_ALIGN_LEFT, 0, name); + } util_rec_set(f.csv_rec, name, "\"\""); f.csv_hdr = true; }