CSVImporter icon indicating copy to clipboard operation
CSVImporter copied to clipboard

"Fatal error: Index out of range" when both ',' and '\n' are within a quoted cell

Open AndrewBennet opened this issue 7 years ago • 2 comments

I get an index out of range error when I try to import a CSV file which has both a newline and a comma within a single (quoted) cell. It does not crash when I only have a newline, or I only have a comma.

See this Swift Playground for a full reproduction of the error. The failing code is also copied below:

let csvFileWithCommaAndNewline = "Column 1,Column 2,Column 3\n"
    + "cell 1.1,cell 1.2,cell 1.3\n"
    + "cell 2.1,\"cell 2.2, with comma and \n newline\",cell 2.3"

let importerWithCommaAndNewline = CSVImporter<Row>(contentString: csvFileWithCommaAndNewline)
importerWithCommaAndNewline.importRecords(structure: {print($0)}, recordMapper: {
    return Row(cell1: $0["Column 1"]!, cell2: $0["Column 2"]!, cell3: $0["Column 3"]!)
})

AndrewBennet avatar Apr 14 '17 10:04 AndrewBennet

Thank you for reporting this, I will have a look at this shortly. If you need it right away, feel free to fix it yourself and provide a pull request. Just make sure that you write a test for this (you already have your example) so this stays fixed in future releases.

Jeehut avatar Apr 15 '17 08:04 Jeehut

This is pretty serious issue. Fix?

Agnakaraara avatar Feb 16 '24 12:02 Agnakaraara