2021-01-13 18:56:32 -08:00
|
|
|
/*************************************************************************
|
|
|
|
|
* ModernUO *
|
2023-08-09 09:09:26 -07:00
|
|
|
* Copyright 2019-2023 - ModernUO Development Team *
|
2021-01-13 18:56:32 -08:00
|
|
|
* Email: hi@modernuo.com *
|
|
|
|
|
* File: TextEncoding.cs *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
namespace Server.Text;
|
|
|
|
|
|
|
|
|
|
public static class TextEncoding
|
2021-01-13 18:56:32 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
private static Encoding m_UTF8, m_Unicode, m_UnicodeLE;
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static Encoding UTF8 => m_UTF8 ??= new UTF8Encoding(false, false);
|
|
|
|
|
public static Encoding Unicode => m_Unicode ??= new UnicodeEncoding(true, false, false);
|
|
|
|
|
public static Encoding UnicodeLE => m_UnicodeLE ??= new UnicodeEncoding(false, false, false);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesAscii(this string str) => GetBytes(str, Encoding.ASCII);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesBigUni(this string str) => GetBytes(str, Unicode);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesLittleUni(this string str) => GetBytes(str, UnicodeLE);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesUtf8(this string str) => GetBytes(str, UTF8);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesAscii(this ReadOnlySpan<char> str) => GetBytes(str, Encoding.ASCII);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesBigUni(this ReadOnlySpan<char> str) => GetBytes(str, Unicode);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesLittleUni(this ReadOnlySpan<char> str) => GetBytes(str, UnicodeLE);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static byte[] GetBytesUtf8(this ReadOnlySpan<char> str) => GetBytes(str, UTF8);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// Unlike the one built into the encoder, this avoids local init
|
|
|
|
|
public static byte[] GetBytes(ReadOnlySpan<char> str, Encoding encoding)
|
|
|
|
|
{
|
|
|
|
|
if (str.Length == 0)
|
2021-01-13 18:56:32 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
return Array.Empty<byte>();
|
2021-01-13 18:56:32 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
|
|
|
|
|
encoding.GetBytes(str, bytes);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
return bytes;
|
|
|
|
|
}
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// Unlike the one built into the encoder, this avoids local init
|
|
|
|
|
public static byte[] GetBytes(string str, Encoding encoding)
|
|
|
|
|
{
|
|
|
|
|
if (str.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<byte>();
|
2021-01-13 18:56:32 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
|
|
|
|
|
encoding.GetBytes(str, 0, str.Length, bytes, 0);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
return bytes;
|
|
|
|
|
}
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesAscii(this string str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesAscii(this ReadOnlySpan<char> str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesBigUni(this string str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesBigUni(this ReadOnlySpan<char> str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesLittleUni(this string str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesLittleUni(this ReadOnlySpan<char> str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesUtf8(this string str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetBytesUtf8(this ReadOnlySpan<char> str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static int GetByteLengthForEncoding(this Encoding encoding) =>
|
|
|
|
|
encoding.BodyName switch
|
2021-01-13 18:56:32 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
"utf-16BE" => 2,
|
|
|
|
|
"utf-16" => 2,
|
|
|
|
|
"utf-32BE" => 3,
|
|
|
|
|
"utf-32" => 3,
|
|
|
|
|
_ => 1
|
|
|
|
|
};
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-10-09 00:57:53 -07:00
|
|
|
private static bool IsSafeChar(ushort c) => c is >= 0x20 and < 0xFFFE;
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
|
|
|
|
|
{
|
|
|
|
|
string s = encoding.GetString(span);
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (!safeString)
|
|
|
|
|
{
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadOnlySpan<char> chars = s.AsSpan();
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
using var sb = new ValueStringBuilder(stackalloc char[256]);
|
|
|
|
|
var hasDoneAnyReplacements = false;
|
2021-01-13 18:56:32 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
for (int i = 0, last = 0; i < chars.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!IsSafeChar(chars[i]) && i == chars.Length - 1)
|
2021-01-13 18:56:32 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
hasDoneAnyReplacements = true;
|
|
|
|
|
sb.Append(chars.Slice(last, i - last));
|
|
|
|
|
last = i + 1; // Skip the unsafe char
|
2021-01-13 18:56:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
return !hasDoneAnyReplacements ? s : sb.ToString();
|
2021-01-13 18:56:32 -08:00
|
|
|
}
|
|
|
|
|
}
|