-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteExtensions.cs
51 lines (44 loc) · 1.58 KB
/
ByteExtensions.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
//************************************************************************************************
// Copyright © 2021 Steven M Cohn. All rights reserved.
//************************************************************************************************
namespace ClipboardViewer
{
internal static class ByteExtensions
{
/// <summary>
/// OneMore Extension >> Determines if the current buffer ends with the given byte pattern
/// </summary>
/// <param name="buffer">A byte array</param>
/// <param name="pattern">A pattern of bytes to compare against the last bytes of buffer</param>
/// <returns>True if buffer ends with pattern, otherwise false</returns>
public static bool EndsWith(this byte[] buffer, byte[] pattern)
{
return With(buffer, pattern, buffer.Length - pattern.Length);
}
/// <summary>
/// OneMore Extension >> Determines if the current buffer starts with the given byte pattern.
/// </summary>
/// <param name="buffer">A byte array.</param>
/// <param name="pattern">A pattern of bytes to compare against the beginning bytes of buffer</param>
/// <returns>True if buffer starts with pattern, otherwise false</returns>
public static bool StartsWith(this byte[] buffer, byte[] pattern)
{
return With(buffer, pattern, 0);
}
private static bool With(byte[] buffer, byte[] pattern, int start)
{
if ((buffer.Length < pattern.Length) || (buffer.Length <= start))
{
return false;
}
for (int b = start, p = 0; p < pattern.Length; p++, b++)
{
if (buffer[b] != pattern[p])
{
return false;
}
}
return true;
}
}
}