From 87ef534f59768d1f9cec18320b29ff57a9b34bef Mon Sep 17 00:00:00 2001 From: PopSlime Date: Sun, 12 Feb 2023 21:48:39 +0800 Subject: [PATCH] Fix resize logic in StringBuffer. --- Assets/Plugins/StringFormatter/StringFormatter.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Assets/Plugins/StringFormatter/StringFormatter.cs b/Assets/Plugins/StringFormatter/StringFormatter.cs index 358d56c..0f47dc3 100644 --- a/Assets/Plugins/StringFormatter/StringFormatter.cs +++ b/Assets/Plugins/StringFormatter/StringFormatter.cs @@ -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(ref char* currRef, char* end, char* dest, ref int prevArgIndex, ref T args) where T : IArgSet {