From 900506f6c6034b754bf05e1c647fc5ca6ee23203 Mon Sep 17 00:00:00 2001 From: nietras Date: Thu, 11 Jul 2024 15:41:38 +0200 Subject: [PATCH] Add SepReaderHeader.TryIndexOf (#146) --- README.md | 1 + src/Sep.Test/SepReaderHeaderTest.cs | 7 +++++++ src/Sep/SepReaderHeader.cs | 2 ++ 3 files changed, 10 insertions(+) diff --git a/README.md b/README.md index dbc3a5e..8779250 100644 --- a/README.md +++ b/README.md @@ -1687,6 +1687,7 @@ namespace nietras.SeparatedValues public void IndicesOf(System.ReadOnlySpan colNames, System.Span colIndices) { } public System.Collections.Generic.IReadOnlyList 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 { diff --git a/src/Sep.Test/SepReaderHeaderTest.cs b/src/Sep.Test/SepReaderHeaderTest.cs index f5c4090..9af102a 100644 --- a/src/Sep.Test/SepReaderHeaderTest.cs +++ b/src/Sep.Test/SepReaderHeaderTest.cs @@ -45,6 +45,13 @@ public void SepReaderHeaderTest_NotEmpty() AreEqual([0, 2], header.IndicesOf((ReadOnlySpan)["A", "C"])); AreEqual([2, 0], header.IndicesOf((IReadOnlyList)["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)["A", "C"], actualIndices); AreEqual([0, 2], actualIndices); diff --git a/src/Sep/SepReaderHeader.cs b/src/Sep/SepReaderHeader.cs index 0d77cf4..0fd597c 100644 --- a/src/Sep/SepReaderHeader.cs +++ b/src/Sep/SepReaderHeader.cs @@ -41,6 +41,8 @@ internal static SepReaderHeader Parse(Sep sep, string line, IEqualityComparer _colNameToIndex[colName]; + public bool TryIndexOf(string colName, out int colIndex) => _colNameToIndex.TryGetValue(colName, out colIndex); + public IReadOnlyList NamesStartingWith(string prefix, StringComparison comparison = StringComparison.Ordinal) { var colNames = new List();