From 6398853de7ed52ed378312073fe47f3af75cce0a Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Wed, 10 Apr 2019 20:05:27 +0100 Subject: [PATCH] Update Range.GetOffsetAndLength (#23855) * Update Range.GetOffsetAndLength Make it return offset and length tuple instead of the old Type Range.OffsetAndLength * Fix the return Tuple fields names --- src/System.Private.CoreLib/shared/System/Range.cs | 26 ++++------------------ .../Runtime/CompilerServices/RuntimeHelpers.cs | 16 ++++++------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Range.cs b/src/System.Private.CoreLib/shared/System/Range.cs index 0098dea..fc5ec52 100644 --- a/src/System.Private.CoreLib/shared/System/Range.cs +++ b/src/System.Private.CoreLib/shared/System/Range.cs @@ -95,15 +95,15 @@ namespace System /// Create a Range object starting from first element to the end. public static Range All => new Range(Index.Start, Index.End); - /// Destruct the range object according to a collection length and return the start offset from the beginning and the length of this range. - /// The length of the collection that the range will be used with. length has to be a positive value + /// Calculate the start offset and length of range object using a collection length. + /// The length of the collection that the range will be used with. length has to be a positive value. /// /// For performance reason, we don't validate the input length parameter against negative values. /// It is expected Range will be used with collections which always have non negative length/count. /// We validate the range is inside the length scope though. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public OffsetAndLength GetOffsetAndLength(int length) + public (int Offset, int Length) GetOffsetAndLength(int length) { int start; Index startIndex = Start; @@ -124,25 +124,7 @@ namespace System ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); } - return new OffsetAndLength(start, end - start); - } - - public readonly struct OffsetAndLength - { - public int Offset { get; } - public int Length { get; } - - public OffsetAndLength(int offset, int length) - { - Offset = offset; - Length = length; - } - - public void Deconstruct(out int offset, out int length) - { - offset = Offset; - length = Length; - } + return (start, end - start); } } } diff --git a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs index 8dea0ae..6925d97 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs @@ -23,29 +23,29 @@ namespace System.Runtime.CompilerServices ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); } - Range.OffsetAndLength offLen = range.GetOffsetAndLength(array.Length); + (int offset, int length) = range.GetOffsetAndLength(array.Length); if (default(T) != null || typeof(T[]) == array.GetType()) { // We know the type of the array to be exactly T[]. - if (offLen.Length == 0) + if (length == 0) { return Array.Empty(); } - var dest = new T[offLen.Length]; + var dest = new T[length]; Buffer.Memmove( ref Unsafe.As(ref dest.GetRawSzArrayData()), - ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), offLen.Offset), - (uint)offLen.Length); + ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), offset), + (uint)length); return dest; } else { // The array is actually a U[] where U:T. - T[] dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), offLen.Length); - Array.Copy(array, offLen.Offset, dest, 0, offLen.Length); + T[] dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length); + Array.Copy(array, offset, dest, 0, length); return dest; } } @@ -56,7 +56,7 @@ namespace System.Runtime.CompilerServices { throw new ArgumentNullException(nameof(type), SR.ArgumentNull_Type); } - + if (!type.IsRuntimeImplemented()) { throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type.ToString())); -- 2.7.4