|
| 1 | +// <copyright file="ValueStringBuilder.Replace.cs" company="Endjin Limited"> |
| 2 | +// Copyright (c) Endjin Limited. All rights reserved. |
| 3 | +// </copyright> |
| 4 | +// <license> |
| 5 | +// Derived from code Licensed to the .NET Foundation under one or more agreements. |
| 6 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 7 | +// See https://github.com/dotnet/runtime/blob/c1049390d5b33483203f058b0e1457d2a1f62bf4/src/libraries/Common/src/System/Text/ValueStringBuilder.cs |
| 8 | +// </license> |
| 9 | + |
| 10 | +#pragma warning disable // Currently this is a straight copy of the original code, so we disable warnings to avoid diagnostic problems. |
| 11 | + |
| 12 | +#if !NET8_0_OR_GREATER |
| 13 | +using System.Runtime.CompilerServices; |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace Corvus.HighPerformance; |
| 17 | + |
| 18 | +public ref partial struct ValueStringBuilder |
| 19 | +{ |
| 20 | +#if !NET8_0_OR_GREATER |
| 21 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 22 | + public void Replace( |
| 23 | + string oldValue, |
| 24 | + string newValue, |
| 25 | + int startIndex, |
| 26 | + int count) |
| 27 | + => Replace(oldValue.AsSpan(), newValue.AsSpan(), startIndex, count); |
| 28 | +#endif |
| 29 | + |
| 30 | + public void Replace( |
| 31 | + ReadOnlySpan<char> oldValue, |
| 32 | + ReadOnlySpan<char> newValue, |
| 33 | + int startIndex, |
| 34 | + int count) |
| 35 | + { |
| 36 | + if (startIndex < 0 || (startIndex + count) > _pos) |
| 37 | + { |
| 38 | + throw new ArgumentOutOfRangeException(nameof(startIndex)); |
| 39 | + } |
| 40 | + |
| 41 | + if (count == 0) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + Span<char> rangeBuffer = _chars.Slice(startIndex, count); |
| 47 | + |
| 48 | + int diff = newValue.Length - oldValue.Length; |
| 49 | + if (diff == 0) |
| 50 | + { |
| 51 | + int matchIndex = rangeBuffer.IndexOf(oldValue); |
| 52 | + if (matchIndex == -1) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + Span<char> remainingBuffer = rangeBuffer; |
| 58 | + do |
| 59 | + { |
| 60 | + remainingBuffer = remainingBuffer[matchIndex..]; |
| 61 | + newValue.CopyTo(remainingBuffer); |
| 62 | + remainingBuffer = remainingBuffer[oldValue.Length..]; |
| 63 | + |
| 64 | + matchIndex = remainingBuffer.IndexOf(oldValue); |
| 65 | + } while (matchIndex != -1); |
| 66 | + |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (diff < 0) |
| 71 | + { |
| 72 | + int matchIndex = rangeBuffer.IndexOf(oldValue); |
| 73 | + if (matchIndex == -1) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // We will never need to grow the buffer, but we might need to shift characters |
| 79 | + // down. |
| 80 | + Span<char> remainingTargetBuffer = _chars[(startIndex + matchIndex)..this._pos]; |
| 81 | + Span<char> remainingSourceBuffer = remainingTargetBuffer; |
| 82 | + int endOfSearchRangeRelativeToRemainingSourceBuffer = count - matchIndex; |
| 83 | + do |
| 84 | + { |
| 85 | + this._pos += diff; |
| 86 | + |
| 87 | + newValue.CopyTo(remainingTargetBuffer); |
| 88 | + |
| 89 | + remainingSourceBuffer = remainingSourceBuffer[oldValue.Length..]; |
| 90 | + endOfSearchRangeRelativeToRemainingSourceBuffer -= oldValue.Length; |
| 91 | + remainingTargetBuffer = remainingTargetBuffer[newValue.Length..]; |
| 92 | + |
| 93 | + matchIndex = remainingSourceBuffer[..endOfSearchRangeRelativeToRemainingSourceBuffer] |
| 94 | + .IndexOf(oldValue); |
| 95 | + |
| 96 | + int lengthOfChunkToRelocate = matchIndex == -1 |
| 97 | + ? remainingSourceBuffer.Length |
| 98 | + : matchIndex; |
| 99 | + remainingSourceBuffer[..lengthOfChunkToRelocate].CopyTo(remainingTargetBuffer); |
| 100 | + |
| 101 | + remainingSourceBuffer = remainingSourceBuffer[lengthOfChunkToRelocate..]; |
| 102 | + endOfSearchRangeRelativeToRemainingSourceBuffer -= lengthOfChunkToRelocate; |
| 103 | + remainingTargetBuffer = remainingTargetBuffer[lengthOfChunkToRelocate..]; |
| 104 | + } while (matchIndex != -1); |
| 105 | + |
| 106 | + return; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + int matchIndex = rangeBuffer.IndexOf(oldValue); |
| 111 | + if (matchIndex == -1) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + Span<int> matchIndexes = stackalloc int[(rangeBuffer.Length + oldValue.Length - 1) / oldValue.Length]; |
| 117 | + |
| 118 | + int matchCount = 0; |
| 119 | + int currentRelocationDistance = 0; |
| 120 | + while (matchIndex != -1) |
| 121 | + { |
| 122 | + matchIndexes[matchCount++] = matchIndex; |
| 123 | + currentRelocationDistance += diff; |
| 124 | + |
| 125 | + int nextIndex = rangeBuffer[(matchIndex + oldValue.Length)..].IndexOf(oldValue); |
| 126 | + matchIndex = nextIndex == -1 ? -1 : matchIndex + nextIndex + oldValue.Length; |
| 127 | + } |
| 128 | + |
| 129 | + int relocationRangeEndIndex = this._pos; |
| 130 | + |
| 131 | + int growBy = (this._pos + currentRelocationDistance) - _chars.Length; |
| 132 | + if (growBy > 0) |
| 133 | + { |
| 134 | + Grow(growBy); |
| 135 | + } |
| 136 | + this._pos += currentRelocationDistance; |
| 137 | + |
| 138 | + |
| 139 | + // We work from the back of the string when growing to avoid having to |
| 140 | + // shift anything more than once. |
| 141 | + do |
| 142 | + { |
| 143 | + matchIndex = matchIndexes[matchCount - 1]; |
| 144 | + |
| 145 | + int relocationTargetStart = startIndex + matchIndex + oldValue.Length + currentRelocationDistance; |
| 146 | + int relocationSourceStart = startIndex + matchIndex + oldValue.Length; |
| 147 | + int endOfSearchRangeRelativeToRemainingSourceBuffer = count - matchIndex; |
| 148 | + |
| 149 | + Span<char> relocationTargetBuffer = _chars[relocationTargetStart..]; |
| 150 | + Span<char> sourceBuffer = _chars[relocationSourceStart..relocationRangeEndIndex]; |
| 151 | + |
| 152 | + sourceBuffer.CopyTo(relocationTargetBuffer); |
| 153 | + |
| 154 | + currentRelocationDistance -= diff; |
| 155 | + Span<char> replaceTargetBuffer = this._chars.Slice(startIndex + matchIndex + currentRelocationDistance); |
| 156 | + newValue.CopyTo(replaceTargetBuffer); |
| 157 | + |
| 158 | + relocationRangeEndIndex = matchIndex + startIndex; |
| 159 | + matchIndex = rangeBuffer[..matchIndex].LastIndexOf(oldValue); |
| 160 | + |
| 161 | + matchCount -= 1; |
| 162 | + } while (matchCount > 0); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments