@@ -13,17 +13,6 @@ public ref partial struct ValueStringBuilder
1313 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1414 public readonly void Replace ( char oldValue , char newValue ) => Replace ( oldValue , newValue , 0 , Length ) ;
1515
16- /// <summary>
17- /// Replaces all instances of one rune with another in this builder.
18- /// </summary>
19- /// <param name="oldValue">The rune to replace.</param>
20- /// <param name="newValue">The rune to replace <paramref name="oldValue"/> with.</param>
21- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
22- public void Replace ( Rune oldValue , Rune newValue )
23- {
24- Replace ( oldValue , newValue , 0 , Length ) ;
25- }
26-
2716 /// <summary>
2817 /// Replaces all instances of one character with another in this builder.
2918 /// </summary>
@@ -34,15 +23,8 @@ public void Replace(Rune oldValue, Rune newValue)
3423 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
3524 public readonly void Replace ( char oldValue , char newValue , int startIndex , int count )
3625 {
37- if ( startIndex < 0 )
38- {
39- throw new ArgumentOutOfRangeException ( nameof ( startIndex ) , "Start index can't be smaller than 0." ) ;
40- }
41-
42- if ( count > bufferPosition )
43- {
44- throw new ArgumentOutOfRangeException ( nameof ( count ) , $ "Count: { count } is bigger than the current size { bufferPosition } .") ;
45- }
26+ ArgumentOutOfRangeException . ThrowIfLessThan ( startIndex , 0 ) ;
27+ ArgumentOutOfRangeException . ThrowIfGreaterThan ( startIndex + count , Length ) ;
4628
4729 for ( var i = startIndex ; i < startIndex + count ; i ++ )
4830 {
@@ -53,6 +35,14 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
5335 }
5436 }
5537
38+ /// <summary>
39+ /// Replaces all instances of one rune with another in this builder.
40+ /// </summary>
41+ /// <param name="oldValue">The rune to replace.</param>
42+ /// <param name="newValue">The rune to replace <paramref name="oldValue"/> with.</param>
43+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
44+ public void Replace ( Rune oldValue , Rune newValue ) => Replace ( oldValue , newValue , 0 , Length ) ;
45+
5646 /// <summary>
5747 /// Replaces all instances of one rune with another in this builder.
5848 /// </summary>
@@ -64,14 +54,14 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
6454 public void Replace ( Rune oldValue , Rune newValue , int startIndex , int count )
6555 {
6656 Span < char > oldValueChars = stackalloc char [ 2 ] ;
67- int oldValueCharsWritten = oldValue . EncodeToUtf16 ( oldValueChars ) ;
68- ReadOnlySpan < char > oldValueCharsReadOnly = oldValueChars [ ..oldValueCharsWritten ] ;
57+ var oldValueCharsWritten = oldValue . EncodeToUtf16 ( oldValueChars ) ;
58+ ReadOnlySpan < char > oldValueCharsSlice = oldValueChars [ ..oldValueCharsWritten ] ;
6959
7060 Span < char > newValueChars = stackalloc char [ 2 ] ;
71- int newValueCharsWritten = newValue . EncodeToUtf16 ( newValueChars ) ;
72- ReadOnlySpan < char > newValueCharsReadOnly = newValueChars [ ..newValueCharsWritten ] ;
61+ var newValueCharsWritten = newValue . EncodeToUtf16 ( newValueChars ) ;
62+ ReadOnlySpan < char > newValueCharsSlice = newValueChars [ ..newValueCharsWritten ] ;
7363
74- Replace ( oldValueCharsReadOnly , newValueCharsReadOnly , startIndex , count ) ;
64+ Replace ( oldValueCharsSlice , newValueCharsSlice , startIndex , count ) ;
7565 }
7666
7767 /// <summary>
@@ -99,6 +89,9 @@ public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char
9989 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
10090 public void Replace ( scoped ReadOnlySpan < char > oldValue , scoped ReadOnlySpan < char > newValue , int startIndex , int count )
10191 {
92+ ArgumentOutOfRangeException . ThrowIfLessThan ( startIndex , 0 ) ;
93+ ArgumentOutOfRangeException . ThrowIfGreaterThan ( startIndex + count , Length ) ;
94+
10295 var length = startIndex + count ;
10396 var slice = buffer [ startIndex ..length ] ;
10497
@@ -188,7 +181,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
188181 }
189182 else
190183 {
191- Replace ( oldValue , ( ReadOnlySpan < char > ) newValue ? . ToString ( ) , startIndex , count ) ;
184+ Replace ( oldValue , ( newValue ? . ToString ( ) ?? string . Empty ) . AsSpan ( ) , startIndex , count ) ;
192185 }
193186 }
194187}
0 commit comments