Skip to content

Commit 4233007

Browse files
committed
fixed: Wrong grow size
1 parent f8494e6 commit 4233007

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ public void Insert(int index, scoped ReadOnlySpan<char> value)
4242
throw new ArgumentOutOfRangeException(nameof(index), "The given index can't be bigger than the string itself.");
4343
}
4444

45-
bufferPosition += value.Length;
46-
if (bufferPosition > buffer.Length)
45+
var newLength = bufferPosition + value.Length;
46+
if (newLength > buffer.Length)
4747
{
48-
Grow(bufferPosition * 2);
48+
Grow(newLength * 2);
4949
}
5050

51+
bufferPosition = newLength;
52+
5153
// Move Slice at beginning index
5254
var oldPosition = bufferPosition - value.Length;
5355
var shift = index + value.Length;
@@ -74,12 +76,14 @@ private void InsertSpanFormattable<T>(int index, T value, ReadOnlySpan<char> for
7476
Span<char> tempBuffer = stackalloc char[bufferSize];
7577
if (value.TryFormat(tempBuffer, out var written, format, null))
7678
{
77-
bufferPosition += written;
78-
if (bufferPosition > buffer.Length)
79+
var newLength = bufferPosition + written;
80+
if (newLength > buffer.Length)
7981
{
80-
Grow(bufferPosition * 2);
82+
Grow(newLength * 2);
8183
}
8284

85+
bufferPosition = newLength;
86+
8387
// Move Slice at beginning index
8488
var oldPosition = bufferPosition - written;
8589
var shift = index + written;

0 commit comments

Comments
 (0)