@@ -22,19 +22,51 @@ namespace System
2222 /// </summary>
2323 public static class Span
2424 {
25- /// <summary>Creates a new <see cref="ReadOnlyMemory{T }"/> over the portion of the target string.</summary>
25+ /// <summary>Creates a new <see cref="ReadOnlyMemory{char }"/> over the portion of the target string.</summary>
2626 /// <param name="text">The target string.</param>
2727 /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
2828 public static ReadOnlyMemory < char > AsReadOnlyMemory ( this string text )
2929 {
3030 if ( text == null )
31- {
3231 ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . text ) ;
33- }
3432
3533 return new ReadOnlyMemory < char > ( text , 0 , text . Length ) ;
3634 }
3735
36+ /// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary>
37+ /// <param name="text">The target string.</param>
38+ /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
39+ /// <exception cref="System.ArgumentOutOfRangeException">
40+ /// Thrown when the specified <paramref name="start"/> index is not in range (<0 or >text.Length).
41+ /// </exception>
42+ public static ReadOnlyMemory < char > AsReadOnlyMemory ( this string text , int start )
43+ {
44+ if ( text == null )
45+ ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . text ) ;
46+
47+ if ( ( uint ) start > ( uint ) text . Length )
48+ ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
49+
50+ return new ReadOnlyMemory < char > ( text , start , text . Length - start ) ;
51+ }
52+
53+ /// <summary>Creates a new <see cref="ReadOnlyMemory{char}"/> over the portion of the target string.</summary>
54+ /// <param name="text">The target string.</param>
55+ /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
56+ /// <exception cref="System.ArgumentOutOfRangeException">
57+ /// Thrown when the specified <paramref name="start"/> index or <paramref name="length"/> is not in range.
58+ /// </exception>
59+ public static ReadOnlyMemory < char > AsReadOnlyMemory ( this string text , int start , int length )
60+ {
61+ if ( text == null )
62+ ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . text ) ;
63+
64+ if ( ( uint ) start > ( uint ) text . Length || ( uint ) length > ( uint ) ( text . Length - start ) )
65+ ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
66+
67+ return new ReadOnlyMemory < char > ( text , start , length ) ;
68+ }
69+
3870 /// <summary>Attempts to get the underlying <see cref="string"/> from a <see cref="ReadOnlyMemory{T}"/>.</summary>
3971 /// <param name="readOnlyMemory">The memory that may be wrapping a <see cref="string"/> object.</param>
4072 /// <param name="text">The string.</param>
@@ -166,6 +198,50 @@ public static ReadOnlySpan<char> AsReadOnlySpan(this string text)
166198 return new ReadOnlySpan < char > ( ref text . GetRawStringData ( ) , text . Length ) ;
167199 }
168200
201+ /// <summary>
202+ /// Creates a new readonly span over the portion of the target string.
203+ /// </summary>
204+ /// <param name="text">The target string.</param>
205+ /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null
206+ /// reference (Nothing in Visual Basic).
207+ /// </exception>
208+ /// <exception cref="System.ArgumentOutOfRangeException">
209+ /// Thrown when the specified <paramref name="start"/> index is not in range (<0 or >text.Length).
210+ /// </exception>
211+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
212+ public static ReadOnlySpan < char > AsReadOnlySpan ( this string text , int start )
213+ {
214+ if ( text == null )
215+ ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . text ) ;
216+
217+ if ( ( uint ) start > ( uint ) text . Length )
218+ ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
219+
220+ return new ReadOnlySpan < char > ( ref Unsafe . Add ( ref text . GetRawStringData ( ) , start ) , text . Length - start ) ;
221+ }
222+
223+ /// <summary>
224+ /// Creates a new readonly span over the portion of the target string.
225+ /// </summary>
226+ /// <param name="text">The target string.</param>
227+ /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null
228+ /// reference (Nothing in Visual Basic).
229+ /// </exception>
230+ /// <exception cref="System.ArgumentOutOfRangeException">
231+ /// Thrown when the specified <paramref name="start"/> index or <paramref name="length"/> is not in range.
232+ /// </exception>
233+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
234+ public static ReadOnlySpan < char > AsReadOnlySpan ( this string text , int start , int length )
235+ {
236+ if ( text == null )
237+ ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . text ) ;
238+
239+ if ( ( uint ) start > ( uint ) text . Length || ( uint ) length > ( uint ) ( text . Length - start ) )
240+ ThrowHelper . ThrowArgumentOutOfRangeException ( ) ;
241+
242+ return new ReadOnlySpan < char > ( ref Unsafe . Add ( ref text . GetRawStringData ( ) , start ) , length ) ;
243+ }
244+
169245 internal static unsafe void CopyTo < T > ( ref T destination , ref T source , int elementsCount )
170246 {
171247 if ( Unsafe . AreSame ( ref destination , ref source ) )
0 commit comments