-
Notifications
You must be signed in to change notification settings - Fork 139
/
ByteArrayExtensions.cs
88 lines (79 loc) · 2.47 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Diagnostics.Contracts;
namespace Libplanet
{
/// <summary>
/// This extension class enables some convenient methods to deal with byte array.
/// </summary>
/// <seealso cref="Address"/>
public static class ByteArrayExtensions
{
/// <summary>
/// Determines whether the beginning of this byte array instance matches a specified string.
/// </summary>
/// <param name="bytes">A byte array to check.</param>
/// <param name="prefix">The prefix byte array to compare.</param>
/// <returns>
/// true if <paramref name="prefix"/> matches the beginning of <paramref name="bytes"/>;
/// otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="bytes"/> or <paramref name="prefix"/> is null.
/// </exception>
[Pure]
public static bool StartsWith(this byte[]? bytes, byte[]? prefix)
{
if (bytes is null)
{
throw new ArgumentNullException(nameof(bytes));
}
if (prefix is null)
{
throw new ArgumentNullException(nameof(prefix));
}
if (prefix.Length > bytes.Length)
{
return false;
}
for (int i = 0, j = 0; i < bytes.Length && j < prefix.Length; i++, j++)
{
if (bytes[i] != prefix[j])
{
return false;
}
}
return true;
}
[Pure]
internal static int IndexOf(this byte[] bytes, byte[] sub)
{
// TODO: Make this method public and write the docs.
if (bytes.Length < 1)
{
return sub.Length > 0 ? -1 : 0;
}
else if (bytes.Length < sub.Length)
{
return -1;
}
// TODO: We need to optimize this...
for (int i = 0; i < bytes.Length; i++)
{
bool found = true;
for (int j = 0; j < sub.Length; j++)
{
if (bytes[i + j] != sub[j])
{
found = false;
break;
}
}
if (found)
{
return i;
}
}
return -1;
}
}
}