Skip to content

Commit

Permalink
safenames: trim spaces/quotes from headers
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
jqnatividad committed Jan 14, 2023
1 parent 17c2c51 commit 0260833
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cmd/safenames.rs
Expand Up @@ -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,
Expand Down

0 comments on commit 0260833

Please sign in to comment.