fix(core): Streamlines text encoding (#407)
- [X] Streamlines text encoding
This commit is contained in:
parent
5631c1da6d
commit
211c2ba8ee
25 changed files with 274 additions and 137 deletions
|
|
@ -19,6 +19,7 @@ using System.Buffers.Binary;
|
|||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
|
|
@ -243,7 +244,7 @@ namespace Server.Network
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1)
|
||||
{
|
||||
int sizeT = Utility.GetByteLengthForEncoding(encoding);
|
||||
int sizeT = TextEncoding.GetByteLengthForEncoding(encoding);
|
||||
|
||||
bool isFixedLength = fixedLength > -1;
|
||||
|
||||
|
|
@ -271,7 +272,7 @@ namespace Server.Network
|
|||
var firstLength = Math.Min(_first.Length - Position, size);
|
||||
|
||||
// Find terminator
|
||||
index = Utility.IndexOfTerminator(_first.Slice(Position, firstLength), sizeT);
|
||||
index = _first.Slice(Position, firstLength).IndexOfTerminator(sizeT);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
|
|
@ -283,7 +284,7 @@ namespace Server.Network
|
|||
}
|
||||
else
|
||||
{
|
||||
index = Utility.IndexOfTerminator(_second.Slice(0, remaining), sizeT);
|
||||
index = _second.Slice(0, remaining).IndexOfTerminator(sizeT);
|
||||
|
||||
int secondLength = index < 0 ? remaining : index;
|
||||
int length = firstLength + secondLength;
|
||||
|
|
@ -294,7 +295,7 @@ namespace Server.Network
|
|||
_second.Slice(0, secondLength).CopyTo(bytes.Slice(firstLength));
|
||||
|
||||
Position += length + (index >= 0 ? sizeT : 0);
|
||||
return Utility.GetString(bytes, encoding, safeString);
|
||||
return TextEncoding.GetString(bytes, encoding, safeString);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -304,7 +305,7 @@ namespace Server.Network
|
|||
{
|
||||
size = Math.Min(remaining, size);
|
||||
span = _second.Slice( Position - _first.Length, size);
|
||||
index = Utility.IndexOfTerminator(span, sizeT);
|
||||
index = span.IndexOfTerminator(sizeT);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
|
|
@ -317,41 +318,41 @@ namespace Server.Network
|
|||
}
|
||||
|
||||
Position += isFixedLength ? size : index + sizeT;
|
||||
return Utility.GetString(span, encoding, safeString);
|
||||
return TextEncoding.GetString(span, encoding, safeString);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUniSafe(int fixedLength) => ReadString(Utility.UnicodeLE, true, fixedLength);
|
||||
public string ReadLittleUniSafe(int fixedLength) => ReadString(TextEncoding.UnicodeLE, true, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUniSafe() => ReadString(Utility.UnicodeLE, true);
|
||||
public string ReadLittleUniSafe() => ReadString(TextEncoding.UnicodeLE, true);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUni(int fixedLength) => ReadString(Utility.UnicodeLE, false, fixedLength);
|
||||
public string ReadLittleUni(int fixedLength) => ReadString(TextEncoding.UnicodeLE, false, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUni() => ReadString(Utility.UnicodeLE);
|
||||
public string ReadLittleUni() => ReadString(TextEncoding.UnicodeLE);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUniSafe(int fixedLength) => ReadString(Utility.Unicode, true, fixedLength);
|
||||
public string ReadBigUniSafe(int fixedLength) => ReadString(TextEncoding.Unicode, true, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUniSafe() => ReadString(Utility.Unicode, true);
|
||||
public string ReadBigUniSafe() => ReadString(TextEncoding.Unicode, true);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUni(int fixedLength) => ReadString(Utility.Unicode, false, fixedLength);
|
||||
public string ReadBigUni(int fixedLength) => ReadString(TextEncoding.Unicode, false, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUni() => ReadString(Utility.Unicode);
|
||||
public string ReadBigUni() => ReadString(TextEncoding.Unicode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadUTF8Safe(int fixedLength) => ReadString(Utility.UTF8, true, fixedLength);
|
||||
public string ReadUTF8Safe(int fixedLength) => ReadString(TextEncoding.UTF8, true, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadUTF8Safe() => ReadString(Utility.UTF8, true);
|
||||
public string ReadUTF8Safe() => ReadString(TextEncoding.UTF8, true);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadUTF8() => ReadString(Utility.UTF8);
|
||||
public string ReadUTF8() => ReadString(TextEncoding.UTF8);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadAsciiSafe(int fixedLength) => ReadString(Encoding.ASCII, true, fixedLength);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ using System.IO;
|
|||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Server;
|
||||
using Server.Text;
|
||||
|
||||
namespace System.Buffers
|
||||
{
|
||||
|
|
@ -418,7 +419,7 @@ namespace System.Buffers
|
|||
{
|
||||
if (fixedLength < 0) { fixedLength = value.Length; }
|
||||
|
||||
WriteString(value.AsSpan(0, Math.Min(fixedLength, value.Length)), Utility.UnicodeLE);
|
||||
WriteString(value.AsSpan(0, Math.Min(fixedLength, value.Length)), TextEncoding.UnicodeLE);
|
||||
var count = fixedLength - value.Length;
|
||||
if (count > 0)
|
||||
{
|
||||
|
|
@ -429,7 +430,7 @@ namespace System.Buffers
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteLittleUniNull(string value)
|
||||
{
|
||||
WriteString(value, Utility.UnicodeLE);
|
||||
WriteString(value, TextEncoding.UnicodeLE);
|
||||
Write((ushort)0);
|
||||
}
|
||||
|
||||
|
|
@ -438,7 +439,7 @@ namespace System.Buffers
|
|||
{
|
||||
if (fixedLength < 0) { fixedLength = value.Length; }
|
||||
|
||||
WriteString(value.AsSpan(0, Math.Min(fixedLength, value.Length)), Utility.Unicode);
|
||||
WriteString(value.AsSpan(0, Math.Min(fixedLength, value.Length)), TextEncoding.Unicode);
|
||||
var count = fixedLength - value.Length;
|
||||
if (count > 0)
|
||||
{
|
||||
|
|
@ -449,17 +450,17 @@ namespace System.Buffers
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteBigUniNull(string value)
|
||||
{
|
||||
WriteString(value, Utility.Unicode);
|
||||
WriteString(value, TextEncoding.Unicode);
|
||||
Write((ushort)0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteUTF8(string value) => WriteString(value, Utility.UTF8);
|
||||
public void WriteUTF8(string value) => WriteString(value, TextEncoding.UTF8);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteUTF8Null(string value)
|
||||
{
|
||||
WriteString(value, Utility.UTF8);
|
||||
WriteString(value, TextEncoding.UTF8);
|
||||
Write((byte)0);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ using System.IO;
|
|||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Server;
|
||||
using Server.Text;
|
||||
|
||||
namespace System.Buffers
|
||||
{
|
||||
|
|
@ -141,7 +142,7 @@ namespace System.Buffers
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1)
|
||||
{
|
||||
int sizeT = Utility.GetByteLengthForEncoding(encoding);
|
||||
int sizeT = TextEncoding.GetByteLengthForEncoding(encoding);
|
||||
|
||||
bool isFixedLength = fixedLength > -1;
|
||||
|
||||
|
|
@ -161,44 +162,44 @@ namespace System.Buffers
|
|||
size = remaining - (remaining & (sizeT - 1));
|
||||
}
|
||||
|
||||
int index = Utility.IndexOfTerminator(_buffer.Slice(Position, size), sizeT);
|
||||
int index = _buffer.Slice(Position, size).IndexOfTerminator(sizeT);
|
||||
|
||||
Position += isFixedLength || index < 0 ? size : index + sizeT;
|
||||
return Utility.GetString(_buffer.Slice(Position, index < 0 ? size : index), encoding, safeString);
|
||||
return TextEncoding.GetString(_buffer.Slice(Position, index < 0 ? size : index), encoding, safeString);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUniSafe(int fixedLength) => ReadString(Utility.UnicodeLE, true, fixedLength);
|
||||
public string ReadLittleUniSafe(int fixedLength) => ReadString(TextEncoding.UnicodeLE, true, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUniSafe() => ReadString(Utility.UnicodeLE, true);
|
||||
public string ReadLittleUniSafe() => ReadString(TextEncoding.UnicodeLE, true);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUni(int fixedLength) => ReadString(Utility.UnicodeLE, false, fixedLength);
|
||||
public string ReadLittleUni(int fixedLength) => ReadString(TextEncoding.UnicodeLE, false, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadLittleUni() => ReadString(Utility.UnicodeLE);
|
||||
public string ReadLittleUni() => ReadString(TextEncoding.UnicodeLE);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUniSafe(int fixedLength) => ReadString(Utility.Unicode, true, fixedLength);
|
||||
public string ReadBigUniSafe(int fixedLength) => ReadString(TextEncoding.Unicode, true, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUniSafe() => ReadString(Utility.Unicode, true);
|
||||
public string ReadBigUniSafe() => ReadString(TextEncoding.Unicode, true);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUni(int fixedLength) => ReadString(Utility.Unicode, false, fixedLength);
|
||||
public string ReadBigUni(int fixedLength) => ReadString(TextEncoding.Unicode, false, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadBigUni() => ReadString(Utility.Unicode);
|
||||
public string ReadBigUni() => ReadString(TextEncoding.Unicode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadUTF8Safe(int fixedLength) => ReadString(Utility.UTF8, true, fixedLength);
|
||||
public string ReadUTF8Safe(int fixedLength) => ReadString(TextEncoding.UTF8, true, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadUTF8Safe() => ReadString(Utility.UTF8, true);
|
||||
public string ReadUTF8Safe() => ReadString(TextEncoding.UTF8, true);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadUTF8() => ReadString(Utility.UTF8);
|
||||
public string ReadUTF8() => ReadString(TextEncoding.UTF8);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public string ReadAsciiSafe(int fixedLength) => ReadString(Encoding.ASCII, true, fixedLength);
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ using System.Runtime.CompilerServices;
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Server;
|
||||
using Server.Text;
|
||||
|
||||
namespace System.Buffers
|
||||
{
|
||||
|
|
@ -264,38 +265,38 @@ namespace System.Buffers
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteLittleUni(string value) => WriteString<char>(value, Utility.UnicodeLE);
|
||||
public void WriteLittleUni(string value) => WriteString<char>(value, TextEncoding.UnicodeLE);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteLittleUniNull(string value)
|
||||
{
|
||||
WriteString<char>(value, Utility.UnicodeLE);
|
||||
WriteString<char>(value, TextEncoding.UnicodeLE);
|
||||
Write((ushort)0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteLittleUni(string value, int fixedLength) => WriteString<char>(value, Utility.UnicodeLE, fixedLength);
|
||||
public void WriteLittleUni(string value, int fixedLength) => WriteString<char>(value, TextEncoding.UnicodeLE, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteBigUni(string value) => WriteString<char>(value, Utility.Unicode);
|
||||
public void WriteBigUni(string value) => WriteString<char>(value, TextEncoding.Unicode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteBigUniNull(string value)
|
||||
{
|
||||
WriteString<char>(value, Utility.Unicode);
|
||||
WriteString<char>(value, TextEncoding.Unicode);
|
||||
Write((ushort)0); // '\0'
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteBigUni(string value, int fixedLength) => WriteString<char>(value, Utility.Unicode, fixedLength);
|
||||
public void WriteBigUni(string value, int fixedLength) => WriteString<char>(value, TextEncoding.Unicode, fixedLength);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteUTF8(string value) => WriteString<byte>(value, Utility.UTF8);
|
||||
public void WriteUTF8(string value) => WriteString<byte>(value, TextEncoding.UTF8);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void WriteUTF8Null(string value)
|
||||
{
|
||||
WriteString<byte>(value, Utility.UTF8);
|
||||
WriteString<byte>(value, TextEncoding.UTF8);
|
||||
Write((byte)0); // '\0'
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Server.Network;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Gumps
|
||||
{
|
||||
|
|
@ -248,7 +249,7 @@ namespace Server.Gumps
|
|||
state.SendDisplayGump(this, out m_Switches, out m_TextEntries);
|
||||
}
|
||||
|
||||
public static byte[] StringToBuffer(string str) => Encoding.ASCII.GetBytes(str);
|
||||
public static byte[] StringToBuffer(string str) => str.GetBytesAscii();
|
||||
|
||||
public virtual void OnResponse(NetState sender, RelayInfo info)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ using System.IO;
|
|||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Json
|
||||
{
|
||||
|
|
@ -62,7 +63,7 @@ namespace Server.Json
|
|||
return default;
|
||||
}
|
||||
|
||||
var text = File.ReadAllText(filePath, Utility.UTF8);
|
||||
var text = File.ReadAllText(filePath, TextEncoding.UTF8);
|
||||
return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ using System.IO;
|
|||
using System.Net;
|
||||
using System.Text;
|
||||
using Server.Guilds;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
|
|
@ -37,7 +38,7 @@ namespace Server
|
|||
public BufferReader(byte[] buffer)
|
||||
{
|
||||
_buffer = buffer;
|
||||
_encoding = Utility.UTF8;
|
||||
_encoding = TextEncoding.UTF8;
|
||||
}
|
||||
|
||||
public void SwapBuffers(byte[] newBuffer, out byte[] oldBuffer)
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ using System.IO;
|
|||
using System.Net;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
|
|
@ -56,7 +57,7 @@ namespace Server
|
|||
public BufferWriter(byte[] buffer, bool prefixStr)
|
||||
{
|
||||
m_PrefixStrings = prefixStr;
|
||||
m_Encoding = Utility.UTF8;
|
||||
m_Encoding = TextEncoding.UTF8;
|
||||
_buffer = buffer;
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +68,7 @@ namespace Server
|
|||
public BufferWriter(int count, bool prefixStr)
|
||||
{
|
||||
m_PrefixStrings = prefixStr;
|
||||
m_Encoding = Utility.UTF8;
|
||||
m_Encoding = TextEncoding.UTF8;
|
||||
_buffer = GC.AllocateUninitializedArray<byte>(count < 1 ? BufferSize : count);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
using System;
|
||||
|
||||
namespace Server
|
||||
namespace Server.Text
|
||||
{
|
||||
public static class HexStringConverter
|
||||
{
|
||||
|
|
@ -66,7 +66,7 @@ namespace Server
|
|||
const uint delimiter = 0x20002C; // ", "
|
||||
const char openBracket = '[';
|
||||
const char closeBracket = ']';
|
||||
var length = bytes.Length * 4; // len * 2 + (len - 1) * 2 + 2
|
||||
var length = Math.Max(2, bytes.Length * 4); // len * 2 + (len - 1) * 2 + 2
|
||||
|
||||
var result = new string((char)0, length);
|
||||
fixed (char* resultP = result)
|
||||
|
|
@ -17,6 +17,7 @@ using System;
|
|||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
|
|
@ -240,5 +241,14 @@ namespace Server
|
|||
|
||||
return list;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfTerminator(this ReadOnlySpan<byte> buffer, int sizeT) =>
|
||||
sizeT switch
|
||||
{
|
||||
2 => MemoryMarshal.Cast<byte, char>(buffer).IndexOf((char)0) * 2,
|
||||
4 => MemoryMarshal.Cast<byte, uint>(buffer).IndexOf((uint)0) * 4,
|
||||
_ => buffer.IndexOf((byte)0)
|
||||
};
|
||||
}
|
||||
}
|
||||
148
Projects/Server/Text/TextEncoding.cs
Normal file
148
Projects/Server/Text/TextEncoding.cs
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* 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;
|
||||
using Server.Buffers;
|
||||
|
||||
namespace Server.Text
|
||||
{
|
||||
public static class TextEncoding
|
||||
{
|
||||
private static Encoding m_UTF8, m_Unicode, m_UnicodeLE;
|
||||
|
||||
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);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesAscii(this string str) => GetBytes(str, Encoding.ASCII);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesBigUni(this string str) => GetBytes(str, Unicode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesLittleUni(this string str) => GetBytes(str, UnicodeLE);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesUtf8(this string str) => GetBytes(str, UTF8);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesAscii(this ReadOnlySpan<char> str) => GetBytes(str, Encoding.ASCII);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesBigUni(this ReadOnlySpan<char> str) => GetBytes(str, Unicode);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesLittleUni(this ReadOnlySpan<char> str) => GetBytes(str, UnicodeLE);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] GetBytesUtf8(this ReadOnlySpan<char> str) => GetBytes(str, UTF8);
|
||||
|
||||
// Unlike the one built into the encoder, this avoids local init
|
||||
public static byte[] GetBytes(ReadOnlySpan<char> str, Encoding encoding)
|
||||
{
|
||||
if (str.Length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
|
||||
encoding.GetBytes(str, bytes);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// 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>();
|
||||
}
|
||||
|
||||
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
|
||||
encoding.GetBytes(str, 0, str.Length, bytes, 0);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesAscii(this string str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesAscii(this ReadOnlySpan<char> str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesBigUni(this string str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesBigUni(this ReadOnlySpan<char> str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesLittleUni(this string str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesLittleUni(this ReadOnlySpan<char> str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesUtf8(this string str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesUtf8(this ReadOnlySpan<char> str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetByteLengthForEncoding(Encoding encoding) =>
|
||||
encoding.BodyName switch
|
||||
{
|
||||
"utf-16BE" => 2,
|
||||
"utf-16" => 2,
|
||||
"utf-32BE" => 3,
|
||||
"utf-32" => 3,
|
||||
_ => 1
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsSafeChar(ushort c) => c >= 0x20 && c < 0xFFFE;
|
||||
|
||||
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
|
||||
{
|
||||
string s = encoding.GetString(span);
|
||||
|
||||
if (!safeString)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> chars = s.AsSpan();
|
||||
|
||||
using var sb = new ValueStringBuilder(stackalloc char[256]);
|
||||
var hasDoneAnyReplacements = false;
|
||||
|
||||
for (int i = 0, last = 0; i < chars.Length; i++)
|
||||
{
|
||||
if (!IsSafeChar(chars[i]) && i == chars.Length - 1)
|
||||
{
|
||||
hasDoneAnyReplacements = true;
|
||||
sb.Append(chars.Slice(last, i - last));
|
||||
last = i + 1; // Skip the unsafe char
|
||||
}
|
||||
}
|
||||
|
||||
return !hasDoneAnyReplacements ? s : sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,19 +7,15 @@ using System.Linq;
|
|||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using Microsoft.Toolkit.HighPerformance.Extensions;
|
||||
using Server.Buffers;
|
||||
using Server.Random;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public static class Utility
|
||||
{
|
||||
private static Encoding m_UTF8, m_UTF8WithEncoding, m_Unicode, m_UnicodeLE;
|
||||
|
||||
private static Dictionary<IPAddress, IPAddress> _ipAddressTable;
|
||||
|
||||
private static readonly SkillName[] m_AllSkills =
|
||||
|
|
@ -105,62 +101,6 @@ namespace Server
|
|||
|
||||
private static readonly Stack<ConsoleColor> m_ConsoleColors = new();
|
||||
|
||||
public static Encoding UTF8 => m_UTF8 ??= new UTF8Encoding(false, false);
|
||||
public static Encoding UTF8WithEncoding => m_UTF8WithEncoding ??= new UTF8Encoding(true, false);
|
||||
public static Encoding Unicode => m_Unicode ??= new UnicodeEncoding(true, false, false);
|
||||
public static Encoding UnicodeLE => m_UnicodeLE ??= new UnicodeEncoding(false, false, false);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetByteLengthForEncoding(Encoding encoding) =>
|
||||
encoding.BodyName switch
|
||||
{
|
||||
"utf-16BE" => 2,
|
||||
"utf-16" => 2,
|
||||
"utf-32BE" => 4,
|
||||
"utf-32" => 4,
|
||||
_ => 1
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfTerminator(ReadOnlySpan<byte> buffer, int sizeT) =>
|
||||
sizeT switch
|
||||
{
|
||||
2 => MemoryMarshal.Cast<byte, char>(buffer).IndexOf((char)0) * 2,
|
||||
4 => MemoryMarshal.Cast<byte, uint>(buffer).IndexOf((uint)0) * 4,
|
||||
_ => buffer.IndexOf((byte)0)
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsSafeChar(ushort c) => c >= 0x20 && c < 0xFFFE;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
|
||||
{
|
||||
string s = encoding.GetString(span);
|
||||
|
||||
if (!safeString)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> chars = s.AsSpan();
|
||||
|
||||
using ValueStringBuilder sb = new ValueStringBuilder(stackalloc char[256]);
|
||||
var hasDoneAnyReplacements = false;
|
||||
|
||||
for (int i = 0, last = 0; i < chars.Length; i++)
|
||||
{
|
||||
if (!IsSafeChar(chars[i]) && i == chars.Length - 1)
|
||||
{
|
||||
hasDoneAnyReplacements = true;
|
||||
sb.Append(chars.Slice(last, i - last));
|
||||
last = i + 1; // Skip the unsafe char
|
||||
}
|
||||
}
|
||||
|
||||
return !hasDoneAnyReplacements ? s : sb.ToString();
|
||||
}
|
||||
|
||||
public static void Separate(StringBuilder sb, string value, string separator)
|
||||
{
|
||||
if (sb.Length > 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue