XFEExtension
【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等
关注
0
Fork
0
Star
0
返回提交历史
Added
XFE各类拓展/BufferExtension.cs
+105
-0
Modified
XFE各类拓展/XFE各类拓展.csproj
+1
-0
XFEstudio/XFEExtension
添加Buffer扩展类 现在可以更加方便的对Buffer进行操作
4778c04
代码差异
2 个文件
+106
-0
@@ -0,0 +1,105 @@
1
using System.Collections.Generic;
2
3
namespace XFE各类拓展
4
{
5
/// <summary>
6
/// 对二进制数组的拓展
7
/// </summary>
8
public static class BufferExtension
9
{
10
/// <summary>
11
/// 获取第一个匹配Buffer的位置
12
/// </summary>
13
/// <param name="buffer"></param>
14
/// <param name="targetBuffer">要匹配Buffer</param>
15
/// <returns>没有查找到为-1</returns>
16
public static int IndexOf(this byte[] buffer, byte[] targetBuffer)
17
{
18
int index = -1;
19
for (int i = 0; i < buffer.Length; i++)
20
{
21
if (buffer[i] == targetBuffer[0])
22
{
23
bool isMatch = true;
24
for (int j = 0; j < targetBuffer.Length; j++)
25
{
26
if (buffer[i + j] != targetBuffer[j])
27
{
28
isMatch = false;
29
break;
30
}
31
}
32
if (isMatch)
33
{
34
index = i;
35
break;
36
}
37
}
38
}
39
return index;
40
}
41
/// <summary>
42
/// 获取所有匹配的Buffer的位置
43
/// </summary>
44
/// <param name="buffer"></param>
45
/// <param name="targetBuffer">要匹配的Buffer</param>
46
/// <param name="shareable">是否可以共用</param>
47
/// <returns>查找到的位置数组</returns>
48
public static int[] IndexesOf(this byte[] buffer, byte[] targetBuffer, bool shareable = false)
49
{
50
var indexes = new List<int>();
51
for (int i = 0; i < buffer.Length; i++)
52
{
53
if (buffer[i] == targetBuffer[0])
54
{
55
bool isMatch = true;
56
for (int j = 0; j < targetBuffer.Length; j++)
57
{
58
if (buffer[i + j] != targetBuffer[j])
59
{
60
isMatch = false;
61
break;
62
}
63
}
64
if (isMatch)
65
{
66
indexes.Add(i);
67
if (!shareable)
68
i += targetBuffer.Length - 1;
69
}
70
}
71
}
72
return indexes.ToArray();
73
}
74
/// <summary>
75
/// 替换Buffer中的指定Buffer为目标Buffer
76
/// </summary>
77
/// <param name="buffer"></param>
78
/// <param name="originBuffer"></param>
79
/// <param name="targetBuffer"></param>
80
/// <returns>替换后的Buffer</returns>
81
public static byte[] Replace(byte[] buffer, byte[] originBuffer, byte[] targetBuffer)
82
{
83
var indexes = IndexesOf(buffer, originBuffer);
84
var newBuffer = new byte[buffer.Length + (targetBuffer.Length - originBuffer.Length) * indexes.Length];
85
int index = 0;
86
for (int i = 0; i < indexes.Length; i++)
87
{
88
for (int j = index; j < indexes[i]; j++)
89
{
90
newBuffer[j] = buffer[j];
91
}
92
for (int j = 0; j < targetBuffer.Length; j++)
93
{
94
newBuffer[indexes[i] + j] = targetBuffer[j];
95
}
96
index = indexes[i] + targetBuffer.Length;
97
}
98
for (int i = index; i < newBuffer.Length; i++)
99
{
100
newBuffer[i] = buffer[i];
101
}
102
return newBuffer;
103
}
104
}
105
}
@@ -53,6 +53,7 @@
53
53
<ItemGroup>
54
54
<Compile Include="ArrayExtension.cs" />
55
55
<Compile Include="AttributeExtension.cs" />
56
<Compile Include="BufferExtension.cs" />
56
57
<Compile Include="CyberComm.cs" />
57
58
<Compile Include="FileExtension.cs" />
58
59
<Compile Include="ListExtension.cs" />