From aa5a89951c65a19ee80ac50bac12a1d78dd491f4 Mon Sep 17 00:00:00 2001 From: lc6464 <64722907+lc6464@users.noreply.github.com> Date: Wed, 13 Sep 2023 23:23:15 +0800 Subject: [PATCH] v2.0.0-RC.1 --- LC6464.Base16384.csproj | 4 ++-- Shared.cs | 5 +++++ Text.cs | 11 +++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/LC6464.Base16384.csproj b/LC6464.Base16384.csproj index 7001997..d5fb807 100644 --- a/LC6464.Base16384.csproj +++ b/LC6464.Base16384.csproj @@ -17,8 +17,8 @@ GPL-3.0-only True LC - 2.0.0-Preview.2 - 修复一些高级 API 逻辑错误以及 Span 访问越界等致命问题。 + 2.0.0-RC.1 + 完成了 ToUtf8BOMBytes 方法。 diff --git a/Shared.cs b/Shared.cs index cbe49e7..98cbd3f 100644 --- a/Shared.cs +++ b/Shared.cs @@ -57,6 +57,11 @@ private static void EncodeLengthInternal(long dataLength, out long outLength, ou /// public static ReadOnlySpan Utf16LEPreamble => new byte[] { 0xFF, 0xFE }; + /// + /// UTF-8 with BOM 编码的 BOM,应在文件头部出现。 + /// + public static ReadOnlySpan Utf8Preamble => new byte[] { 0xEF, 0xBB, 0xBF }; + /// /// 计算编码指针需要的长度。 diff --git a/Text.cs b/Text.cs index 6498015..22c5f99 100644 --- a/Text.cs +++ b/Text.cs @@ -14,15 +14,18 @@ public static partial class Base16384 { public static ReadOnlySpan ConvertFromUtf16BEBytesToUtf8Bytes(this ReadOnlySpan data) => Encoding.Convert(Encoding.BigEndianUnicode, Encoding.UTF8, data.ToArray()); -#if DEBUG // 此处仍有问题,暂时不使用 /// /// 将 UTF-16 BE 编码的数据转换为 UTF-8 with BOM 编码的数据。 /// /// UTF-16 BE 编码的数据 /// UTF-8 with BOM 编码的数据 - public static ReadOnlySpan ConvertFromUtf16BEBytesToUtf8BOMBytes(this ReadOnlySpan data) => - Encoding.Convert(Encoding.BigEndianUnicode, new UTF8Encoding(true), data.ToArray()); -#endif + public static ReadOnlySpan ConvertFromUtf16BEBytesToUtf8BOMBytes(this ReadOnlySpan data) { + Span buffer = new byte[data.Length + 3]; + Utf8Preamble.CopyTo(buffer); + data.ConvertFromUtf16BEBytesToUtf8Bytes().CopyTo(buffer[3..]); + return buffer; + } + /// /// 将 UTF-16 BE 编码的数据转换为字符串。