fix(core): Streamlines text encoding (#407)

- [X] Streamlines text encoding
This commit is contained in:
Kamron Batman 2021-01-13 18:56:32 -08:00 committed by GitHub
parent 5631c1da6d
commit 211c2ba8ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 274 additions and 137 deletions

View file

@ -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;
}
}
}

View file

@ -1,5 +1,5 @@
using BenchmarkDotNet.Running; using BenchmarkDotNet.Running;
using Benchmarks.BenchmarkUtilities; using Benchmarks.BenchmarkText;
namespace Benchmarks namespace Benchmarks
{ {
@ -11,7 +11,8 @@ namespace Benchmarks
// var packetConstruction = BenchmarkRunner.Run<BenchmarkPacketConstruction>(); // var packetConstruction = BenchmarkRunner.Run<BenchmarkPacketConstruction>();
// var broadcast = BenchmarkRunner.Run<BenchmarkPacketBroadcast>(); // var broadcast = BenchmarkRunner.Run<BenchmarkPacketBroadcast>();
// var stringHelpers = BenchmarkRunner.Run<BenchmarkStringHelpers>(); // var stringHelpers = BenchmarkRunner.Run<BenchmarkStringHelpers>();
var indexList = BenchmarkRunner.Run<BenchmarkOrderedHashSet>(); // var indexList = BenchmarkRunner.Run<BenchmarkOrderedHashSet>();
var textEncoding = BenchmarkRunner.Run<BenchmarkTextEncoding>();
} }
} }
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using Server.Text;
namespace Server.Tests namespace Server.Tests
{ {

View file

@ -1,4 +1,5 @@
using System.Text; using System.Text;
using Server.Text;
namespace Server.Tests namespace Server.Tests
{ {
@ -7,9 +8,9 @@ namespace Server.Tests
public static Encoding GetEncoding(string bodyname) => public static Encoding GetEncoding(string bodyname) =>
bodyname switch bodyname switch
{ {
"utf-8" => Utility.UTF8, "utf-8" => TextEncoding.UTF8,
"utf-16" => Utility.UnicodeLE, "utf-16" => TextEncoding.UnicodeLE,
"utf-16BE" => Utility.Unicode, "utf-16BE" => TextEncoding.Unicode,
_ => Encoding.ASCII _ => Encoding.ASCII
}; };
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using Server.Text;
using Xunit; using Xunit;
namespace Server.Tests namespace Server.Tests

View file

@ -19,6 +19,7 @@ using System.Buffers.Binary;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Server.Text;
namespace Server.Network namespace Server.Network
{ {
@ -243,7 +244,7 @@ namespace Server.Network
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1) 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; bool isFixedLength = fixedLength > -1;
@ -271,7 +272,7 @@ namespace Server.Network
var firstLength = Math.Min(_first.Length - Position, size); var firstLength = Math.Min(_first.Length - Position, size);
// Find terminator // Find terminator
index = Utility.IndexOfTerminator(_first.Slice(Position, firstLength), sizeT); index = _first.Slice(Position, firstLength).IndexOfTerminator(sizeT);
if (index < 0) if (index < 0)
{ {
@ -283,7 +284,7 @@ namespace Server.Network
} }
else else
{ {
index = Utility.IndexOfTerminator(_second.Slice(0, remaining), sizeT); index = _second.Slice(0, remaining).IndexOfTerminator(sizeT);
int secondLength = index < 0 ? remaining : index; int secondLength = index < 0 ? remaining : index;
int length = firstLength + secondLength; int length = firstLength + secondLength;
@ -294,7 +295,7 @@ namespace Server.Network
_second.Slice(0, secondLength).CopyTo(bytes.Slice(firstLength)); _second.Slice(0, secondLength).CopyTo(bytes.Slice(firstLength));
Position += length + (index >= 0 ? sizeT : 0); 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); size = Math.Min(remaining, size);
span = _second.Slice( Position - _first.Length, size); span = _second.Slice( Position - _first.Length, size);
index = Utility.IndexOfTerminator(span, sizeT); index = span.IndexOfTerminator(sizeT);
if (index >= 0) if (index >= 0)
{ {
@ -317,41 +318,41 @@ namespace Server.Network
} }
Position += isFixedLength ? size : index + sizeT; Position += isFixedLength ? size : index + sizeT;
return Utility.GetString(span, encoding, safeString); return TextEncoding.GetString(span, encoding, safeString);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadLittleUniSafe() => ReadString(Utility.UnicodeLE, true); public string ReadLittleUniSafe() => ReadString(TextEncoding.UnicodeLE, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadLittleUni() => ReadString(Utility.UnicodeLE); public string ReadLittleUni() => ReadString(TextEncoding.UnicodeLE);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadBigUniSafe() => ReadString(Utility.Unicode, true); public string ReadBigUniSafe() => ReadString(TextEncoding.Unicode, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadBigUni() => ReadString(Utility.Unicode); public string ReadBigUni() => ReadString(TextEncoding.Unicode);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUTF8Safe() => ReadString(Utility.UTF8, true); public string ReadUTF8Safe() => ReadString(TextEncoding.UTF8, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUTF8() => ReadString(Utility.UTF8); public string ReadUTF8() => ReadString(TextEncoding.UTF8);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadAsciiSafe(int fixedLength) => ReadString(Encoding.ASCII, true, fixedLength); public string ReadAsciiSafe(int fixedLength) => ReadString(Encoding.ASCII, true, fixedLength);

View file

@ -18,6 +18,7 @@ using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Server; using Server;
using Server.Text;
namespace System.Buffers namespace System.Buffers
{ {
@ -418,7 +419,7 @@ namespace System.Buffers
{ {
if (fixedLength < 0) { fixedLength = value.Length; } 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; var count = fixedLength - value.Length;
if (count > 0) if (count > 0)
{ {
@ -429,7 +430,7 @@ namespace System.Buffers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLittleUniNull(string value) public void WriteLittleUniNull(string value)
{ {
WriteString(value, Utility.UnicodeLE); WriteString(value, TextEncoding.UnicodeLE);
Write((ushort)0); Write((ushort)0);
} }
@ -438,7 +439,7 @@ namespace System.Buffers
{ {
if (fixedLength < 0) { fixedLength = value.Length; } 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; var count = fixedLength - value.Length;
if (count > 0) if (count > 0)
{ {
@ -449,17 +450,17 @@ namespace System.Buffers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteBigUniNull(string value) public void WriteBigUniNull(string value)
{ {
WriteString(value, Utility.Unicode); WriteString(value, TextEncoding.Unicode);
Write((ushort)0); Write((ushort)0);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteUTF8(string value) => WriteString(value, Utility.UTF8); public void WriteUTF8(string value) => WriteString(value, TextEncoding.UTF8);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteUTF8Null(string value) public void WriteUTF8Null(string value)
{ {
WriteString(value, Utility.UTF8); WriteString(value, TextEncoding.UTF8);
Write((byte)0); Write((byte)0);
} }

View file

@ -19,6 +19,7 @@ using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Server; using Server;
using Server.Text;
namespace System.Buffers namespace System.Buffers
{ {
@ -141,7 +142,7 @@ namespace System.Buffers
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1) 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; bool isFixedLength = fixedLength > -1;
@ -161,44 +162,44 @@ namespace System.Buffers
size = remaining - (remaining & (sizeT - 1)); 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; 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)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadLittleUniSafe() => ReadString(Utility.UnicodeLE, true); public string ReadLittleUniSafe() => ReadString(TextEncoding.UnicodeLE, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadLittleUni() => ReadString(Utility.UnicodeLE); public string ReadLittleUni() => ReadString(TextEncoding.UnicodeLE);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadBigUniSafe() => ReadString(Utility.Unicode, true); public string ReadBigUniSafe() => ReadString(TextEncoding.Unicode, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadBigUni() => ReadString(Utility.Unicode); public string ReadBigUni() => ReadString(TextEncoding.Unicode);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUTF8Safe() => ReadString(Utility.UTF8, true); public string ReadUTF8Safe() => ReadString(TextEncoding.UTF8, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadUTF8() => ReadString(Utility.UTF8); public string ReadUTF8() => ReadString(TextEncoding.UTF8);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadAsciiSafe(int fixedLength) => ReadString(Encoding.ASCII, true, fixedLength); public string ReadAsciiSafe(int fixedLength) => ReadString(Encoding.ASCII, true, fixedLength);

View file

@ -21,6 +21,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using Server; using Server;
using Server.Text;
namespace System.Buffers namespace System.Buffers
{ {
@ -264,38 +265,38 @@ namespace System.Buffers
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteLittleUniNull(string value) public void WriteLittleUniNull(string value)
{ {
WriteString<char>(value, Utility.UnicodeLE); WriteString<char>(value, TextEncoding.UnicodeLE);
Write((ushort)0); Write((ushort)0);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteBigUniNull(string value) public void WriteBigUniNull(string value)
{ {
WriteString<char>(value, Utility.Unicode); WriteString<char>(value, TextEncoding.Unicode);
Write((ushort)0); // '\0' Write((ushort)0); // '\0'
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [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)] [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)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteUTF8Null(string value) public void WriteUTF8Null(string value)
{ {
WriteString<byte>(value, Utility.UTF8); WriteString<byte>(value, TextEncoding.UTF8);
Write((byte)0); // '\0' Write((byte)0); // '\0'
} }

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Server.Network; using Server.Network;
using Server.Text;
namespace Server.Gumps namespace Server.Gumps
{ {
@ -248,7 +249,7 @@ namespace Server.Gumps
state.SendDisplayGump(this, out m_Switches, out m_TextEntries); 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) public virtual void OnResponse(NetState sender, RelayInfo info)
{ {

View file

@ -19,6 +19,7 @@ using System.IO;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using Server.Text;
namespace Server.Json namespace Server.Json
{ {
@ -62,7 +63,7 @@ namespace Server.Json
return default; return default;
} }
var text = File.ReadAllText(filePath, Utility.UTF8); var text = File.ReadAllText(filePath, TextEncoding.UTF8);
return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions); return JsonSerializer.Deserialize<T>(text, options ?? DefaultOptions);
} }

View file

@ -21,6 +21,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Text; using System.Text;
using Server.Guilds; using Server.Guilds;
using Server.Text;
namespace Server namespace Server
{ {
@ -37,7 +38,7 @@ namespace Server
public BufferReader(byte[] buffer) public BufferReader(byte[] buffer)
{ {
_buffer = buffer; _buffer = buffer;
_encoding = Utility.UTF8; _encoding = TextEncoding.UTF8;
} }
public void SwapBuffers(byte[] newBuffer, out byte[] oldBuffer) public void SwapBuffers(byte[] newBuffer, out byte[] oldBuffer)

View file

@ -20,6 +20,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Server.Text;
namespace Server namespace Server
{ {
@ -56,7 +57,7 @@ namespace Server
public BufferWriter(byte[] buffer, bool prefixStr) public BufferWriter(byte[] buffer, bool prefixStr)
{ {
m_PrefixStrings = prefixStr; m_PrefixStrings = prefixStr;
m_Encoding = Utility.UTF8; m_Encoding = TextEncoding.UTF8;
_buffer = buffer; _buffer = buffer;
} }
@ -67,7 +68,7 @@ namespace Server
public BufferWriter(int count, bool prefixStr) public BufferWriter(int count, bool prefixStr)
{ {
m_PrefixStrings = prefixStr; m_PrefixStrings = prefixStr;
m_Encoding = Utility.UTF8; m_Encoding = TextEncoding.UTF8;
_buffer = GC.AllocateUninitializedArray<byte>(count < 1 ? BufferSize : count); _buffer = GC.AllocateUninitializedArray<byte>(count < 1 ? BufferSize : count);
} }

View file

@ -15,7 +15,7 @@
using System; using System;
namespace Server namespace Server.Text
{ {
public static class HexStringConverter public static class HexStringConverter
{ {
@ -66,7 +66,7 @@ namespace Server
const uint delimiter = 0x20002C; // ", " const uint delimiter = 0x20002C; // ", "
const char openBracket = '['; const char openBracket = '[';
const char closeBracket = ']'; 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); var result = new string((char)0, length);
fixed (char* resultP = result) fixed (char* resultP = result)

View file

@ -17,6 +17,7 @@ using System;
using System.Buffers; using System.Buffers;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Server namespace Server
{ {
@ -240,5 +241,14 @@ namespace Server
return list; 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)
};
} }
} }

View 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();
}
}
}

View file

@ -7,19 +7,15 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Xml; using System.Xml;
using Microsoft.Toolkit.HighPerformance.Extensions; using Microsoft.Toolkit.HighPerformance.Extensions;
using Server.Buffers;
using Server.Random; using Server.Random;
namespace Server namespace Server
{ {
public static class Utility public static class Utility
{ {
private static Encoding m_UTF8, m_UTF8WithEncoding, m_Unicode, m_UnicodeLE;
private static Dictionary<IPAddress, IPAddress> _ipAddressTable; private static Dictionary<IPAddress, IPAddress> _ipAddressTable;
private static readonly SkillName[] m_AllSkills = private static readonly SkillName[] m_AllSkills =
@ -105,62 +101,6 @@ namespace Server
private static readonly Stack<ConsoleColor> m_ConsoleColors = new(); 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) public static void Separate(StringBuilder sb, string value, string separator)
{ {
if (sb.Length > 0) if (sb.Length > 0)

View file

@ -15,7 +15,7 @@
using System; using System;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using Server.Text;
namespace Server.Accounting.Security namespace Server.Accounting.Security
{ {
@ -28,10 +28,7 @@ namespace Server.Accounting.Security
public string EncryptPassword(string plainPassword) public string EncryptPassword(string plainPassword)
{ {
var password = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)); byte[] bytes = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)).GetBytesAscii();
var bytes = GC.AllocateUninitializedArray<byte>(Encoding.ASCII.GetByteCount(password));
Encoding.ASCII.GetBytes(password, bytes);
return m_MD5HashProvider.ComputeHash(bytes).ToHexString(); return m_MD5HashProvider.ComputeHash(bytes).ToHexString();
} }

View file

@ -16,6 +16,7 @@
using System; using System;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Security.Cryptography; using System.Security.Cryptography;
using Server.Text;
namespace Server.Accounting.Security namespace Server.Accounting.Security
{ {

View file

@ -15,7 +15,7 @@
using System; using System;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using Server.Text;
namespace Server.Accounting.Security namespace Server.Accounting.Security
{ {
@ -28,10 +28,7 @@ namespace Server.Accounting.Security
public string EncryptPassword(string plainPassword) public string EncryptPassword(string plainPassword)
{ {
var password = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)); byte[] bytes = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)).GetBytesAscii();
var bytes = GC.AllocateUninitializedArray<byte>(Encoding.ASCII.GetByteCount(password));
Encoding.ASCII.GetBytes(password, bytes);
return m_SHA1HashProvider.ComputeHash(bytes).ToHexString(); return m_SHA1HashProvider.ComputeHash(bytes).ToHexString();
} }

View file

@ -16,6 +16,7 @@
using System; using System;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using Server.Text;
namespace Server.Accounting.Security namespace Server.Accounting.Security
{ {
@ -26,10 +27,7 @@ namespace Server.Accounting.Security
public string EncryptPassword(string plainPassword) public string EncryptPassword(string plainPassword)
{ {
var password = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)); byte[] bytes = plainPassword.AsSpan(0, Math.Min(256, plainPassword.Length)).GetBytesAscii();
var bytes = GC.AllocateUninitializedArray<byte>(Encoding.ASCII.GetByteCount(password));
Encoding.ASCII.GetBytes(password, bytes);
return m_SHA2HashProvider.ComputeHash(bytes).ToHexString(); return m_SHA2HashProvider.ComputeHash(bytes).ToHexString();
} }

View file

@ -6,6 +6,7 @@ using Server.ContextMenus;
using Server.Gumps; using Server.Gumps;
using Server.Multis; using Server.Multis;
using Server.Network; using Server.Network;
using Server.Text;
namespace Server.Items namespace Server.Items
{ {
@ -501,7 +502,7 @@ namespace Server.Items
for (var j = 0; j < page.Lines.Length; ++j) 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(buffer, 0, buffer.Length);
Stream.Write((byte)0); Stream.Write((byte)0);
@ -517,8 +518,8 @@ namespace Server.Items
var title = book.Title ?? ""; var title = book.Title ?? "";
var author = book.Author ?? ""; var author = book.Author ?? "";
var titleBuffer = Utility.UTF8.GetBytes(title); var titleBuffer = title.GetBytesUtf8();
var authorBuffer = Utility.UTF8.GetBytes(author); var authorBuffer = author.GetBytesUtf8();
EnsureCapacity(15 + titleBuffer.Length + authorBuffer.Length); EnsureCapacity(15 + titleBuffer.Length + authorBuffer.Length);

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Server.Network; using Server.Network;
using Server.Targeting; using Server.Targeting;
using Server.Text;
namespace Server.Items namespace Server.Items
{ {
@ -517,7 +518,7 @@ namespace Server.Items
{ {
EnsureCapacity(38); EnsureCapacity(38);
var buffer = Utility.UTF8.GetBytes(board.BoardName ?? ""); var buffer = TextEncoding.UTF8.GetBytes(board.BoardName ?? "");
Stream.Write((byte)0x00); // PacketID Stream.Write((byte)0x00); // PacketID
Stream.Write(board.Serial); // Bulletin board serial Stream.Write(board.Serial); // Bulletin board serial
@ -568,7 +569,7 @@ namespace Server.Items
public void WriteString(string v) public void WriteString(string v)
{ {
var buffer = Utility.UTF8.GetBytes(v); var buffer = TextEncoding.UTF8.GetBytes(v);
var len = buffer.Length + 1; var len = buffer.Length + 1;
if (len > 255) if (len > 255)
@ -644,7 +645,7 @@ namespace Server.Items
public void WriteString(string v, bool padding) 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 tail = padding ? 2 : 1;
var len = buffer.Length + tail; var len = buffer.Length + tail;