diff --git a/CHANGELOG.md b/CHANGELOG.md index 351c16a..1873cc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ## [Unreleased] +### Added + +- Added `Replace(Rune, Rune)` overload +- Added `Replace(Rune, Rune, int, int)` overload + ## [2.0.0] - 2025-01-12 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`. diff --git a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs index 9b2c82c..a6cc015 100644 --- a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs +++ b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs @@ -21,7 +21,7 @@ public ref partial struct ValueStringBuilder /// Size of the buffer allocated on the stack. /// Any . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Insert(int index, T value, ReadOnlySpan format = default, int bufferSize = 36) + public void Insert(int index, T value, scoped ReadOnlySpan format = default, int bufferSize = 36) where T : ISpanFormattable => InsertSpanFormattable(index, value, format, bufferSize); /// @@ -60,7 +60,7 @@ public void Insert(int index, scoped ReadOnlySpan value) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void InsertSpanFormattable(int index, T value, ReadOnlySpan format, int bufferSize) + private void InsertSpanFormattable(int index, T value, scoped ReadOnlySpan format, int bufferSize) where T : ISpanFormattable { if (index < 0) diff --git a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs index 2b4b1ce..0994f28 100644 --- a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs +++ b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs @@ -1,4 +1,5 @@ using System.Runtime.CompilerServices; +using System.Text; namespace LinkDotNet.StringBuilder; @@ -12,6 +13,17 @@ public ref partial struct ValueStringBuilder [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly void Replace(char oldValue, char newValue) => Replace(oldValue, newValue, 0, Length); + /// + /// Replaces all instances of one rune with another in this builder. + /// + /// The rune to replace. + /// The rune to replace with. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Replace(Rune oldValue, Rune newValue) + { + Replace(oldValue, newValue, 0, Length); + } + /// /// Replaces all instances of one character with another in this builder. /// @@ -41,6 +53,27 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c } } + /// + /// Replaces all instances of one rune with another in this builder. + /// + /// The rune to replace. + /// The rune to replace with. + /// The index to start in this builder. + /// The number of characters to read in this builder. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Replace(Rune oldValue, Rune newValue, int startIndex, int count) + { + Span oldValueChars = stackalloc char[2]; + int oldValueCharsWritten = oldValue.EncodeToUtf16(oldValueChars); + ReadOnlySpan oldValueCharsReadOnly = oldValueChars[..oldValueCharsWritten]; + + Span newValueChars = stackalloc char[2]; + int newValueCharsWritten = newValue.EncodeToUtf16(newValueChars); + ReadOnlySpan newValueCharsReadOnly = newValueChars[..newValueCharsWritten]; + + Replace(oldValueCharsReadOnly, newValueCharsReadOnly, startIndex, count); + } + /// /// Replaces all instances of one string with another in this builder. /// @@ -50,7 +83,7 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c /// If is empty, instances of are removed. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Replace(ReadOnlySpan oldValue, ReadOnlySpan newValue) + public void Replace(scoped ReadOnlySpan oldValue, scoped ReadOnlySpan newValue) => Replace(oldValue, newValue, 0, Length); ///