11using System . Runtime . CompilerServices ;
2+ using System . Text ;
23
34namespace LinkDotNet . StringBuilder ;
45
@@ -12,6 +13,17 @@ public ref partial struct ValueStringBuilder
1213 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
1314 public readonly void Replace ( char oldValue , char newValue ) => Replace ( oldValue , newValue , 0 , Length ) ;
1415
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+
1527 /// <summary>
1628 /// Replaces all instances of one character with another in this builder.
1729 /// </summary>
@@ -42,60 +54,37 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
4254 }
4355
4456 /// <summary>
45- /// Replaces all instances of one string with another in this builder.
57+ /// Replaces all instances of one rune with another in this builder.
4658 /// </summary>
47- /// <param name="oldValue">The string to replace.</param>
48- /// <param name="newValue">The string to replace <paramref name="oldValue"/> with.</param>
49- /// <remarks>
50- /// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/>
51- /// are removed from this builder.
52- /// </remarks>
59+ /// <param name="oldValue">The rune to replace.</param>
60+ /// <param name="newValue">The rune to replace <paramref name="oldValue"/> with.</param>
61+ /// <param name="startIndex">The index to start in this builder.</param>
62+ /// <param name="count">The number of characters to read in this builder.</param>
5363 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
54- public void Replace ( ReadOnlySpan < char > oldValue , ReadOnlySpan < char > newValue )
55- => Replace ( oldValue , newValue , 0 , Length ) ;
64+ public void Replace ( Rune oldValue , Rune newValue , int startIndex , int count )
65+ {
66+ Span < char > oldValueChars = stackalloc char [ 2 ] ;
67+ int oldValueCharsWritten = oldValue . EncodeToUtf16 ( oldValueChars ) ;
68+ ReadOnlySpan < char > oldValueCharsReadOnly = oldValueChars [ ..oldValueCharsWritten ] ;
5669
57- /// <summary>
58- /// Replaces all instances of one string with another in this builder.
59- /// </summary>
60- /// <param name="oldValue">The string to replace.</param>
61- /// <param name="newValue">Object to replace <paramref name="oldValue"/> with.</param>
62- /// <remarks>
63- /// If <paramref name="newValue"/> is from type <see cref="ISpanFormattable"/> an optimized version is taken.
64- /// Otherwise the ToString method is called.
65- /// </remarks>
66- /// /// <typeparam name="T">Any type.</typeparam>
67- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
68- public void ReplaceGeneric < T > ( ReadOnlySpan < char > oldValue , T newValue )
69- => ReplaceGeneric ( oldValue , newValue , 0 , Length ) ;
70+ Span < char > newValueChars = stackalloc char [ 2 ] ;
71+ int newValueCharsWritten = newValue . EncodeToUtf16 ( newValueChars ) ;
72+ ReadOnlySpan < char > newValueCharsReadOnly = newValueChars [ ..newValueCharsWritten ] ;
73+
74+ Replace ( oldValueCharsReadOnly , newValueCharsReadOnly , startIndex , count ) ;
75+ }
7076
7177 /// <summary>
7278 /// Replaces all instances of one string with another in this builder.
7379 /// </summary>
7480 /// <param name="oldValue">The string to replace.</param>
75- /// <param name="newValue">Object to replace <paramref name="oldValue"/> with.</param>
76- /// <param name="startIndex">The index to start in this builder.</param>
77- /// <param name="count">The number of characters to read in this builder.</param>
81+ /// <param name="newValue">The string to replace <paramref name="oldValue"/> with.</param>
7882 /// <remarks>
79- /// If <paramref name="newValue"/> is from type <see cref="ISpanFormattable"/> an optimized version is taken.
80- /// Otherwise the ToString method is called.
83+ /// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/> are removed.
8184 /// </remarks>
82- /// /// <typeparam name="T">Any type.</typeparam>
8385 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
84- public void ReplaceGeneric < T > ( ReadOnlySpan < char > oldValue , T newValue , int startIndex , int count )
85- {
86- if ( newValue is ISpanFormattable spanFormattable )
87- {
88- Span < char > tempBuffer = stackalloc char [ 24 ] ;
89- if ( spanFormattable . TryFormat ( tempBuffer , out var written , default , null ) )
90- {
91- Replace ( oldValue , tempBuffer [ ..written ] , startIndex , count ) ;
92- }
93- }
94- else
95- {
96- Replace ( oldValue , ( ReadOnlySpan < char > ) newValue ? . ToString ( ) , startIndex , count ) ;
97- }
98- }
86+ public void Replace ( scoped ReadOnlySpan < char > oldValue , scoped ReadOnlySpan < char > newValue )
87+ => Replace ( oldValue , newValue , 0 , Length ) ;
9988
10089 /// <summary>
10190 /// Replaces all instances of one string with another in this builder.
@@ -105,8 +94,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
10594 /// <param name="startIndex">The index to start in this builder.</param>
10695 /// <param name="count">The number of characters to read in this builder.</param>
10796 /// <remarks>
108- /// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/>
109- /// are removed from this builder.
97+ /// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/> are removed.
11098 /// </remarks>
11199 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
112100 public void Replace ( scoped ReadOnlySpan < char > oldValue , scoped ReadOnlySpan < char > newValue , int startIndex , int count )
@@ -160,4 +148,47 @@ public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char
160148 }
161149 }
162150 }
151+
152+ /// <summary>
153+ /// Replaces all instances of one string with another in this builder.
154+ /// </summary>
155+ /// <param name="oldValue">The string to replace.</param>
156+ /// <param name="newValue">Object to replace <paramref name="oldValue"/> with.</param>
157+ /// <remarks>
158+ /// If <paramref name="newValue"/> is from type <see cref="ISpanFormattable"/> an optimized version is taken.
159+ /// Otherwise the ToString method is called.
160+ /// </remarks>
161+ /// /// <typeparam name="T">Any type.</typeparam>
162+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
163+ public void ReplaceGeneric < T > ( ReadOnlySpan < char > oldValue , T newValue )
164+ => ReplaceGeneric ( oldValue , newValue , 0 , Length ) ;
165+
166+ /// <summary>
167+ /// Replaces all instances of one string with another in this builder.
168+ /// </summary>
169+ /// <param name="oldValue">The string to replace.</param>
170+ /// <param name="newValue">Object to replace <paramref name="oldValue"/> with.</param>
171+ /// <param name="startIndex">The index to start in this builder.</param>
172+ /// <param name="count">The number of characters to read in this builder.</param>
173+ /// <remarks>
174+ /// If <paramref name="newValue"/> is from type <see cref="ISpanFormattable"/> an optimized version is taken.
175+ /// Otherwise the ToString method is called.
176+ /// </remarks>
177+ /// /// <typeparam name="T">Any type.</typeparam>
178+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
179+ public void ReplaceGeneric < T > ( ReadOnlySpan < char > oldValue , T newValue , int startIndex , int count )
180+ {
181+ if ( newValue is ISpanFormattable spanFormattable )
182+ {
183+ Span < char > tempBuffer = stackalloc char [ 24 ] ;
184+ if ( spanFormattable . TryFormat ( tempBuffer , out var written , default , null ) )
185+ {
186+ Replace ( oldValue , tempBuffer [ ..written ] , startIndex , count ) ;
187+ }
188+ }
189+ else
190+ {
191+ Replace ( oldValue , ( ReadOnlySpan < char > ) newValue ? . ToString ( ) , startIndex , count ) ;
192+ }
193+ }
163194}
0 commit comments