Skip to content

Commit

Permalink
Avoid using stackalloc in a loop (#32375)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Feb 15, 2020
1 parent 98e57ab commit 68b05d4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/libraries/System.Private.Uri/src/System/IriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end
int next = start;
char ch;

Span<byte> maxUtf8EncodedSpan = stackalloc byte[4];

for (; next < end; ++next)
{
if ((ch = pInput[next]) == '%')
Expand Down Expand Up @@ -235,8 +237,6 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end

if (escape)
{
Span<byte> encodedBytes = stackalloc byte[4];

Rune rune;
if (surrogatePair)
{
Expand All @@ -247,8 +247,8 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end
rune = Rune.ReplacementChar;
}

int bytesWritten = rune.EncodeToUtf8(encodedBytes);
encodedBytes = encodedBytes.Slice(0, bytesWritten);
int bytesWritten = rune.EncodeToUtf8(maxUtf8EncodedSpan);
Span<byte> encodedBytes = maxUtf8EncodedSpan.Slice(0, bytesWritten);

foreach (byte b in encodedBytes)
{
Expand Down

0 comments on commit 68b05d4

Please sign in to comment.