Skip to content

Commit

Permalink
Add SepReaderHeader.TryIndexOf (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
nietras authored Jul 11, 2024
1 parent 0abb208 commit 900506f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,7 @@ namespace nietras.SeparatedValues
public void IndicesOf(System.ReadOnlySpan<string> colNames, System.Span<int> colIndices) { }
public System.Collections.Generic.IReadOnlyList<string> NamesStartingWith(string prefix, System.StringComparison comparison = 4) { }
public override string ToString() { }
public bool TryIndexOf(string colName, out int colIndex) { }
}
public readonly struct SepReaderOptions : System.IEquatable<nietras.SeparatedValues.SepReaderOptions>
{
Expand Down
7 changes: 7 additions & 0 deletions src/Sep.Test/SepReaderHeaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public void SepReaderHeaderTest_NotEmpty()
AreEqual([0, 2], header.IndicesOf((ReadOnlySpan<string>)["A", "C"]));
AreEqual([2, 0], header.IndicesOf((IReadOnlyList<string>)["C", "A"]));

var tryTrue = header.TryIndexOf("B", out var tryTrueIndex);
Assert.IsTrue(tryTrue);
Assert.AreEqual(1, tryTrueIndex);
var tryFalse = header.TryIndexOf("XX", out var tryFalseIndex);
Assert.IsFalse(tryFalse);
Assert.AreEqual(0, tryFalseIndex);

var actualIndices = new int[2];
header.IndicesOf((ReadOnlySpan<string>)["A", "C"], actualIndices);
AreEqual([0, 2], actualIndices);
Expand Down
2 changes: 2 additions & 0 deletions src/Sep/SepReaderHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ internal static SepReaderHeader Parse(Sep sep, string line, IEqualityComparer<st

public int IndexOf(string colName) => _colNameToIndex[colName];

public bool TryIndexOf(string colName, out int colIndex) => _colNameToIndex.TryGetValue(colName, out colIndex);

public IReadOnlyList<string> NamesStartingWith(string prefix, StringComparison comparison = StringComparison.Ordinal)
{
var colNames = new List<string>();
Expand Down

0 comments on commit 900506f

Please sign in to comment.