Skip to content

Commit

Permalink
Add array variance checks
Browse files Browse the repository at this point in the history
  • Loading branch information
xtqqczze committed Jun 18, 2021
1 parent a851f05 commit f261e92
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,18 @@ public static void Fill<T>(T[] array, T value)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}

var span = new Span<T>(ref MemoryMarshal.GetArrayDataReference(array), array.Length);
span.Fill(value);
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
{
for (int i = 0; i < array.Length; i++)
{
array[i] = value;
}
}
else
{
var span = new Span<T>(ref MemoryMarshal.GetArrayDataReference(array), array.Length);
span.Fill(value);
}
}

public static void Fill<T>(T[] array, T value, int startIndex, int count)
Expand All @@ -806,9 +816,19 @@ public static void Fill<T>(T[] array, T value, int startIndex, int count)
ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
}

ref T first = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)startIndex);
var span = new Span<T>(ref first, count);
span.Fill(value);
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
{
for (int i = startIndex; i < startIndex + count; i++)
{
array[i] = value;
}
}
else
{
ref T first = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), (nint)(uint)startIndex);
var span = new Span<T>(ref first, count);
span.Fill(value);
}
}

public static T? Find<T>(T[] array, Predicate<T> match)
Expand Down

0 comments on commit f261e92

Please sign in to comment.