diff options
author | Tarek Mahmoud Sayed <tarekms@microsoft.com> | 2019-02-06 15:15:46 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-06 15:15:46 -0800 |
commit | fe1185343cd02559824fcbfaf69967166978e8e5 (patch) | |
tree | 84c26b6a1c1a05dcb211665a9ed8ddf339179aa6 /src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs | |
parent | 17f4677cd45e2feed716d2b63046fa24a66bf662 (diff) | |
download | coreclr-fe1185343cd02559824fcbfaf69967166978e8e5.tar.gz coreclr-fe1185343cd02559824fcbfaf69967166978e8e5.tar.bz2 coreclr-fe1185343cd02559824fcbfaf69967166978e8e5.zip |
Index and Range updates (#22331)
* Index and Range updates
* Address @mikedn feedback
* Address Feedback
* more feedback
* Use Deconstruct in Range.GetOffsetAndLength
* Rename GetArrayRange to GetSubArray
* Temporary disable the old Corefx Range tests
* Return back the TimeSpan test disabling
* Fix Range jit test
* Exclude the jit test
* revert the changes in the jit Range test
* Address Suggested Feedback
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs b/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs index 11980fbcac..1e1fd90e44 100644 --- a/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs +++ b/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs @@ -401,6 +401,54 @@ namespace System } /// <summary> + /// Creates a new span over the portion of the target array. + /// </summary> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span<T> AsSpan<T>(this T[] array, Index startIndex) + { + if (array == null) + { + if (!startIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + + return default; + } + + if (default(T) == null && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + + int actualIndex = startIndex.GetOffset(array.Length); + if ((uint)actualIndex > (uint)array.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new Span<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), actualIndex), array.Length - actualIndex); + } + + /// <summary> + /// Creates a new span over the portion of the target array. + /// </summary> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span<T> AsSpan<T>(this T[] array, Range range) + { + if (array == null) + { + Index startIndex = range.Start; + Index endIndex = range.End; + + if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + + return default; + } + + if (default(T) == null && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + + (int start, int length) = range.GetOffsetAndLength(array.Length); + return new Span<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start), length); + } + + /// <summary> /// Creates a new readonly span over the portion of the target string. /// </summary> /// <param name="text">The target string.</param> @@ -506,6 +554,26 @@ namespace System /// <summary>Creates a new <see cref="ReadOnlyMemory{T}"/> over the portion of the target string.</summary> /// <param name="text">The target string.</param> + /// <param name="startIndex">The index at which to begin this slice.</param> + public static ReadOnlyMemory<char> AsMemory(this string text, Index startIndex) + { + if (text == null) + { + if (!startIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + return default; + } + + int actualIndex = startIndex.GetOffset(text.Length); + if ((uint)actualIndex > (uint)text.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlyMemory<char>(text, actualIndex, text.Length - actualIndex); + } + + /// <summary>Creates a new <see cref="ReadOnlyMemory{T}"/> over the portion of the target string.</summary> + /// <param name="text">The target string.</param> /// <param name="start">The index at which to begin this slice.</param> /// <param name="length">The desired length for the slice (exclusive).</param> /// <remarks>Returns default when <paramref name="text"/> is null.</remarks> @@ -532,5 +600,25 @@ namespace System return new ReadOnlyMemory<char>(text, start, length); } + + /// <summary>Creates a new <see cref="ReadOnlyMemory{T}"/> over the portion of the target string.</summary> + /// <param name="text">The target string.</param> + /// <param name="range">The range used to indicate the start and length of the sliced string.</param> + public static ReadOnlyMemory<char> AsMemory(this string text, Range range) + { + if (text == null) + { + Index startIndex = range.Start; + Index endIndex = range.End; + + if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + return default; + } + + (int start, int length) = range.GetOffsetAndLength(text.Length); + return new ReadOnlyMemory<char>(text, start, length); + } } } |