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
32
Projects/Benchmarks/Benchmarks/Text/BenchmarkTextEncoding.cs
Normal file
32
Projects/Benchmarks/Benchmarks/Text/BenchmarkTextEncoding.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<BenchmarkPacketConstruction>();
|
||||
// var broadcast = BenchmarkRunner.Run<BenchmarkPacketBroadcast>();
|
||||
// var stringHelpers = BenchmarkRunner.Run<BenchmarkStringHelpers>();
|
||||
var indexList = BenchmarkRunner.Run<BenchmarkOrderedHashSet>();
|
||||
// var indexList = BenchmarkRunner.Run<BenchmarkOrderedHashSet>();
|
||||
var textEncoding = BenchmarkRunner.Run<BenchmarkTextEncoding>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Tests
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using Server.Text;
|
||||
using Xunit;
|
||||
|
||||
namespace Server.Tests
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<byte>(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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Security.Cryptography;
|
||||
using Server.Text;
|
||||
|
||||
namespace Server.Accounting.Security
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<byte>(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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<byte>(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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue