Fix resize logic in StringBuffer.

This commit is contained in:
2023-02-12 21:48:39 +08:00
parent 87362b47c5
commit 87ef534f59

View File

@@ -410,8 +410,16 @@ namespace System.Text.Formatting {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void CheckCapacity (int count) {
if (currentCount + count > buffer.Length)
Array.Resize(ref buffer, buffer.Length * 2);
int newCount = currentCount + count;
if (newCount > buffer.Length) {
int newCapacity = buffer.Length * 2;
if (newCapacity < newCount) newCapacity = newCount;
char[] newBuffer = new char[newCapacity];
fixed (void* nptr = newBuffer, ptr = buffer) {
Unsafe.CopyBlock(nptr, ptr, (uint)(currentCount * sizeof(char)));
}
buffer = newBuffer;
}
}
bool AppendSegment<T>(ref char* currRef, char* end, char* dest, ref int prevArgIndex, ref T args) where T : IArgSet {