Skip to content

Commit

Permalink
Fixed renting buffer from pool in INumberBase (#102755)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoidl authored Jun 5, 2024
1 parent 933e0a3 commit db0eb5d
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,15 @@ static virtual TSelf Parse(ReadOnlySpan<byte> utf8Text, NumberStyles style, IFor
scoped Span<char> utf16Text;
int textMaxCharCount = Encoding.UTF8.GetMaxCharCount(utf8Text.Length);

if (textMaxCharCount < 256)
if (textMaxCharCount <= 256)
{
utf16TextArray = null;
utf16Text = stackalloc char[256];
}
else
{
utf16TextArray = ArrayPool<char>.Shared.Rent(textMaxCharCount);
utf16Text = utf16TextArray.AsSpan(0, textMaxCharCount);
utf16Text = utf16TextArray;
}

OperationStatus utf8TextStatus = Utf8.ToUtf16(utf8Text, utf16Text, out _, out int utf16TextLength, replaceInvalidSequences: false);
Expand Down Expand Up @@ -440,15 +440,15 @@ static virtual bool TryParse(ReadOnlySpan<byte> utf8Text, NumberStyles style, IF
scoped Span<char> utf16Text;
int textMaxCharCount = Encoding.UTF8.GetMaxCharCount(utf8Text.Length);

if (textMaxCharCount < 256)
if (textMaxCharCount <= 256)
{
utf16TextArray = null;
utf16Text = stackalloc char[256];
}
else
{
utf16TextArray = ArrayPool<char>.Shared.Rent(textMaxCharCount);
utf16Text = utf16TextArray.AsSpan(0, textMaxCharCount);
utf16Text = utf16TextArray;
}

OperationStatus utf8TextStatus = Utf8.ToUtf16(utf8Text, utf16Text, out _, out int utf16TextLength, replaceInvalidSequences: false);
Expand Down Expand Up @@ -486,15 +486,15 @@ bool IUtf8SpanFormattable.TryFormat(Span<byte> utf8Destination, out int bytesWri
scoped Span<char> utf16Destination;
int destinationMaxCharCount = Encoding.UTF8.GetMaxCharCount(utf8Destination.Length);

if (destinationMaxCharCount < 256)
if (destinationMaxCharCount <= 256)
{
utf16DestinationArray = null;
utf16Destination = stackalloc char[256];
}
else
{
utf16DestinationArray = ArrayPool<char>.Shared.Rent(destinationMaxCharCount);
utf16Destination = utf16DestinationArray.AsSpan(0, destinationMaxCharCount);
utf16Destination = utf16DestinationArray;
}

if (!TryFormat(utf16Destination, out int charsWritten, format, provider))
Expand Down Expand Up @@ -542,15 +542,15 @@ static TSelf IUtf8SpanParsable<TSelf>.Parse(ReadOnlySpan<byte> utf8Text, IFormat
scoped Span<char> utf16Text;
int textMaxCharCount = Encoding.UTF8.GetMaxCharCount(utf8Text.Length);

if (textMaxCharCount < 256)
if (textMaxCharCount <= 256)
{
utf16TextArray = null;
utf16Text = stackalloc char[256];
}
else
{
utf16TextArray = ArrayPool<char>.Shared.Rent(textMaxCharCount);
utf16Text = utf16TextArray.AsSpan(0, textMaxCharCount);
utf16Text = utf16TextArray;
}

OperationStatus utf8TextStatus = Utf8.ToUtf16(utf8Text, utf16Text, out _, out int utf16TextLength, replaceInvalidSequences: false);
Expand Down Expand Up @@ -589,15 +589,15 @@ static bool IUtf8SpanParsable<TSelf>.TryParse(ReadOnlySpan<byte> utf8Text, IForm
scoped Span<char> utf16Text;
int textMaxCharCount = Encoding.UTF8.GetMaxCharCount(utf8Text.Length);

if (textMaxCharCount < 256)
if (textMaxCharCount <= 256)
{
utf16TextArray = null;
utf16Text = stackalloc char[256];
}
else
{
utf16TextArray = ArrayPool<char>.Shared.Rent(textMaxCharCount);
utf16Text = utf16TextArray.AsSpan(0, textMaxCharCount);
utf16Text = utf16TextArray;
}

OperationStatus utf8TextStatus = Utf8.ToUtf16(utf8Text, utf16Text, out _, out int utf16TextLength, replaceInvalidSequences: false);
Expand Down

0 comments on commit db0eb5d

Please sign in to comment.