Skip to content

Commit 77fc713

Browse files
authored
Merge pull request #222 from Joy-less/more-runes
Add more rune overloads
2 parents 1480c40 + 07ba7ef commit 77fc713

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Added `Replace(Rune, Rune)` overload
12+
- Added `Replace(Rune, Rune, int, int)` overload
13+
914
## [2.0.0] - 2025-01-12
1015

1116
This is the `v2` release of the **ValueStringBuilder**. There aren't any noticeable breaking changes. Only old framework versions were removed to make further development easier. The API is the same (with new additions) as in `v1`.

src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs

+2-2
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

+34-1
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>
@@ -50,7 +83,7 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c
5083
/// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/> are removed.
5184
/// </remarks>
5285
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53-
public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue)
86+
public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char> newValue)
5487
=> Replace(oldValue, newValue, 0, Length);
5588

5689
/// <summary>

0 commit comments

Comments
 (0)