Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b66086f

Browse files
atsushikanstephentoub
authored andcommitted
Add CoreLib helpers for MemoryExtensions.AsSpan/AsMemory(T[], int) (#16505)
* Add CoreLib helpers for MemoryExtensions.AsSpan/AsMemory(T[], int) (Part of https://github.com/dotnet/corefx/issues/26894) We intentionally don't have (T[], int] constructor overloads for Span and Memory. So as not to incur unnecessary argument checks, we implement this directly in CoreLib and will invoke it from CoreFx for the fast-Span version. * Expose an internal array-int constructor instead. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 92afa6e commit b66086f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Common/src/CoreLib/System/Memory.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ public Memory(T[] array)
5858
_length = array.Length;
5959
}
6060

61+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
62+
internal Memory(T[] array, int start)
63+
{
64+
if (array == null)
65+
{
66+
if (start != 0)
67+
ThrowHelper.ThrowArgumentOutOfRangeException();
68+
this = default;
69+
return; // returns default
70+
}
71+
if (default(T) == null && array.GetType() != typeof(T[]))
72+
ThrowHelper.ThrowArrayTypeMismatchException();
73+
if ((uint)start > (uint)array.Length)
74+
ThrowHelper.ThrowArgumentOutOfRangeException();
75+
76+
_object = array;
77+
_index = start;
78+
_length = array.Length - start;
79+
}
80+
6181
/// <summary>
6282
/// Creates a new memory over the portion of the target array beginning
6383
/// at 'start' index and ending at 'end' index (exclusive).

src/Common/src/CoreLib/System/Span.NonGeneric.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,30 @@ internal static unsafe bool EndsWithOrdinalHelper(ReadOnlySpan<char> span, ReadO
341341
return OrdinalHelper(span.Slice(span.Length - value.Length), value, value.Length);
342342
}
343343

344+
/// <summary>
345+
/// Helper method for MemoryExtensions.AsSpan(T[] array, int start).
346+
/// </summary>
347+
public static Span<T> AsSpan<T>(T[] array, int start)
348+
{
349+
if (array == null)
350+
{
351+
if (start != 0)
352+
ThrowHelper.ThrowArgumentOutOfRangeException();
353+
return default;
354+
}
355+
if (default(T) == null && array.GetType() != typeof(T[]))
356+
ThrowHelper.ThrowArrayTypeMismatchException();
357+
if ((uint)start > (uint)array.Length)
358+
ThrowHelper.ThrowArgumentOutOfRangeException();
359+
360+
return new Span<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start), array.Length - start);
361+
}
362+
363+
/// <summary>
364+
/// Helper method for MemoryExtensions.AsMemory(T[] array, int start).
365+
/// </summary>
366+
public static Memory<T> AsMemory<T>(T[] array, int start) => new Memory<T>(array, start);
367+
344368
/// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary>
345369
/// <param name="text">The target string.</param>
346370
/// <remarks>Returns default when <paramref name="text"/> is null.</remarks>

0 commit comments

Comments
 (0)