Skip to content

Commit 27db5aa

Browse files
committed
refactor: Directly write into buffer without stackalloc
1 parent 0e544d5 commit 27db5aa

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ public void AppendLine(scoped ReadOnlySpan<char> str)
7474
private void AppendSpanFormattable<T>(T value, ReadOnlySpan<char> format = default, int bufferSize = 36)
7575
where T : ISpanFormattable
7676
{
77-
Span<char> tempBuffer = stackalloc char[bufferSize];
78-
if (value.TryFormat(tempBuffer, out var written, format, null))
77+
if (bufferSize + bufferPosition >= Capacity)
7978
{
80-
Append(tempBuffer[..written]);
79+
Grow(bufferSize + bufferPosition);
8180
}
82-
else
81+
82+
if (!value.TryFormat(buffer[bufferPosition..], out var written, format, null))
8383
{
8484
throw new InvalidOperationException($"Could not insert {value} into given buffer. Is the buffer (size: {bufferSize}) large enough?");
8585
}
86+
87+
bufferPosition += written;
8688
}
8789
}

0 commit comments

Comments
 (0)