Skip to content
This repository has been archived by the owner on Dec 24, 2022. It is now read-only.

Commit

Permalink
Auto trim headers with leading/trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Nov 6, 2019
1 parent 6b32fb4 commit 8e83147
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/ServiceStack.Text/CsvReader.cs
Expand Up @@ -59,7 +59,8 @@ public static List<string> ParseLines(string csv)
return rows;
}

public static List<string> ParseFields(string line)
public static List<string> ParseFields(string line) => ParseFields(line, null);
public static List<string> ParseFields(string line, Func<string,string> parseFn)
{
var to = new List<string>();
if (string.IsNullOrEmpty(line))
Expand All @@ -70,7 +71,7 @@ public static List<string> ParseFields(string line)
while (++i <= len)
{
var value = EatValue(line, ref i);
to.Add(value.FromCsvField());
to.Add(parseFn != null ? parseFn(value.FromCsvField()) : value.FromCsvField());
}

return to;
Expand Down Expand Up @@ -355,7 +356,9 @@ public static List<T> Read(List<string> rows)

List<string> headers = null;
if (!CsvConfig<T>.OmitHeaders || Headers.Count == 0)
headers = CsvReader.ParseFields(rows[0]);
{
headers = CsvReader.ParseFields(rows[0], s => s.Trim());
}

if (typeof(T).IsValueType || typeof(T) == typeof(string))
{
Expand Down

0 comments on commit 8e83147

Please sign in to comment.