Rewrite TextFormatter.RemoveHotKeySpecifier

Uses stackalloc char buffer with fallback to rented array.
This commit is contained in:
Tonttu
2025-03-16 00:19:06 +02:00
committed by Tig
parent 5ab51fc08b
commit 2e4e73a436
2 changed files with 130 additions and 13 deletions

View File

@@ -2444,24 +2444,44 @@ public class TextFormatter
return text;
}
// Scan
var start = string.Empty;
var i = 0;
foreach (Rune c in text.EnumerateRunes ())
const int maxStackallocCharBufferSize = 512; // ~1 kiB
char[]? rentedBufferArray = null;
try
{
if (c == hotKeySpecifier && i == hotPos)
{
i++;
Span<char> buffer = text.Length <= maxStackallocCharBufferSize
? stackalloc char[text.Length]
: (rentedBufferArray = ArrayPool<char>.Shared.Rent(text.Length));
continue;
int i = 0;
var remainingBuffer = buffer;
foreach (Rune c in text.EnumerateRunes ())
{
if (c == hotKeySpecifier && i == hotPos)
{
i++;
continue;
}
int charsWritten = c.EncodeToUtf16 (remainingBuffer);
remainingBuffer = remainingBuffer [charsWritten..];
i++;
}
start += c;
i++;
}
ReadOnlySpan<char> newText = buffer [..^remainingBuffer.Length];
// If the resulting string would be the same as original then just return the original.
if (newText.Equals(text, StringComparison.Ordinal))
{
return text;
}
return start;
return new string (newText);
}
finally
{
if (rentedBufferArray != null)
{
ArrayPool<char>.Shared.Return (rentedBufferArray);
}
}
}
#endregion // Static Members