-
Notifications
You must be signed in to change notification settings - Fork 1
/
Text.cs
59 lines (53 loc) · 1.98 KB
/
Text.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
using System.Text;
namespace LC6464.Base16384;
/// <summary>
/// Base16384 编解码器。
/// </summary>
public static partial class Base16384 {
/// <summary>
/// 将 UTF-16 BE 编码的数据转换为 UTF-8 编码的数据。
/// </summary>
/// <param name="data">UTF-16 BE 编码的数据</param>
/// <returns>UTF-8 编码的数据</returns>
public static ReadOnlySpan<byte> ConvertFromUtf16BEBytesToUtf8Bytes(this ReadOnlySpan<byte> data) =>
Encoding.Convert(Encoding.BigEndianUnicode, Encoding.UTF8, data.ToArray());
/// <summary>
/// 将 UTF-16 BE 编码的数据转换为 UTF-8 with BOM 编码的数据。
/// </summary>
/// <param name="data">UTF-16 BE 编码的数据</param>
/// <returns>UTF-8 with BOM 编码的数据</returns>
public static ReadOnlySpan<byte> ConvertFromUtf16BEBytesToUtf8BOMBytes(this ReadOnlySpan<byte> data) {
var utf8Bytes = data.ConvertFromUtf16BEBytesToUtf8Bytes();
Span<byte> buffer = new byte[utf8Bytes.Length + 3];
Utf8Preamble.CopyTo(buffer);
utf8Bytes.CopyTo(buffer[3..]);
return buffer;
}
/// <summary>
/// 将 UTF-16 BE 编码的数据转换为字符串。
/// </summary>
/// <param name="data">UTF-16 BE 编码的数据</param>
/// <returns>字符串</returns>
public static ReadOnlySpan<byte> ConvertFromUtf16BEBytesToUtf16LEBytes(this ReadOnlySpan<byte> data) {
Span<byte> buffer = new byte[data.Length];
var count = data.Length / 2;
for (var i = 0; i < count; i++) {
buffer[i * 2] = data[(i * 2) + 1];
buffer[(i * 2) + 1] = data[i * 2];
}
return buffer;
}
/// <summary>
/// 将 UTF-16 BE 编码的数据转换为字符串。
/// </summary>
/// <param name="data">UTF-16 BE 编码的数据</param>
/// <returns>字符串</returns>
public static ReadOnlySpan<char> ConvertFromUtf16BEBytesToString(this ReadOnlySpan<byte> data) {
var count = data.Length / 2;
Span<char> buffer = new char[count];
for (var i = 0; i < count; i++) {
buffer[i] = (char)((data[i * 2] << 8) | data[(i * 2) + 1]);
}
return buffer;
}
}