/// <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;
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);
}
}
}
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;
}
}
{
throw new ArgumentNullException(nameof(type), SR.ArgumentNull_Type);
}
-
+
if (!type.IsRuntimeImplemented())
{
throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type.ToString()));