Skip to content

Commit a29b6fc

Browse files
authored
List InsertRange Span (#149)
1 parent b677cee commit a29b6fc

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

api_list.include.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
* `Void AddRange<T>(ReadOnlySpan<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.addrange)
5252
* `Void CopyTo<T>(Span<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.copyto?view=net-8.0)
53+
* `Void InsertRange<T>(Int32, ReadOnlySpan<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.insertrange)
5354

5455

5556
#### DateTime

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ The class `Polyfill` includes the following extension methods:
407407

408408
* `Void AddRange<T>(ReadOnlySpan<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.addrange)
409409
* `Void CopyTo<T>(Span<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.copyto?view=net-8.0)
410+
* `Void InsertRange<T>(Int32, ReadOnlySpan<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.insertrange)
410411

411412

412413
#### DateTime

src/Polyfill/Polyfill_List.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ public static void AddRange<T>(this List<T> list, ReadOnlySpan<T> source)
2424
}
2525
}
2626

27+
/// <summary>Inserts the elements of a span into the <see cref="List{T}"/> at the specified index.</summary>
28+
/// <typeparam name="T">The type of elements in the list.</typeparam>
29+
/// <param name="list">The list into which the elements should be inserted.</param>
30+
/// <param name="index">The zero-based index at which the new elements should be inserted.</param>
31+
/// <param name="source">The span whose elements should be added to the <see cref="List{T}"/>.</param>
32+
/// <exception cref="ArgumentNullException">The <paramref name="list"/> is null.</exception>
33+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0 or greater than <paramref name="list"/>'s <see cref="List{T}.Count"/>.</exception>
34+
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.collectionextensions.insertrange")]
35+
public static void InsertRange<T>(this List<T> list, int index, ReadOnlySpan<T> source)
36+
{
37+
for (var i = 0; i < source.Length; i++)
38+
{
39+
var item = source[i];
40+
list.Insert(i + index, item);
41+
}
42+
}
43+
2744
/// <summary>Copies the entire <see cref="List{T}"/> to a span.</summary>
2845
/// <typeparam name="T">The type of elements in the list.</typeparam>
2946
/// <param name="list">The list from which the elements are copied.</param>

src/Tests/PolyfillTests_List.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ public void ListAddRangeReadOnlySpan()
99
Assert.AreEqual('b', list[1]);
1010
}
1111

12+
[Test]
13+
public void ListInsertRangeReadOnlySpan()
14+
{
15+
var list = new List<char>
16+
{
17+
'a'
18+
};
19+
list.InsertRange(1, "bc".AsSpan());
20+
Assert.AreEqual('b', list[1]);
21+
Assert.AreEqual('c', list[2]);
22+
}
23+
1224
[Test]
1325
public void ListCopyToSpan()
1426
{

0 commit comments

Comments
 (0)