From 335d4a3d08c2669a1f23bd21c18a154b5b23cba3 Mon Sep 17 00:00:00 2001 From: Andrew El Date: Thu, 6 May 2021 21:48:52 -0400 Subject: [PATCH] add ReplaceAt method to Utf16ValueStringBuilder --- .../Scripts/ZString/Utf16ValueStringBuilder.cs | 16 ++++++++++++++++ src/ZString/Utf16ValueStringBuilder.cs | 16 ++++++++++++++++ tests/ZString.Tests/ReplaceTest.cs | 13 +++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs b/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs index b1d4419..63e634f 100644 --- a/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs +++ b/src/ZString.Unity/Assets/Scripts/ZString/Utf16ValueStringBuilder.cs @@ -469,6 +469,22 @@ public void Replace(ReadOnlySpan oldValue, ReadOnlySpan newValue, in buffer = newBuffer; index = newBufferIndex; } + + /// + /// Replaces the contents of a single position within the builder. + /// + /// The character to use at the position. + /// The index to replace. + public void ReplaceAt(char newChar, int replaceIndex) + { + int currentLength = Length; + if ((uint)replaceIndex > (uint)currentLength) + { + ExceptionUtil.ThrowArgumentOutOfRangeException(nameof(replaceIndex)); + } + + buffer[replaceIndex] = newChar; + } /// /// Removes a range of characters from this builder. diff --git a/src/ZString/Utf16ValueStringBuilder.cs b/src/ZString/Utf16ValueStringBuilder.cs index b1d4419..63e634f 100644 --- a/src/ZString/Utf16ValueStringBuilder.cs +++ b/src/ZString/Utf16ValueStringBuilder.cs @@ -469,6 +469,22 @@ public void Replace(ReadOnlySpan oldValue, ReadOnlySpan newValue, in buffer = newBuffer; index = newBufferIndex; } + + /// + /// Replaces the contents of a single position within the builder. + /// + /// The character to use at the position. + /// The index to replace. + public void ReplaceAt(char newChar, int replaceIndex) + { + int currentLength = Length; + if ((uint)replaceIndex > (uint)currentLength) + { + ExceptionUtil.ThrowArgumentOutOfRangeException(nameof(replaceIndex)); + } + + buffer[replaceIndex] = newChar; + } /// /// Removes a range of characters from this builder. diff --git a/tests/ZString.Tests/ReplaceTest.cs b/tests/ZString.Tests/ReplaceTest.cs index 25f7f34..8b25eb1 100644 --- a/tests/ZString.Tests/ReplaceTest.cs +++ b/tests/ZString.Tests/ReplaceTest.cs @@ -7,6 +7,19 @@ namespace ZStringTests { public class ReplaceTest { + + [Fact] + public void ReplaceAtCharTest() + { + var s = new string(' ', 10); + using (var zsb = ZString.CreateStringBuilder()) + { + zsb.Append(s); + zsb.ReplaceAt('-',2); + zsb.ToString().Should().Be(new StringBuilder(s) { [2] = '-'}.ToString()); + } + } + [Fact] public void ReplaceCharTest() {