Skip to content

Commit

Permalink
SepReader.Cols: Add CombinePathsToString (net9.0+ only due to Path.Co…
Browse files Browse the repository at this point in the history
…mbine(Span<>) only) (#253)
  • Loading branch information
nietras authored Jan 31, 2025
1 parent 985457a commit 8429e4f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,7 @@ namespace nietras.SeparatedValues
{
public int Count { get; }
public nietras.SeparatedValues.SepReader.Col this[int index] { get; }
public string CombinePathsToString() { }
public System.ReadOnlySpan<char> Join(System.ReadOnlySpan<char> separator) { }
public string JoinToString(System.ReadOnlySpan<char> separator) { }
public System.Span<T> Parse<T>()
Expand Down
11 changes: 11 additions & 0 deletions src/Sep.Test/SepReaderColsTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
#if NET9_0_OR_GREATER
using System.IO;
#endif
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -190,6 +193,14 @@ public void SepReaderColsTest_Join(string separator)
Run((cols, range) => Assert.AreEqual(string.Join(separator, _colTexts[range]), cols.JoinToString(separator)));
}

#if NET9_0_OR_GREATER
[TestMethod]
public void SepReaderColsTest_CombinePathsToString()
{
Run((cols, range) => Assert.AreEqual(Path.Combine(_colTexts[range]), cols.CombinePathsToString()));
}
#endif

static string ToString(SepReader.Col col) => col.ToString();

static void Run(ColsTestAction action, string text = Text, bool checkIndexOutOfRange = true)
Expand Down
6 changes: 6 additions & 0 deletions src/Sep/SepReader.Cols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public string JoinToString(ReadOnlySpan<char> separator) => IsIndices()
? _state.JoinToString(_colIndices, separator)
: _state.JoinToString(_colStartIfRange, _colIndices.Length, separator);

#if NET9_0_OR_GREATER
public string CombinePathsToString() => IsIndices()
? _state.CombinePathsToString(_colIndices)
: _state.CombinePathsToString(_colStartIfRange, _colIndices.Length);
#endif

bool IsIndices() => _colStartIfRange < 0;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
25 changes: 25 additions & 0 deletions src/Sep/SepReaderState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
#if NET9_0_OR_GREATER
using System.IO;
#endif
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static nietras.SeparatedValues.SepReader;
Expand Down Expand Up @@ -704,6 +707,17 @@ internal string JoinToString(ReadOnlySpan<int> colIndices, scoped ReadOnlySpan<c
return JoinToString(colRanges, separator);
}

#if NET9_0_OR_GREATER
[SkipLocalsInit]
internal string CombinePathsToString(ReadOnlySpan<int> colIndices)
{
if (colIndices.Length == 0) { return string.Empty; }
if (colIndices.Length == 1) { return ToStringDefault(colIndices[0]); }
var paths = ToStrings(colIndices);
return Path.Combine(paths);
}
#endif

void GetColRanges(ReadOnlySpan<int> colIndices, Span<SepRange> colRanges)
{
A.Assert(colIndices.Length == colRanges.Length);
Expand Down Expand Up @@ -824,6 +838,17 @@ internal string JoinToString(int colStart, int colCount, scoped ReadOnlySpan<cha
return JoinToString(colRanges, separator);
}

#if NET9_0_OR_GREATER
[SkipLocalsInit]
internal string CombinePathsToString(int colStart, int colCount)
{
if (colCount == 0) { return string.Empty; }
if (colCount == 1) { return ToStringDefault(colStart); }
var paths = ToStrings(colStart, colCount);
return Path.Combine(paths);
}
#endif

void GetColRanges(int colStart, Span<SepRange> colRanges)
{
for (var i = 0; i < colRanges.Length; i++)
Expand Down

0 comments on commit 8429e4f

Please sign in to comment.