Skip to content

Commit

Permalink
add ReplaceAt method to Utf16ValueStringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
to11mtm committed May 7, 2021
1 parent de77404 commit 335d4a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Expand Up @@ -469,6 +469,22 @@ public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, in
buffer = newBuffer;
index = newBufferIndex;
}

/// <summary>
/// Replaces the contents of a single position within the builder.
/// </summary>
/// <param name="newChar">The character to use at the position.</param>
/// <param name="replaceIndex">The index to replace.</param>
public void ReplaceAt(char newChar, int replaceIndex)
{
int currentLength = Length;
if ((uint)replaceIndex > (uint)currentLength)
{
ExceptionUtil.ThrowArgumentOutOfRangeException(nameof(replaceIndex));
}

buffer[replaceIndex] = newChar;
}

/// <summary>
/// Removes a range of characters from this builder.
Expand Down
16 changes: 16 additions & 0 deletions src/ZString/Utf16ValueStringBuilder.cs
Expand Up @@ -469,6 +469,22 @@ public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, in
buffer = newBuffer;
index = newBufferIndex;
}

/// <summary>
/// Replaces the contents of a single position within the builder.
/// </summary>
/// <param name="newChar">The character to use at the position.</param>
/// <param name="replaceIndex">The index to replace.</param>
public void ReplaceAt(char newChar, int replaceIndex)
{
int currentLength = Length;
if ((uint)replaceIndex > (uint)currentLength)
{
ExceptionUtil.ThrowArgumentOutOfRangeException(nameof(replaceIndex));
}

buffer[replaceIndex] = newChar;
}

/// <summary>
/// Removes a range of characters from this builder.
Expand Down
13 changes: 13 additions & 0 deletions tests/ZString.Tests/ReplaceTest.cs
Expand Up @@ -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()
{
Expand Down

0 comments on commit 335d4a3

Please sign in to comment.