Skip to content

Commit

Permalink
Add params ReadOnlySpan<T> overloads (#100898)
Browse files Browse the repository at this point in the history
* Add params span overloads

* Complete src/ref changes

* Add tests for MemoryExtensions, String, and StringBuilder

* Update tests for ImmutableHashSet, ImmutableList, ImmutableQueue, ImmutableSortedSet, and ImmutableStack

* Update tests for System.Diagnostics.Metrics

* Update tests for Console.Write[Line]

* Update tests for Deleate.Combine

* Update tests for IndentedTextWriter.Write[Line]

* Update tests for Path.Combine and Path.Join

* Update tests for StreamWriter and TextWriter

* Update tests for JsonArray and JsonTypeInfoResolver.Combine

* Update tests for System.Threading

* Add project reference to System.Memory where needed

* Fix missing params in ImmutableSortedSet.cs

* Fix error CS9220 in System.Dynamic.Runtime.Tests

* Suppress CS9220 in System.Dynamic.Runtime.Tests

* Address review feedback

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
  • Loading branch information
jozkee and stephentoub authored Apr 15, 2024
1 parent 617f81f commit 64a937c
Show file tree
Hide file tree
Showing 80 changed files with 1,436 additions and 299 deletions.
201 changes: 172 additions & 29 deletions src/libraries/Common/tests/Tests/System/StringTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static partial class ImmutableArray
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(T item1, T item2, T item3, T item4) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(params T[]? items) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(T[] items, int start, int length) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableArray<T> Create<T>(System.Span<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TSource> ToImmutableArray<TSource>(this System.Collections.Generic.IEnumerable<TSource> items) { throw null; }
public static System.Collections.Immutable.ImmutableArray<TSource> ToImmutableArray<TSource>(this System.Collections.Immutable.ImmutableArray<TSource>.Builder builder) { throw null; }
Expand Down Expand Up @@ -244,7 +244,7 @@ public static partial class ImmutableArray
public System.Collections.Immutable.ImmutableArray<T> AddRange<TDerived>(TDerived[] items) where TDerived : T { throw null; }
public System.Collections.Immutable.ImmutableArray<T> AddRange(ImmutableArray<T> items, int length) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> AddRange<TDerived>(ImmutableArray<TDerived> items) where TDerived : T { throw null; }
public System.Collections.Immutable.ImmutableArray<T> AddRange(System.ReadOnlySpan<T> items) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> AddRange(params System.ReadOnlySpan<T> items) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> AddRange(params T[] items) { throw null; }
public System.ReadOnlyMemory<T> AsMemory() { throw null; }
public System.ReadOnlySpan<T> AsSpan() { throw null; }
Expand Down Expand Up @@ -284,7 +284,7 @@ public void CopyTo(System.Span<T> destination) { }
public System.Collections.Immutable.ImmutableArray<T> InsertRange(int index, System.Collections.Generic.IEnumerable<T> items) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> InsertRange(int index, System.Collections.Immutable.ImmutableArray<T> items) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> InsertRange(int index, T[] items) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> InsertRange(int index, System.ReadOnlySpan<T> items) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> InsertRange(int index, params System.ReadOnlySpan<T> items) { throw null; }
public ref readonly T ItemRef(int index) { throw null; }
public int LastIndexOf(T item) { throw null; }
public int LastIndexOf(T item, int startIndex) { throw null; }
Expand Down Expand Up @@ -362,8 +362,8 @@ public void AddRange(T[] items, int length) { }
public void AddRange<TDerived>(System.Collections.Immutable.ImmutableArray<TDerived> items) where TDerived : T { }
public void AddRange<TDerived>(System.Collections.Immutable.ImmutableArray<TDerived>.Builder items) where TDerived : T { }
public void AddRange<TDerived>(TDerived[] items) where TDerived : T { }
public void AddRange(System.ReadOnlySpan<T> items) { }
public void AddRange<TDerived>(System.ReadOnlySpan<TDerived> items) where TDerived : T { }
public void AddRange(params System.ReadOnlySpan<T> items) { }
public void AddRange<TDerived>(params System.ReadOnlySpan<TDerived> items) where TDerived : T { }
public void Clear() { }
public bool Contains(T item) { throw null; }
public void CopyTo(T[] array, int index) { }
Expand Down Expand Up @@ -562,10 +562,10 @@ public static partial class ImmutableHashSet
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, T item) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<TSource> ToImmutableHashSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<TSource> ToImmutableHashSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>? equalityComparer) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<TSource> ToImmutableHashSet<TSource>(this System.Collections.Immutable.ImmutableHashSet<TSource>.Builder builder) { throw null; }
Expand Down Expand Up @@ -691,7 +691,7 @@ public static partial class ImmutableList
public static System.Collections.Immutable.ImmutableList<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(params System.ReadOnlySpan<T> items) { throw null; }
public static int IndexOf<T>(this System.Collections.Immutable.IImmutableList<T> list, T item) { throw null; }
public static int IndexOf<T>(this System.Collections.Immutable.IImmutableList<T> list, T item, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public static int IndexOf<T>(this System.Collections.Immutable.IImmutableList<T> list, T item, int startIndex) { throw null; }
Expand Down Expand Up @@ -887,7 +887,7 @@ public static partial class ImmutableQueue
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.IImmutableQueue<T> Dequeue<T>(this System.Collections.Immutable.IImmutableQueue<T> queue, out T value) { throw null; }
}
[System.Runtime.CompilerServices.CollectionBuilderAttribute(typeof(System.Collections.Immutable.ImmutableQueue), "Create")]
Expand Down Expand Up @@ -1063,10 +1063,10 @@ public static partial class ImmutableSortedSet
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, T item) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<TSource> ToImmutableSortedSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<TSource> ToImmutableSortedSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<TSource> ToImmutableSortedSet<TSource>(this System.Collections.Immutable.ImmutableSortedSet<TSource>.Builder builder) { throw null; }
Expand Down Expand Up @@ -1198,7 +1198,7 @@ public static partial class ImmutableStack
public static System.Collections.Immutable.ImmutableStack<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(params System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.IImmutableStack<T> Pop<T>(this System.Collections.Immutable.IImmutableStack<T> stack, out T value) { throw null; }
}
[System.Runtime.CompilerServices.CollectionBuilderAttribute(typeof(System.Collections.Immutable.ImmutableStack), "Create")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static ImmutableArray<T> Create<T>(T item1, T item2, T item3, T item4)
/// <typeparam name="T">The type of element stored in the array.</typeparam>
/// <param name="items">The elements to store in the array.</param>
/// <returns>An immutable array containing the specified items.</returns>
public static ImmutableArray<T> Create<T>(ReadOnlySpan<T> items)
public static ImmutableArray<T> Create<T>(params ReadOnlySpan<T> items)
{
if (items.IsEmpty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public void AddRange(ImmutableArray<T> items, int length)
/// Adds the specified items to the end of the array.
/// </summary>
/// <param name="items">The items to add at the end of the array.</param>
public void AddRange(ReadOnlySpan<T> items)
public void AddRange(params ReadOnlySpan<T> items)
{
int offset = this.Count;
this.Count += items.Length;
Expand All @@ -443,7 +443,7 @@ public void AddRange(ReadOnlySpan<T> items)
/// </summary>
/// <typeparam name="TDerived">The type that derives from the type of item already in the array.</typeparam>
/// <param name="items">The items to add at the end of the array.</param>
public void AddRange<TDerived>(ReadOnlySpan<TDerived> items) where TDerived : T
public void AddRange<TDerived>(params ReadOnlySpan<TDerived> items) where TDerived : T
{
int offset = this.Count;
this.Count += items.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ public IEnumerable<TResult> OfType<TResult>()
/// </summary>
/// <param name="items">The values to add.</param>
/// <returns>A new list with the elements added.</returns>
public ImmutableArray<T> AddRange(ReadOnlySpan<T> items)
public ImmutableArray<T> AddRange(params ReadOnlySpan<T> items)
{
ImmutableArray<T> self = this;
return self.InsertRange(self.Length, items);
Expand Down Expand Up @@ -949,7 +949,7 @@ public ImmutableArray<T> InsertRange(int index, T[] items)
/// <param name="index">The index at which to insert the value.</param>
/// <param name="items">The elements to insert.</param>
/// <returns>The new immutable collection.</returns>
public ImmutableArray<T> InsertRange(int index, ReadOnlySpan<T> items)
public ImmutableArray<T> InsertRange(int index, params ReadOnlySpan<T> items)
{
ImmutableArray<T> self = this;
self.ThrowNullRefIfNotInitialized();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static ImmutableHashSet<T> Create<T>(params T[] items)
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableHashSet<T> Create<T>(ReadOnlySpan<T> items)
public static ImmutableHashSet<T> Create<T>(params ReadOnlySpan<T> items)
{
return ImmutableHashSet<T>.Empty.Union(items);
}
Expand All @@ -124,7 +124,7 @@ public static ImmutableHashSet<T> Create<T>(IEqualityComparer<T>? equalityCompar
/// <param name="equalityComparer">The equality comparer.</param>
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableHashSet<T> Create<T>(IEqualityComparer<T>? equalityComparer, ReadOnlySpan<T> items)
public static ImmutableHashSet<T> Create<T>(IEqualityComparer<T>? equalityComparer, params ReadOnlySpan<T> items)
{
return ImmutableHashSet<T>.Empty.WithComparer(equalityComparer).Union(items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static ImmutableList<T> Create<T>(params T[] items)
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the list with.</param>
/// <returns>A new immutable list that contains the specified items.</returns>
public static ImmutableList<T> Create<T>(ReadOnlySpan<T> items) => ImmutableList<T>.Empty.AddRange(items);
public static ImmutableList<T> Create<T>(params ReadOnlySpan<T> items) => ImmutableList<T>.Empty.AddRange(items);

/// <summary>
/// Creates a new immutable list builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static ImmutableQueue<T> Create<T>(params T[] items)
/// <typeparam name="T">The type of items in the immutable queue.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the queue with.</param>
/// <returns>A new immutable queue that contains the specified items.</returns>
public static ImmutableQueue<T> Create<T>(ReadOnlySpan<T> items)
public static ImmutableQueue<T> Create<T>(params ReadOnlySpan<T> items)
{
if (items.IsEmpty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static ImmutableSortedSet<T> Create<T>(params T[] items)
/// <typeparam name="T">The type of items in the immutable set.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the set with.</param>
/// <returns>A new immutable set that contains the specified items.</returns>
public static ImmutableSortedSet<T> Create<T>(ReadOnlySpan<T> items)
public static ImmutableSortedSet<T> Create<T>(params ReadOnlySpan<T> items)
{
return ImmutableSortedSet<T>.Empty.Union(items);
}
Expand All @@ -123,7 +123,7 @@ public static ImmutableSortedSet<T> Create<T>(IComparer<T>? comparer, params T[]
/// <param name="comparer">The comparer.</param>
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableSortedSet<T> Create<T>(IComparer<T>? comparer, ReadOnlySpan<T> items)
public static ImmutableSortedSet<T> Create<T>(IComparer<T>? comparer, params ReadOnlySpan<T> items)
{
return ImmutableSortedSet<T>.Empty.WithComparer(comparer).Union(items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static ImmutableStack<T> Create<T>(params T[] items)
/// <typeparam name="T">The type of items in the immutable stack.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the stack with.</param>
/// <returns>A new immutable stack that contains the specified items.</returns>
public static ImmutableStack<T> Create<T>(ReadOnlySpan<T> items)
public static ImmutableStack<T> Create<T>(params ReadOnlySpan<T> items)
{
ImmutableStack<T> stack = ImmutableStack<T>.Empty;
foreach (T item in items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ public void Create()
Assert.Equal(1, set.Count);
Assert.Same(comparer, set.KeyComparer);

set = ImmutableHashSet.Create("a", "b");
set = ImmutableHashSet.Create(new[] { "a", "b" });
Assert.Equal(2, set.Count);
Assert.Same(EqualityComparer<string>.Default, set.KeyComparer);

set = ImmutableHashSet.Create((ReadOnlySpan<string>)new[] { "a", "b" });
Assert.Equal(2, set.Count);
Assert.Same(EqualityComparer<string>.Default, set.KeyComparer);

set = ImmutableHashSet.Create(comparer, "a", "b");
set = ImmutableHashSet.Create(comparer, new[] { "a", "b" });
Assert.Equal(2, set.Count);
Assert.Same(comparer, set.KeyComparer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public void Create()
list = ImmutableList.Create("a");
Assert.Equal(1, list.Count);

list = ImmutableList.Create("a", "b");
list = ImmutableList.Create(new[] { "a", "b" });
Assert.Equal(2, list.Count);

list = ImmutableList.Create((ReadOnlySpan<string>)new[] { "a", "b" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void Create()
Assert.False(queue.IsEmpty);
Assert.Equal(new[] { 1 }, queue);

queue = ImmutableQueue.Create(1, 2);
queue = ImmutableQueue.Create(new int[] { 1, 2 });
Assert.False(queue.IsEmpty);
Assert.Equal(new[] { 1, 2 }, queue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,15 @@ public void Create()
Assert.Equal(1, set.Count);
Assert.Same(comparer, set.KeyComparer);

set = ImmutableSortedSet.Create("a", "b");
set = ImmutableSortedSet.Create(new [] { "a", "b" });
Assert.Equal(2, set.Count);
Assert.Same(Comparer<string>.Default, set.KeyComparer);

set = ImmutableSortedSet.Create((ReadOnlySpan<string>)new[] { "a", "b" });
Assert.Equal(2, set.Count);
Assert.Same(Comparer<string>.Default, set.KeyComparer);

set = ImmutableSortedSet.Create(comparer, "a", "b");
set = ImmutableSortedSet.Create(comparer, new[] { "a", "b" });
Assert.Equal(2, set.Count);
Assert.Same(comparer, set.KeyComparer);

Expand Down
Loading

0 comments on commit 64a937c

Please sign in to comment.