Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,25 @@ public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, in
buffer.AsSpan(0, startIndex).CopyTo(newBuffer);
int newBufferIndex = startIndex;

for (int i = startIndex; i < endIndex; i += oldValue.Length)
int readIndex = startIndex;
for (; readIndex < endIndex; readIndex += oldValue.Length)
{
var span = readOnlySpan.Slice(i, endIndex - i);
var span = readOnlySpan.Slice(readIndex, endIndex - readIndex);
var pos = span.IndexOf(oldValue, StringComparison.Ordinal);
if (pos == -1)
{
var remain = readOnlySpan.Slice(i);
remain.CopyTo(newBuffer.AsSpan(newBufferIndex));
newBufferIndex += remain.Length;
break;
}
readOnlySpan.Slice(i, pos).CopyTo(newBuffer.AsSpan(newBufferIndex));
readOnlySpan.Slice(readIndex, pos).CopyTo(newBuffer.AsSpan(newBufferIndex));
newValue.CopyTo(newBuffer.AsSpan(newBufferIndex + pos));
newBufferIndex += pos + newValue.Length;
i += pos;
readIndex += pos;
}

var remain = readOnlySpan.Slice(readIndex);
remain.CopyTo(newBuffer.AsSpan(newBufferIndex));
newBufferIndex += remain.Length;

if (buffer!.Length != ThreadStaticBufferSize)
{
ArrayPool<char>.Shared.Return(buffer);
Expand Down
16 changes: 9 additions & 7 deletions src/ZString/Utf16ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,25 @@ public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, in
buffer.AsSpan(0, startIndex).CopyTo(newBuffer);
int newBufferIndex = startIndex;

for (int i = startIndex; i < endIndex; i += oldValue.Length)
int readIndex = startIndex;
for (; readIndex < endIndex; readIndex += oldValue.Length)
{
var span = readOnlySpan.Slice(i, endIndex - i);
var span = readOnlySpan.Slice(readIndex, endIndex - readIndex);
var pos = span.IndexOf(oldValue, StringComparison.Ordinal);
if (pos == -1)
{
var remain = readOnlySpan.Slice(i);
remain.CopyTo(newBuffer.AsSpan(newBufferIndex));
newBufferIndex += remain.Length;
break;
}
readOnlySpan.Slice(i, pos).CopyTo(newBuffer.AsSpan(newBufferIndex));
readOnlySpan.Slice(readIndex, pos).CopyTo(newBuffer.AsSpan(newBufferIndex));
newValue.CopyTo(newBuffer.AsSpan(newBufferIndex + pos));
newBufferIndex += pos + newValue.Length;
i += pos;
readIndex += pos;
}

var remain = readOnlySpan.Slice(readIndex);
remain.CopyTo(newBuffer.AsSpan(newBufferIndex));
newBufferIndex += remain.Length;

if (buffer!.Length != ThreadStaticBufferSize)
{
ArrayPool<char>.Shared.Return(buffer);
Expand Down
17 changes: 17 additions & 0 deletions tests/ZString.Tests/ReplaceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ public void ReplaceStringTest()
}
}

[Theory]
[InlineData("abcTAIL", "bc", "X", 1, 2)]
[InlineData("abcTAIL", "bc", "", 1, 2)]
[InlineData("abcTAIL", "bc", "longer", 1, 2)]
[InlineData("abcbcTAIL", "bc", "X", 1, 4)]
[InlineData("abczTAIL", "bc", "X", 1, 3)]
[InlineData("abc", "bc", "X", 1, 2)]
public void ReplacePreservesSuffix(string text, string oldValue, string newValue, int startIndex, int count)
{
using (var zsb = ZString.CreateStringBuilder())
{
zsb.Append(text);
zsb.Replace(oldValue, newValue, startIndex, count);
zsb.ToString().Should().Be(new StringBuilder(text).Replace(oldValue, newValue, startIndex, count).ToString());
}
}

[Fact]
public void NotMatchTest()
{
Expand Down