Skip to content

Commit

Permalink
Add SepReaderOptions.ColNameComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
nietras committed Mar 20, 2024
1 parent 6757c35 commit 036b774
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ public CultureInfo? CultureInfo { get; init; } = SepDefaults.CultureInfo;
/// </summary>
public bool HasHeader { get; init; } = true;
/// <summary>
/// Specifies <see cref="IEqualityComparer{T}" /> to use
/// for comparing header column names and index look up.
/// </summary>
public IEqualityComparer<string> ColNameComparer { get; init; } = SepDefaults.ColNameComparer;
/// <summary>
/// Specifies the method factory used to convert a column span
/// of `char`s to a `string`.
/// </summary>
Expand Down Expand Up @@ -1500,6 +1505,7 @@ namespace nietras.SeparatedValues
public delegate nietras.SeparatedValues.SepToString SepCreateToString(nietras.SeparatedValues.SepHeader? maybeHeader, int colCount);
public static class SepDefaults
{
public static System.StringComparer ColNameComparer { get; }
public static System.Globalization.CultureInfo CultureInfo { get; }
public static char Separator { get; }
}
Expand Down Expand Up @@ -1627,6 +1633,7 @@ namespace nietras.SeparatedValues
{
public SepReaderOptions() { }
public SepReaderOptions(nietras.SeparatedValues.Sep? sep) { }
public System.Collections.Generic.IEqualityComparer<string> ColNameComparer { get; init; }
public nietras.SeparatedValues.SepCreateToString CreateToString { get; init; }
public System.Globalization.CultureInfo? CultureInfo { get; init; }
public bool DisableColCountCheck { get; init; }
Expand Down
4 changes: 3 additions & 1 deletion src/Sep/SepDefaults.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace nietras.SeparatedValues;
Expand All @@ -10,6 +11,7 @@ public static class SepDefaults

public static char Separator => _separator;
public static CultureInfo CultureInfo { get; } = CultureInfo.InvariantCulture;
public static StringComparer ColNameComparer { get; } = StringComparer.Ordinal;

internal static IReadOnlyList<char> AutoDetectSeparators { get; } =
new[] { Separator, '\t', '|', ',' };
Expand Down
7 changes: 5 additions & 2 deletions src/Sep/SepHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public SepHeader(string row, Dictionary<string, int> colNameToIndex)

public static SepHeader Empty { get; } = new(string.Empty, new Dictionary<string, int>());

internal static SepHeader Parse(Sep sep, string line)
internal static SepHeader Parse(Sep sep, string line) =>
Parse(sep, line, SepDefaults.ColNameComparer);

internal static SepHeader Parse(Sep sep, string line, IEqualityComparer<string> comparer)
{
var colNames = sep.Split(line);
var colNameToIndex = new Dictionary<string, int>(colNames.Length);
var colNameToIndex = new Dictionary<string, int>(colNames.Length, comparer);
for (var i = 0; i < colNames.Length; i++)
{
var colName = colNames[i];
Expand Down
2 changes: 1 addition & 1 deletion src/Sep/SepReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal void Initialize(SepReaderOptions options)
_colCountExpected = firstRowColCount;
if (options.HasHeader)
{
var colNameToIndex = new Dictionary<string, int>(firstRowColCount);
var colNameToIndex = new Dictionary<string, int>(firstRowColCount, options.ColNameComparer);
for (var colIndex = 0; colIndex < firstRowColCount; colIndex++)
{
var colName = ToStringDirect(colIndex);
Expand Down
9 changes: 8 additions & 1 deletion src/Sep/SepReaderOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;

namespace nietras.SeparatedValues;

Expand All @@ -11,6 +12,7 @@ public SepReaderOptions(Sep? sep)
Sep = sep;
CultureInfo = SepDefaults.CultureInfo;
HasHeader = true;
ColNameComparer = SepDefaults.ColNameComparer;
CreateToString = SepToString.Direct;
DisableFastFloat = false;
DisableColCountCheck = false;
Expand All @@ -31,6 +33,11 @@ public SepReaderOptions(Sep? sep)
/// </summary>
public bool HasHeader { get; init; } = true;
/// <summary>
/// Specifies <see cref="IEqualityComparer{T}" /> to use
/// for comparing header column names and index look up.
/// </summary>
public IEqualityComparer<string> ColNameComparer { get; init; } = SepDefaults.ColNameComparer;
/// <summary>
/// Specifies the method factory used to convert a column span
/// of `char`s to a `string`.
/// </summary>
Expand Down

0 comments on commit 036b774

Please sign in to comment.