@@ -13,17 +13,6 @@ public ref partial struct ValueStringBuilder
13
13
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
14
14
public readonly void Replace ( char oldValue , char newValue ) => Replace ( oldValue , newValue , 0 , Length ) ;
15
15
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
-
27
16
/// <summary>
28
17
/// Replaces all instances of one character with another in this builder.
29
18
/// </summary>
@@ -34,15 +23,8 @@ public void Replace(Rune oldValue, Rune newValue)
34
23
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
35
24
public readonly void Replace ( char oldValue , char newValue , int startIndex , int count )
36
25
{
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 ) ;
46
28
47
29
for ( var i = startIndex ; i < startIndex + count ; i ++ )
48
30
{
@@ -53,6 +35,14 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
53
35
}
54
36
}
55
37
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
+
56
46
/// <summary>
57
47
/// Replaces all instances of one rune with another in this builder.
58
48
/// </summary>
@@ -64,14 +54,14 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
64
54
public void Replace ( Rune oldValue , Rune newValue , int startIndex , int count )
65
55
{
66
56
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 ] ;
69
59
70
60
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 ] ;
73
63
74
- Replace ( oldValueCharsReadOnly , newValueCharsReadOnly , startIndex , count ) ;
64
+ Replace ( oldValueCharsSlice , newValueCharsSlice , startIndex , count ) ;
75
65
}
76
66
77
67
/// <summary>
@@ -99,6 +89,9 @@ public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char
99
89
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
100
90
public void Replace ( scoped ReadOnlySpan < char > oldValue , scoped ReadOnlySpan < char > newValue , int startIndex , int count )
101
91
{
92
+ ArgumentOutOfRangeException . ThrowIfLessThan ( startIndex , 0 ) ;
93
+ ArgumentOutOfRangeException . ThrowIfGreaterThan ( startIndex + count , Length ) ;
94
+
102
95
var length = startIndex + count ;
103
96
var slice = buffer [ startIndex ..length ] ;
104
97
@@ -188,7 +181,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
188
181
}
189
182
else
190
183
{
191
- Replace ( oldValue , ( ReadOnlySpan < char > ) newValue ? . ToString ( ) , startIndex , count ) ;
184
+ Replace ( oldValue , ( newValue ? . ToString ( ) ?? string . Empty ) . AsSpan ( ) , startIndex , count ) ;
192
185
}
193
186
}
194
187
}
0 commit comments