From 0260833bc8b36ea6e6ccb9e79687c76470a8a6b0 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Fri, 13 Jan 2023 20:46:29 -0500 Subject: [PATCH] `safenames`: trim spaces/quotes from headers before applying safenames fn. Let the csv library handle quoting on headers. This should fix safenames working on unfortunately high-ranking malformed CSV examples on https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html when people search for "examples CSV" --- src/cmd/safenames.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cmd/safenames.rs b/src/cmd/safenames.rs index b955d73f2..84a9f21da 100644 --- a/src/cmd/safenames.rs +++ b/src/cmd/safenames.rs @@ -158,8 +158,15 @@ pub fn run(argv: &[&str]) -> CliResult<()> { let mut headers = csv::StringRecord::from_byte_record_lossy(old_headers.clone()); if let SafeNameMode::Conditional | SafeNameMode::Always = safenames_mode { + // trim enclosing quotes and spaces from headers as it messes up safenames + // csv library will automatically add quotes when necessary when we write it + let mut noquote_headers = csv::StringRecord::new(); + for header in &headers { + noquote_headers.push_field(header.trim_matches(|c| c == '"' || c == ' ')); + } + let (safe_headers, changed_count) = util::safe_header_names( - &headers, + &noquote_headers, true, safenames_mode == SafeNameMode::Conditional, &reserved_names_vec,