diff --git a/src/mscorlib/shared/System/Span.NonGeneric.cs b/src/mscorlib/shared/System/Span.NonGeneric.cs index 775426f6b1db..f2ad245bae89 100644 --- a/src/mscorlib/shared/System/Span.NonGeneric.cs +++ b/src/mscorlib/shared/System/Span.NonGeneric.cs @@ -22,19 +22,51 @@ namespace System /// public static class Span { - /// Creates a new over the portion of the target string. + /// Creates a new over the portion of the target string. /// The target string. /// Thrown when is a null reference (Nothing in Visual Basic). public static ReadOnlyMemory AsReadOnlyMemory(this string text) { if (text == null) - { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - } return new ReadOnlyMemory(text, 0, text.Length); } + /// Creates a new over the portion of the target string. + /// The target string. + /// Thrown when is a null reference (Nothing in Visual Basic). + /// + /// Thrown when the specified index is not in range (<0 or >text.Length). + /// + public static ReadOnlyMemory AsReadOnlyMemory(this string text, int start) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlyMemory(text, start, text.Length - start); + } + + /// Creates a new over the portion of the target string. + /// The target string. + /// Thrown when is a null reference (Nothing in Visual Basic). + /// + /// Thrown when the specified index or is not in range. + /// + public static ReadOnlyMemory AsReadOnlyMemory(this string text, int start, int length) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlyMemory(text, start, length); + } + /// Attempts to get the underlying from a . /// The memory that may be wrapping a object. /// The string. @@ -166,6 +198,50 @@ public static ReadOnlySpan AsReadOnlySpan(this string text) return new ReadOnlySpan(ref text.GetRawStringData(), text.Length); } + /// + /// Creates a new readonly span over the portion of the target string. + /// + /// The target string. + /// Thrown when is a null + /// reference (Nothing in Visual Basic). + /// + /// + /// Thrown when the specified index is not in range (<0 or >text.Length). + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlySpan(this string text, int start) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), start), text.Length - start); + } + + /// + /// Creates a new readonly span over the portion of the target string. + /// + /// The target string. + /// Thrown when is a null + /// reference (Nothing in Visual Basic). + /// + /// + /// Thrown when the specified index or is not in range. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlySpan(this string text, int start, int length) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), start), length); + } + internal static unsafe void CopyTo(ref T destination, ref T source, int elementsCount) { if (Unsafe.AreSame(ref destination, ref source))