-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.cs
131 lines (121 loc) · 4.82 KB
/
Utility.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using Ionic.Zlib;
using System.Text;
namespace TemplateConverTools
{
internal static class Utility
{
internal const string dType = "d";
internal const string fType = "f";
internal const string sType = "s";
public static byte[] ComressData(byte[] originalData)
{
using MemoryStream originalStream = new(originalData);
using MemoryStream compressedStream = new();
using (ZlibStream zlibStream = new(compressedStream, Ionic.Zlib.CompressionMode.Compress))
{
originalStream.CopyTo(zlibStream);
}
return compressedStream.ToArray();
}
public static byte[] DecompressData(byte[] compressedData)
{
using MemoryStream compressedStream = new(compressedData);
using MemoryStream decompressedStream = new();
using (ZlibStream zlibStream = new(compressedStream, Ionic.Zlib.CompressionMode.Decompress))
{
zlibStream.CopyTo(decompressedStream);
}
return decompressedStream.ToArray();
}
public static bool IsEndTag(string sesName)
{
return "deleted" == sesName || "packEnd" == sesName;
}
public static void UTFConverBytes(string str, out byte[] utf8Bytes, out byte[] byteLen)
{
utf8Bytes = Encoding.UTF8.GetBytes(str);
byteLen = BitConverter.GetBytes((ushort)utf8Bytes.Length);
}
public static void UTFConverBigEndianBytes(string str, out byte[] bytes)
{
bytes = Encoding.UTF8.GetBytes(str);
var byteLen = BitConverter.GetBytes((ushort)bytes.Length).Reverse().ToArray();
bytes = ConcatenateArrays(ref byteLen, ref bytes);
static byte[] ConcatenateArrays(ref byte[] array1, ref byte[] array2)
{
byte[] result = new byte[array1.Length + array2.Length];
Buffer.BlockCopy(array1, 0, result, 0, array1.Length);
Buffer.BlockCopy(array2, 0, result, array1.Length, array2.Length);
return result;
}
}
public static byte[] UTFConverBigEndianBytes(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);
var byteLen = BitConverter.GetBytes((ushort)bytes.Length).Reverse().ToArray();
return bytes = ConcatenateArrays(ref byteLen, ref bytes);
static byte[] ConcatenateArrays(ref byte[] array1, ref byte[] array2)
{
byte[] result = new byte[array1.Length + array2.Length];
Buffer.BlockCopy(array1, 0, result, 0, array1.Length);
Buffer.BlockCopy(array2, 0, result, array1.Length, array2.Length);
return result;
}
}
public static string ByteConverString(byte[] bytes)
{
if (bytes.Length < 2)
throw new ArgumentException("Byte array is too short to contain a length prefix.");
var lengthBytes = bytes.Take(2).Reverse().ToArray();
ushort length = BitConverter.ToUInt16(lengthBytes, 0);
if (bytes.Length < 2 + length)
throw new ArgumentException("Byte array does not match the length prefix.");
var stringBytes = bytes.Skip(2).Take(length).ToArray();
return Encoding.UTF8.GetString(stringBytes);
}
public static string ExtractFirstNumberString(string input)
{
StringBuilder numberString = new();
foreach (char ch in input)
{
if (char.IsDigit(ch))
{
// 如果是数字,追加到字符串中
numberString.Append(ch);
}
else
{
// 如果不是数字,停止循环
break;
}
}
return (numberString.Length > 0) ? numberString.ToString() : "0";
}
internal static List<string> SplitString(string input)
{
List<string> parts = new();
StringBuilder part = new();
bool insideQuotes = false;
for (int i = 0; i < input.Length; i++)
{
char currentChar = input[i];
if (currentChar == '"')
{
insideQuotes = !insideQuotes;
}
else if (currentChar == ',' && !insideQuotes)
{
parts.Add(part.ToString().Trim());
part.Clear();
}
else
{
part.Append(currentChar);
}
}
parts.Add(part.ToString().Trim()); // Add the last part
part.Clear();
return parts;
}
}
}