Skip to content

Commit 7640b38

Browse files
Add new Split overload for C# 14 (#789)
1 parent 3d3ca7f commit 7640b38

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

Source/SuperLinq/Split.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@ TSource separator
3535
return Split(source, separator, count: int.MaxValue);
3636
}
3737

38+
/// <summary>
39+
/// Splits the source sequence by a separator.
40+
/// </summary>
41+
/// <typeparam name="TSource">
42+
/// Type of element in the source sequence.
43+
/// </typeparam>
44+
/// <param name="source">
45+
/// The source sequence.
46+
/// </param>
47+
/// <param name="separator">
48+
/// Separator element.
49+
/// </param>
50+
/// <exception cref="ArgumentNullException">
51+
/// <paramref name="source"/> is <see langword="null"/>.
52+
/// </exception>
53+
/// <returns>
54+
/// A sequence of splits of elements.
55+
/// </returns>
56+
/// <remarks>
57+
/// <para>
58+
/// This method is implemented by using deferred execution and streams the groupings. The grouping elements,
59+
/// however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next
60+
/// grouping occurs.
61+
/// </para>
62+
/// </remarks>
63+
// Exists because in C# 14, `MemoryExtensions.Split()` binds tighter
64+
public static IEnumerable<IReadOnlyList<TSource>> Split<TSource>(
65+
this TSource[] source,
66+
TSource separator
67+
)
68+
{
69+
return Split(source, separator, count: int.MaxValue);
70+
}
71+
3872
/// <summary>
3973
/// Splits the source sequence by a separator given a maximum count of splits.
4074
/// </summary>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Needs to be in non `SuperLinq` namespace for proper testing of overload resolution
2+
#pragma warning disable IDE0130 // Namespace does not match folder structure
3+
4+
namespace Not.SuperLinq.Tests;
5+
6+
public sealed class SplitArrayTest
7+
{
8+
9+
[Fact]
10+
public void SplitArray()
11+
{
12+
var sequence = Enumerable.Range(1, 10).ToArray();
13+
var result = sequence.Split(5).ToList();
14+
15+
result
16+
.AssertSequenceEqual(
17+
[
18+
Enumerable.Range(1, 4),
19+
Enumerable.Range(6, 5),
20+
]
21+
);
22+
}
23+
}

Tests/SuperLinq.Tests/SplitTest.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
31
namespace SuperLinq.Tests;
42

53
public sealed class SplitTest
@@ -11,6 +9,21 @@ public void SplitIsLazy()
119
_ = new BreakingSequence<int>().Split(1, 2);
1210
}
1311

12+
[Fact]
13+
public void Split()
14+
{
15+
using var sequence = Enumerable.Range(1, 10).AsTestingSequence();
16+
var result = sequence.Split(5);
17+
18+
result
19+
.AssertSequenceEqual(
20+
[
21+
Enumerable.Range(1, 4),
22+
Enumerable.Range(6, 5),
23+
]
24+
);
25+
}
26+
1427
[Fact]
1528
public void SplitWithComparer()
1629
{
@@ -28,20 +41,18 @@ public void SplitWithComparerUptoMaxCount()
2841
}
2942

3043
[Fact]
31-
[SuppressMessage("Style", "IDE0305:Simplify collection initialization")]
3244
public void SplitWithSeparatorAndResultTransformation()
3345
{
3446
using var sequence = "the quick brown fox".AsTestingSequence();
35-
var result = sequence.Split(' ', chars => new string(chars.ToArray()));
47+
var result = sequence.Split(' ', chars => new string([.. chars]));
3648
result.AssertSequenceEqual("the", "quick", "brown", "fox");
3749
}
3850

3951
[Fact]
40-
[SuppressMessage("Style", "IDE0305:Simplify collection initialization")]
4152
public void SplitUptoMaxCount()
4253
{
4354
using var sequence = "the quick brown fox".AsTestingSequence();
44-
var result = sequence.Split(' ', 2, chars => new string(chars.ToArray()));
55+
var result = sequence.Split(' ', 2, chars => new string([.. chars]));
4556
result.AssertSequenceEqual("the", "quick", "brown fox");
4657
}
4758

0 commit comments

Comments
 (0)