diff --git a/Projects/Benchmarks/Benchmarks/Text/BenchmarkTextEncoding.cs b/Projects/Benchmarks/Benchmarks/Text/BenchmarkTextEncoding.cs new file mode 100644 index 000000000..5594bf937 --- /dev/null +++ b/Projects/Benchmarks/Benchmarks/Text/BenchmarkTextEncoding.cs @@ -0,0 +1,32 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Jobs; +using Server.Text; + +namespace Benchmarks.BenchmarkText +{ + [MemoryDiagnoser] + [SimpleJob(RuntimeMoniker.NetCoreApp50)] + public class BenchmarkTextEncoding + { + private const string text = + "This is supposed to be a really long text Ƞă¼ÖƄŭȘú😅¥♓ǵDƤĂ😋ġǁ⚕😐'ƿī😪_l" + + "This is supposed to be a really long text Ƞă¼ÖƄŭȘú😅¥♓ǵDƤĂ😋ġǁ⚕😐'ƿī😪_l" + + "This is supposed to be a really long text Ƞă¼ÖƄŭȘú😅¥♓ǵDƤĂ😋ġǁ⚕😐'ƿī😪_l" + + "This is supposed to be a really long text Ƞă¼ÖƄŭȘú😅¥♓ǵDƤĂ😋ġǁ⚕😐'ƿī😪_l" + + "This is supposed to be a really long text Ƞă¼ÖƄŭȘú😅¥♓ǵDƤĂ😋ġǁ⚕😐'ƿī😪_l"; + + [Benchmark] + public byte[] TestEncodingOldReturnBytes() + { + var bytes = TextEncoding.UTF8.GetBytes(text); + return bytes; + } + + [Benchmark] + public byte[] TestEncodingNewReturnBytes() + { + var bytes = text.GetBytesUtf8(); + return bytes; + } + } +} diff --git a/Projects/Benchmarks/Program.cs b/Projects/Benchmarks/Program.cs index b9fba96aa..19eab94b6 100644 --- a/Projects/Benchmarks/Program.cs +++ b/Projects/Benchmarks/Program.cs @@ -1,5 +1,5 @@ using BenchmarkDotNet.Running; -using Benchmarks.BenchmarkUtilities; +using Benchmarks.BenchmarkText; namespace Benchmarks { @@ -11,7 +11,8 @@ namespace Benchmarks // var packetConstruction = BenchmarkRunner.Run(); // var broadcast = BenchmarkRunner.Run(); // var stringHelpers = BenchmarkRunner.Run(); - var indexList = BenchmarkRunner.Run(); + // var indexList = BenchmarkRunner.Run(); + var textEncoding = BenchmarkRunner.Run(); } } } diff --git a/Projects/Server.Tests/Helpers/AssertExtensions.cs b/Projects/Server.Tests/Helpers/AssertExtensions.cs index 2782a6c05..19e1ceac7 100644 --- a/Projects/Server.Tests/Helpers/AssertExtensions.cs +++ b/Projects/Server.Tests/Helpers/AssertExtensions.cs @@ -1,4 +1,5 @@ using System; +using Server.Text; namespace Server.Tests { diff --git a/Projects/Server.Tests/Helpers/EncodingHelpers.cs b/Projects/Server.Tests/Helpers/EncodingHelpers.cs index e874da1ff..075206236 100644 --- a/Projects/Server.Tests/Helpers/EncodingHelpers.cs +++ b/Projects/Server.Tests/Helpers/EncodingHelpers.cs @@ -1,4 +1,5 @@ using System.Text; +using Server.Text; namespace Server.Tests { @@ -7,9 +8,9 @@ namespace Server.Tests public static Encoding GetEncoding(string bodyname) => bodyname switch { - "utf-8" => Utility.UTF8, - "utf-16" => Utility.UnicodeLE, - "utf-16BE" => Utility.Unicode, + "utf-8" => TextEncoding.UTF8, + "utf-16" => TextEncoding.UnicodeLE, + "utf-16BE" => TextEncoding.Unicode, _ => Encoding.ASCII }; } diff --git a/Projects/Server.Tests/Tests/Utility/HexStringConverterTest.cs b/Projects/Server.Tests/Tests/Utility/HexStringConverterTest.cs index 05d0d9ddd..b435a1d64 100644 --- a/Projects/Server.Tests/Tests/Utility/HexStringConverterTest.cs +++ b/Projects/Server.Tests/Tests/Utility/HexStringConverterTest.cs @@ -1,4 +1,5 @@ using System; +using Server.Text; using Xunit; namespace Server.Tests diff --git a/Projects/Server/Buffers/CircularBufferReader.cs b/Projects/Server/Buffers/CircularBufferReader.cs index 0bd6d5425..a33258c0b 100644 --- a/Projects/Server/Buffers/CircularBufferReader.cs +++ b/Projects/Server/Buffers/CircularBufferReader.cs @@ -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); diff --git a/Projects/Server/Buffers/CircularBufferWriter.cs b/Projects/Server/Buffers/CircularBufferWriter.cs index 0ec0c6889..252261753 100644 --- a/Projects/Server/Buffers/CircularBufferWriter.cs +++ b/Projects/Server/Buffers/CircularBufferWriter.cs @@ -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); } diff --git a/Projects/Server/Buffers/SpanReader.cs b/Projects/Server/Buffers/SpanReader.cs index b0f8757f8..374ebe86a 100644 --- a/Projects/Server/Buffers/SpanReader.cs +++ b/Projects/Server/Buffers/SpanReader.cs @@ -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); diff --git a/Projects/Server/Buffers/SpanWriter.cs b/Projects/Server/Buffers/SpanWriter.cs index 7ca8952e5..0f59dc0ff 100644 --- a/Projects/Server/Buffers/SpanWriter.cs +++ b/Projects/Server/Buffers/SpanWriter.cs @@ -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(value, Utility.UnicodeLE); + public void WriteLittleUni(string value) => WriteString(value, TextEncoding.UnicodeLE); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteLittleUniNull(string value) { - WriteString(value, Utility.UnicodeLE); + WriteString(value, TextEncoding.UnicodeLE); Write((ushort)0); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteLittleUni(string value, int fixedLength) => WriteString(value, Utility.UnicodeLE, fixedLength); + public void WriteLittleUni(string value, int fixedLength) => WriteString(value, TextEncoding.UnicodeLE, fixedLength); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteBigUni(string value) => WriteString(value, Utility.Unicode); + public void WriteBigUni(string value) => WriteString(value, TextEncoding.Unicode); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteBigUniNull(string value) { - WriteString(value, Utility.Unicode); + WriteString(value, TextEncoding.Unicode); Write((ushort)0); // '\0' } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void WriteBigUni(string value, int fixedLength) => WriteString(value, Utility.Unicode, fixedLength); + public void WriteBigUni(string value, int fixedLength) => WriteString(value, TextEncoding.Unicode, fixedLength); [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); // '\0' } diff --git a/Projects/Server/Gumps/Gump.cs b/Projects/Server/Gumps/Gump.cs index 0ac9698fc..8f45e531c 100644 --- a/Projects/Server/Gumps/Gump.cs +++ b/Projects/Server/Gumps/Gump.cs @@ -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) { diff --git a/Projects/Server/Json/JsonConfig.cs b/Projects/Server/Json/JsonConfig.cs index bcd7994ba..828779441 100644 --- a/Projects/Server/Json/JsonConfig.cs +++ b/Projects/Server/Json/JsonConfig.cs @@ -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(text, options ?? DefaultOptions); } diff --git a/Projects/Server/Serialization/BufferReader.cs b/Projects/Server/Serialization/BufferReader.cs index 8860c851c..59f8611d1 100644 --- a/Projects/Server/Serialization/BufferReader.cs +++ b/Projects/Server/Serialization/BufferReader.cs @@ -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) diff --git a/Projects/Server/Serialization/BufferWriter.cs b/Projects/Server/Serialization/BufferWriter.cs index 8e1ca729e..b6a8be01a 100644 --- a/Projects/Server/Serialization/BufferWriter.cs +++ b/Projects/Server/Serialization/BufferWriter.cs @@ -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(count < 1 ? BufferSize : count); } diff --git a/Projects/Server/Utilities/HexStringConverter.cs b/Projects/Server/Text/HexStringConverter.cs similarity index 97% rename from Projects/Server/Utilities/HexStringConverter.cs rename to Projects/Server/Text/HexStringConverter.cs index 5056581bf..5069d31c8 100644 --- a/Projects/Server/Utilities/HexStringConverter.cs +++ b/Projects/Server/Text/HexStringConverter.cs @@ -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) diff --git a/Projects/Server/Utilities/InsensitiveStringHelpers.cs b/Projects/Server/Text/InsensitiveStringHelpers.cs similarity index 100% rename from Projects/Server/Utilities/InsensitiveStringHelpers.cs rename to Projects/Server/Text/InsensitiveStringHelpers.cs diff --git a/Projects/Server/Utilities/OrdinalStringHelpers.cs b/Projects/Server/Text/OrdinalStringHelpers.cs similarity index 100% rename from Projects/Server/Utilities/OrdinalStringHelpers.cs rename to Projects/Server/Text/OrdinalStringHelpers.cs diff --git a/Projects/Server/Utilities/StringHelpers.cs b/Projects/Server/Text/StringHelpers.cs similarity index 94% rename from Projects/Server/Utilities/StringHelpers.cs rename to Projects/Server/Text/StringHelpers.cs index ef93584e4..624100d5e 100644 --- a/Projects/Server/Utilities/StringHelpers.cs +++ b/Projects/Server/Text/StringHelpers.cs @@ -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 buffer, int sizeT) => + sizeT switch + { + 2 => MemoryMarshal.Cast(buffer).IndexOf((char)0) * 2, + 4 => MemoryMarshal.Cast(buffer).IndexOf((uint)0) * 4, + _ => buffer.IndexOf((byte)0) + }; } } diff --git a/Projects/Server/Text/TextEncoding.cs b/Projects/Server/Text/TextEncoding.cs new file mode 100644 index 000000000..c84e01def --- /dev/null +++ b/Projects/Server/Text/TextEncoding.cs @@ -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 . * + *************************************************************************/ + +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 str) => GetBytes(str, Encoding.ASCII); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static byte[] GetBytesBigUni(this ReadOnlySpan str) => GetBytes(str, Unicode); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static byte[] GetBytesLittleUni(this ReadOnlySpan str) => GetBytes(str, UnicodeLE); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static byte[] GetBytesUtf8(this ReadOnlySpan str) => GetBytes(str, UTF8); + + // Unlike the one built into the encoder, this avoids local init + public static byte[] GetBytes(ReadOnlySpan str, Encoding encoding) + { + if (str.Length == 0) + { + return Array.Empty(); + } + + var bytes = GC.AllocateUninitializedArray(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(); + } + + var bytes = GC.AllocateUninitializedArray(encoding.GetByteCount(str)); + encoding.GetBytes(str, 0, str.Length, bytes, 0); + + return bytes; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesAscii(this string str, Span buffer) => Encoding.ASCII.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesAscii(this ReadOnlySpan str, Span buffer) => Encoding.ASCII.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesBigUni(this string str, Span buffer) => Unicode.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesBigUni(this ReadOnlySpan str, Span buffer) => Unicode.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesLittleUni(this string str, Span buffer) => UnicodeLE.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesLittleUni(this ReadOnlySpan str, Span buffer) => UnicodeLE.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesUtf8(this string str, Span buffer) => UTF8.GetBytes(str, buffer); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int GetBytesUtf8(this ReadOnlySpan str, Span 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 span, Encoding encoding, bool safeString = false) + { + string s = encoding.GetString(span); + + if (!safeString) + { + return s; + } + + ReadOnlySpan 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(); + } + } +} diff --git a/Projects/Server/Utilities/Utility.cs b/Projects/Server/Utilities/Utility.cs index 915149b1a..9b6a135ce 100644 --- a/Projects/Server/Utilities/Utility.cs +++ b/Projects/Server/Utilities/Utility.cs @@ -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 _ipAddressTable; private static readonly SkillName[] m_AllSkills = @@ -105,62 +101,6 @@ namespace Server private static readonly Stack 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 buffer, int sizeT) => - sizeT switch - { - 2 => MemoryMarshal.Cast(buffer).IndexOf((char)0) * 2, - 4 => MemoryMarshal.Cast(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 span, Encoding encoding, bool safeString = false) - { - string s = encoding.GetString(span); - - if (!safeString) - { - return s; - } - - ReadOnlySpan 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) diff --git a/Projects/UOContent/Accounting/Security/MD5PasswordProtection.cs b/Projects/UOContent/Accounting/Security/MD5PasswordProtection.cs index c593157ba..ba69522d3 100644 --- a/Projects/UOContent/Accounting/Security/MD5PasswordProtection.cs +++ b/Projects/UOContent/Accounting/Security/MD5PasswordProtection.cs @@ -15,7 +15,7 @@ using System; using System.Security.Cryptography; -using System.Text; +using Server.Text; namespace Server.Accounting.Security { @@ -28,10 +28,7 @@ namespace Server.Accounting.Security public string EncryptPassword(string plainPassword) { - var password = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)); - var bytes = GC.AllocateUninitializedArray(Encoding.ASCII.GetByteCount(password)); - Encoding.ASCII.GetBytes(password, bytes); - + byte[] bytes = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)).GetBytesAscii(); return m_MD5HashProvider.ComputeHash(bytes).ToHexString(); } diff --git a/Projects/UOContent/Accounting/Security/PBKDF2PasswordProtection.cs b/Projects/UOContent/Accounting/Security/PBKDF2PasswordProtection.cs index ee8959a03..3fd3d7685 100644 --- a/Projects/UOContent/Accounting/Security/PBKDF2PasswordProtection.cs +++ b/Projects/UOContent/Accounting/Security/PBKDF2PasswordProtection.cs @@ -16,6 +16,7 @@ using System; using System.Buffers.Binary; using System.Security.Cryptography; +using Server.Text; namespace Server.Accounting.Security { diff --git a/Projects/UOContent/Accounting/Security/SHA1PasswordProtection.cs b/Projects/UOContent/Accounting/Security/SHA1PasswordProtection.cs index a21730fb3..fb2fa0ea5 100644 --- a/Projects/UOContent/Accounting/Security/SHA1PasswordProtection.cs +++ b/Projects/UOContent/Accounting/Security/SHA1PasswordProtection.cs @@ -15,7 +15,7 @@ using System; using System.Security.Cryptography; -using System.Text; +using Server.Text; namespace Server.Accounting.Security { @@ -28,10 +28,7 @@ namespace Server.Accounting.Security public string EncryptPassword(string plainPassword) { - var password = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)); - var bytes = GC.AllocateUninitializedArray(Encoding.ASCII.GetByteCount(password)); - Encoding.ASCII.GetBytes(password, bytes); - + byte[] bytes = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)).GetBytesAscii(); return m_SHA1HashProvider.ComputeHash(bytes).ToHexString(); } diff --git a/Projects/UOContent/Accounting/Security/SHA2PasswordProtection.cs b/Projects/UOContent/Accounting/Security/SHA2PasswordProtection.cs index 5c53dc68c..a7f5cedb2 100644 --- a/Projects/UOContent/Accounting/Security/SHA2PasswordProtection.cs +++ b/Projects/UOContent/Accounting/Security/SHA2PasswordProtection.cs @@ -16,6 +16,7 @@ using System; using System.Security.Cryptography; using System.Text; +using Server.Text; namespace Server.Accounting.Security { @@ -26,10 +27,7 @@ namespace Server.Accounting.Security public string EncryptPassword(string plainPassword) { - var password = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)); - var bytes = GC.AllocateUninitializedArray(Encoding.ASCII.GetByteCount(password)); - Encoding.ASCII.GetBytes(password, bytes); - + byte[] bytes = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)).GetBytesAscii(); return m_SHA2HashProvider.ComputeHash(bytes).ToHexString(); } diff --git a/Projects/UOContent/Items/Books/BaseBook.cs b/Projects/UOContent/Items/Books/BaseBook.cs index a571d3e2a..b6f661b79 100644 --- a/Projects/UOContent/Items/Books/BaseBook.cs +++ b/Projects/UOContent/Items/Books/BaseBook.cs @@ -6,6 +6,7 @@ using Server.ContextMenus; using Server.Gumps; using Server.Multis; using Server.Network; +using Server.Text; namespace Server.Items { @@ -501,7 +502,7 @@ namespace Server.Items for (var j = 0; j < page.Lines.Length; ++j) { - var buffer = Utility.UTF8.GetBytes(page.Lines[j]); + var buffer = page.Lines[j].GetBytesUtf8(); Stream.Write(buffer, 0, buffer.Length); Stream.Write((byte)0); @@ -517,8 +518,8 @@ namespace Server.Items var title = book.Title ?? ""; var author = book.Author ?? ""; - var titleBuffer = Utility.UTF8.GetBytes(title); - var authorBuffer = Utility.UTF8.GetBytes(author); + var titleBuffer = title.GetBytesUtf8(); + var authorBuffer = author.GetBytesUtf8(); EnsureCapacity(15 + titleBuffer.Length + authorBuffer.Length); diff --git a/Projects/UOContent/Items/Misc/BulletinBoards.cs b/Projects/UOContent/Items/Misc/BulletinBoards.cs index 00ab7a014..cf91baba3 100644 --- a/Projects/UOContent/Items/Misc/BulletinBoards.cs +++ b/Projects/UOContent/Items/Misc/BulletinBoards.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using Server.Network; using Server.Targeting; +using Server.Text; namespace Server.Items { @@ -517,7 +518,7 @@ namespace Server.Items { EnsureCapacity(38); - var buffer = Utility.UTF8.GetBytes(board.BoardName ?? ""); + var buffer = TextEncoding.UTF8.GetBytes(board.BoardName ?? ""); Stream.Write((byte)0x00); // PacketID Stream.Write(board.Serial); // Bulletin board serial @@ -568,7 +569,7 @@ namespace Server.Items public void WriteString(string v) { - var buffer = Utility.UTF8.GetBytes(v); + var buffer = TextEncoding.UTF8.GetBytes(v); var len = buffer.Length + 1; if (len > 255) @@ -644,7 +645,7 @@ namespace Server.Items public void WriteString(string v, bool padding) { - var buffer = Utility.UTF8.GetBytes(v); + var buffer = TextEncoding.UTF8.GetBytes(v); var tail = padding ? 2 : 1; var len = buffer.Length + tail;