Update Range.GetOffsetAndLength (#23855)
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Wed, 10 Apr 2019 19:05:27 +0000 (20:05 +0100)
committerGitHub <noreply@github.com>
Wed, 10 Apr 2019 19:05:27 +0000 (20:05 +0100)
* 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
src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/RuntimeHelpers.cs

index 0098dea..fc5ec52 100644 (file)
@@ -95,15 +95,15 @@ namespace System
         /// <summary>Create a Range object starting from first element to the end.</summary>
         public static Range All => new Range(Index.Start, Index.End);
 
-        /// <summary>Destruct the range object according to a collection length and return the start offset from the beginning and the length of this range.</summary>
-        /// <param name="length">The length of the collection that the range will be used with. length has to be a positive value</param>
+        /// <summary>Calculate the start offset and length of range object using a collection length.</summary>
+        /// <param name="length">The length of the collection that the range will be used with. length has to be a positive value.</param>
         /// <remarks>
         /// 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.
         /// </remarks>
         [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);
         }
     }
 }
index 8dea0ae..6925d97 100644 (file)
@@ -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<T>();
                 }
 
-                var dest = new T[offLen.Length];
+                var dest = new T[length];
                 Buffer.Memmove(
                     ref Unsafe.As<byte, T>(ref dest.GetRawSzArrayData()),
-                    ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), offLen.Offset),
-                    (uint)offLen.Length);
+                    ref Unsafe.Add(ref Unsafe.As<byte, T>(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()));