Skip to content

Commit b47bcea

Browse files
committed
Add more rune overloads
1 parent a768225 commit b47bcea

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This is the `v2` release of the **ValueStringBuilder**. There aren't any noticea
1515
- Added `Append(Rune)` overload
1616
- Added `AppendJoin(Rune, IEnumerable<string?>)` overload
1717
- Added `AppendJoin<T>(Rune, IEnumerable<T>)` overload
18+
- Added `Replace(Rune, Rune)` overload
19+
- Added `Replace(Rune, Rune, int, int)` overload
1820

1921
### Removed
2022

src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ref partial struct ValueStringBuilder
2121
/// <param name="bufferSize">Size of the buffer allocated on the stack.</param>
2222
/// <typeparam name="T">Any <see cref="ISpanFormattable"/>.</typeparam>
2323
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24-
public void Insert<T>(int index, T value, ReadOnlySpan<char> format = default, int bufferSize = 36)
24+
public void Insert<T>(int index, T value, scoped ReadOnlySpan<char> format = default, int bufferSize = 36)
2525
where T : ISpanFormattable => InsertSpanFormattable(index, value, format, bufferSize);
2626

2727
/// <summary>
@@ -60,7 +60,7 @@ public void Insert(int index, scoped ReadOnlySpan<char> value)
6060
}
6161

6262
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
private void InsertSpanFormattable<T>(int index, T value, ReadOnlySpan<char> format, int bufferSize)
63+
private void InsertSpanFormattable<T>(int index, T value, scoped ReadOnlySpan<char> format, int bufferSize)
6464
where T : ISpanFormattable
6565
{
6666
if (index < 0)

src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using System.Text;
23

34
namespace 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>
@@ -41,6 +53,27 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
4153
}
4254
}
4355

56+
/// <summary>
57+
/// Replaces all instances of one rune with another in this builder.
58+
/// </summary>
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>
63+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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];
69+
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+
}
76+
4477
/// <summary>
4578
/// Replaces all instances of one string with another in this builder.
4679
/// </summary>
@@ -51,7 +84,7 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
5184
/// are removed from this builder.
5285
/// </remarks>
5386
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54-
public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue)
87+
public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char> newValue)
5588
=> Replace(oldValue, newValue, 0, Length);
5689

5790
/// <summary>
@@ -65,7 +98,7 @@ public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue)
6598
/// </remarks>
6699
/// /// <typeparam name="T">Any type.</typeparam>
67100
[MethodImpl(MethodImplOptions.AggressiveInlining)]
68-
public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue)
101+
public void ReplaceGeneric<T>(scoped ReadOnlySpan<char> oldValue, T newValue)
69102
=> ReplaceGeneric(oldValue, newValue, 0, Length);
70103

71104
/// <summary>
@@ -81,7 +114,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue)
81114
/// </remarks>
82115
/// /// <typeparam name="T">Any type.</typeparam>
83116
[MethodImpl(MethodImplOptions.AggressiveInlining)]
84-
public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int startIndex, int count)
117+
public void ReplaceGeneric<T>(scoped ReadOnlySpan<char> oldValue, T newValue, int startIndex, int count)
85118
{
86119
if (newValue is ISpanFormattable spanFormattable)
87120
{

0 commit comments

Comments
 (0)