Skip to content

Commit

Permalink
Use Span.Fill in String(Char, Int32) constructor (#60269)
Browse files Browse the repository at this point in the history
* Apply suggestions from code review

Co-authored-by: Adam Sitnik <[email protected]>
Co-authored-by: Jeremy Koritzinsky <[email protected]>
  • Loading branch information
3 people authored Nov 15, 2021
1 parent 3d7e89c commit 31dd708
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions src/libraries/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,35 +321,9 @@ string Ctor(char c, int count)
}

string result = FastAllocateString(count);

if (c != '\0') // Fast path null char string
if (c != '\0')
{
unsafe
{
fixed (char* dest = &result._firstChar)
{
uint cc = (uint)((c << 16) | c);
uint* dmem = (uint*)dest;
if (count >= 4)
{
count -= 4;
do
{
dmem[0] = cc;
dmem[1] = cc;
dmem += 2;
count -= 4;
} while (count >= 0);
}
if ((count & 2) != 0)
{
*dmem = cc;
dmem++;
}
if ((count & 1) != 0)
((char*)dmem)[0] = c;
}
}
SpanHelpers.Fill(ref result._firstChar, (uint)count, c);
}
return result;
}
Expand Down

0 comments on commit 31dd708

Please sign in to comment.