Skip to content

Delimiter detection does not ignore comments #2356

@BenPH

Description

@BenPH

Describe the bug
With DetectDelimiter = true and AllowComments = true, the delimiter detection includes commented lines in the lines where it's looking for delimiters. If a comment line does not include any delimiter then delimiter detection will fail.

See here for where the detection will fail if the commented line has no delimiters https://github.com/JoshClose/CsvHelper/blob/master/src/CsvHelper/Configuration/ConfigurationFunctions.cs#L234-L248.

To Reproduce

using CsvHelper;
using CsvHelper.Configuration;
using System.Globalization;
using System.Text;

class Program
{

    static void Main(string[] args)
    {
        var s = new StringBuilder();
        s.Append("#Some comment\r\n");
        s.Append("A;B;C\r\n");
        s.Append("1;2;3\r\n");
        var config = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            Comment = '#',
            AllowComments = true,
            DetectDelimiter = true,
            IgnoreBlankLines = true
        };
        using (var reader = new StringReader(s.ToString()))
        using (var csv = new CsvReader(reader, config))
        {
            var x = csv.GetRecords<Foo>().ToList();
        }
    }

    public class Foo
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }
}

Expected behavior
The delimiter detection should ignore any comment lines when AllowComments = true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions