csv icon indicating copy to clipboard operation
csv copied to clipboard

Wrong escaping logic with nested double quotes and a comma at the end

Open adridele opened this issue 1 year ago • 2 comments

This csv row:

"Normal,\"quoted with nested \"double\" quotes, and comma at the end,\",normal 3,normal 4,normal 5"

will count 2 columns instead of 5.

Note, there is a nested double quotes, which with that alone it passes the test correctly (5 columns); but if you add a comma at the very end of the column (2nd column in this case) it will evaluate to only 2 columns instead of 5.

adridele avatar May 22 '24 06:05 adridele

I just realized that it would be helpful to represent this input text as a C# raw literal: """Normal,"quoted with nested ""double"" quotes, and comma at the end,",normal 3,normal 4,normal 5""" This literal will fail! Again note, there is a nested double quotes, which with that alone it passes the test correctly (5 columns); but if you add a comma at the very end of the column (2nd column in this case) it will evaluate to only 2 columns instead of 5.

Here is a test case:

//Arrange...
var options = new CsvOptions // Defaults
{
    Separator = ',',
    HeaderMode = HeaderMode.HeaderAbsent, // Assumes first row is a header row
    AllowNewLineInEnclosedFieldValues = true, // Respects new line (either \r\n or \n) characters inside field values enclosed in double quotes.
};
string input = """Normal,"quoted with nested ""double"" quotes, and comma at the end,",normal 3,normal 4,normal 5""";

//Act...
var data = CsvReader.ReadFromText(input, options).First();

//Assert...
Assert.AreEqual(5, data.ColumnCount);

adridele avatar Jun 18 '24 04:06 adridele

Stale issue message

github-actions[bot] avatar Aug 18 '24 00:08 github-actions[bot]