-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathByteArrayExtensions.cs
34 lines (27 loc) · 1.05 KB
/
ByteArrayExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Runtime.CompilerServices;
namespace SecurityDriven.Inferno.Extensions
{
public static class ByteArrayExtensions
{
internal const int SHORT_BYTECOPY_THRESHOLD = 32;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] CloneBytes(this byte[] bytes) => CloneBytes(bytes, 0, bytes.Length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] CloneBytes(this byte[] bytes, int offset, int count)
{
var clone = new byte[count];
if (count <= SHORT_BYTECOPY_THRESHOLD)
for (int i = 0; i < count; ++i) clone[i] = bytes[offset + i];
else
Utils.BlockCopy(bytes, offset, clone, 0, count);
return clone;
}//CloneBytes()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ClearBytes(this byte[] bytes) => ClearBytes(bytes, 0, bytes.Length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ClearBytes(this byte[] bytes, int offset, int count)
{
System.Array.Clear(bytes, offset, count);
}//ClearBytes()
}//class ByteArrayExtensions
}//ns