diff --git a/CHANGELOG.md b/CHANGELOG.md index 10eb412..4e82b2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T ## [Unreleased] +### Changed + +- Optimized `Replace(char, char)` (by @Joy-less in #241) +- Optimized `Replace(ReadOnlySpan, ReadOnlySpan)` when both spans are length 1 (by @Joy-less in #241) + ## [2.4.0] - 2025-02-21 ### Added diff --git a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs index 967515f..ebf41f6 100644 --- a/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs +++ b/src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs @@ -26,13 +26,7 @@ public readonly void Replace(char oldValue, char newValue, int startIndex, int c ArgumentOutOfRangeException.ThrowIfLessThan(startIndex, 0); ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Length); - for (var i = startIndex; i < startIndex + count; i++) - { - if (buffer[i] == oldValue) - { - buffer[i] = newValue; - } - } + buffer.Slice(startIndex, count).Replace(oldValue, newValue); } /// @@ -97,6 +91,12 @@ public void Replace(scoped ReadOnlySpan oldValue, scoped ReadOnlySpan