diff --git a/src/Ramstack.Parsing/Utilities/StringBuffer.cs b/src/Ramstack.Parsing/Utilities/StringBuffer.cs index 7e185e9..8b90d3b 100644 --- a/src/Ramstack.Parsing/Utilities/StringBuffer.cs +++ b/src/Ramstack.Parsing/Utilities/StringBuffer.cs @@ -40,8 +40,12 @@ public StringBuffer() => /// Initializes a new instance of the structure. /// /// The initial capacity of the buffer. - public StringBuffer(int capacity) => - _chars = ArrayPool.Shared.Rent(capacity); + public StringBuffer(int capacity) + { + _chars = []; + if (capacity != 0) + _chars = ArrayPool.Shared.Rent(capacity); + } /// /// Appends a single character to the string buffer. @@ -109,6 +113,7 @@ public override string ToString() { var chars = _chars; var count = _count; + _chars = []; _count = 0; @@ -135,14 +140,19 @@ static string ToStringImpl(char[] buffer, int length) [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { - if (_count != 0) - DisposeImpl(_chars); + var chars = _chars; + + _chars = []; + _count = 0; + + if (chars is not null) + DisposeImpl(chars); [MethodImpl(MethodImplOptions.NoInlining)] - static void DisposeImpl(char[] buffer) + static void DisposeImpl(char[] chars) { - if (buffer.Length != 0) - ArrayPool.Shared.Return(buffer); + if (chars.Length != 0) + ArrayPool.Shared.Return(chars); } }