diff options
author | Atsushi Kanamori <AtsushiKan@users.noreply.github.com> | 2018-02-22 11:16:25 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-22 11:16:25 -0800 |
commit | 42239fe363615056a7382f8d4e2b9b02da1d0421 (patch) | |
tree | 3774abf7899bea625e470ef2583b30999c1bd834 | |
parent | 34247df6ccb1bcc54363807f047e67c8d43c03cb (diff) | |
download | coreclr-42239fe363615056a7382f8d4e2b9b02da1d0421.tar.gz coreclr-42239fe363615056a7382f8d4e2b9b02da1d0421.tar.bz2 coreclr-42239fe363615056a7382f8d4e2b9b02da1d0421.zip |
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.
-rw-r--r-- | src/mscorlib/shared/System/Memory.cs | 20 | ||||
-rw-r--r-- | src/mscorlib/shared/System/Span.NonGeneric.cs | 24 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/mscorlib/shared/System/Memory.cs b/src/mscorlib/shared/System/Memory.cs index 8fc5b04ee4..c220274aef 100644 --- a/src/mscorlib/shared/System/Memory.cs +++ b/src/mscorlib/shared/System/Memory.cs @@ -58,6 +58,26 @@ namespace System _length = array.Length; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal Memory(T[] array, int start) + { + if (array == null) + { + if (start != 0) + ThrowHelper.ThrowArgumentOutOfRangeException(); + this = default; + return; // returns default + } + if (default(T) == null && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + if ((uint)start > (uint)array.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + _object = array; + _index = start; + _length = array.Length - start; + } + /// <summary> /// Creates a new memory over the portion of the target array beginning /// at 'start' index and ending at 'end' index (exclusive). diff --git a/src/mscorlib/shared/System/Span.NonGeneric.cs b/src/mscorlib/shared/System/Span.NonGeneric.cs index 4ef1c233e2..6cc801c602 100644 --- a/src/mscorlib/shared/System/Span.NonGeneric.cs +++ b/src/mscorlib/shared/System/Span.NonGeneric.cs @@ -341,6 +341,30 @@ namespace System return OrdinalHelper(span.Slice(span.Length - value.Length), value, value.Length); } + /// <summary> + /// Helper method for MemoryExtensions.AsSpan(T[] array, int start). + /// </summary> + public static Span<T> AsSpan<T>(T[] array, int start) + { + if (array == null) + { + if (start != 0) + ThrowHelper.ThrowArgumentOutOfRangeException(); + return default; + } + if (default(T) == null && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + if ((uint)start > (uint)array.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new Span<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start), array.Length - start); + } + + /// <summary> + /// Helper method for MemoryExtensions.AsMemory(T[] array, int start). + /// </summary> + public static Memory<T> AsMemory<T>(T[] array, int start) => new Memory<T>(array, start); + /// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary> /// <param name="text">The target string.</param> /// <remarks>Returns default when <paramref name="text"/> is null.</remarks> |