Skip to content

Instantly share code, notes, and snippets.

@usausa
Last active April 14, 2018 07:42
Show Gist options
  • Save usausa/79bb5fd09ca3e2d540d0561e7f9e6079 to your computer and use it in GitHub Desktop.
Save usausa/79bb5fd09ca3e2d540d0561e7f9e6079 to your computer and use it in GitHub Desktop.
public static unsafe byte[] Fill(this byte[] array, int offset, int length, byte value)
{
if ((length <= 0) || (array == null))
{
return array;
}
fixed (byte* pSrc = &array[offset])
{
*pSrc = value;
byte* pDst;
int copy;
for (copy = 1; copy <= length / 2; copy <<= 1)
{
pDst = pSrc + copy;
Buffer.MemoryCopy(pSrc, pDst, length - copy, copy);
}
pDst = pSrc + copy;
Buffer.MemoryCopy(pSrc, pDst, length - copy, length - copy);
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment