@@ -22,19 +22,51 @@ namespace System
22
22
/// </summary>
23
23
public static class Span
24
24
{
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>
26
26
/// <param name="text">The target string.</param>
27
27
/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="text"/> is a null reference (Nothing in Visual Basic).</exception>
28
28
public static ReadOnlyMemory < char > AsReadOnlyMemory ( this string text )
29
29
{
30
30
if ( text == null )
31
- {
32
31
ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . text ) ;
33
- }
34
32
35
33
return new ReadOnlyMemory < char > ( text , 0 , text . Length ) ;
36
34
}
37
35
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
+
38
70
/// <summary>Attempts to get the underlying <see cref="string"/> from a <see cref="ReadOnlyMemory{T}"/>.</summary>
39
71
/// <param name="readOnlyMemory">The memory that may be wrapping a <see cref="string"/> object.</param>
40
72
/// <param name="text">The string.</param>
@@ -166,6 +198,50 @@ public static ReadOnlySpan<char> AsReadOnlySpan(this string text)
166
198
return new ReadOnlySpan < char > ( ref text . GetRawStringData ( ) , text . Length ) ;
167
199
}
168
200
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
+
169
245
internal static unsafe void CopyTo < T > ( ref T destination , ref T source , int elementsCount )
170
246
{
171
247
if ( Unsafe . AreSame ( ref destination , ref source ) )
0 commit comments