Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CsvHelper/Configuration/ConfigurationFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static string GetDelimiter(GetDelimiterArgs args)
var match = Regex.Match(text, newLine);
var line = match.Success ? text.Substring(0, match.Index) : text;

if (line.Length > 0)
if (line.Length > 0 && !(config.AllowComments && line[0] == config.Comment))
{
var delimiterCounts = new Dictionary<string, int>();
foreach (var delimiter in config.DetectDelimiterValues)
Expand Down
16 changes: 16 additions & 0 deletions tests/CsvHelper.Tests/Parsing/DetectDelimiterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,21 @@ public void GetDelimiter_TextHasEmptyLines_DetectsDelimiter()
var delimeter = ConfigurationFunctions.GetDelimiter(new Delegates.GetDelimiterArgs(s.ToString(), config));
Assert.Equal(";", delimeter);
}

[Fact]
public void GetDelimiter_TextHasComments_DetectsDelimiter()
{
var s = new StringBuilder();
s.AppendLine("#comment,,,");
s.AppendLine("Id;Name");
s.AppendLine("1;2");
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
AllowComments = true,
Comment = '#',
DetectDelimiter = true,
};
Assert.Equal(";", ConfigurationFunctions.GetDelimiter(new Delegates.GetDelimiterArgs(s.ToString(), config)));
}
}
}