fix: Cleans up core code (#1187)
**Only one functional change** * Fixes a bug in LogFactory where `Warning` is being logged as `Information` Non-functional changes: * Updates/Fixes copyright headers * Removes namespace scopes for core files. View with [whitespace off](https://github.com/modernuo/ModernUO/pull/1187/files?w=1).
This commit is contained in:
parent
0138d40bda
commit
f268d5d4e2
262 changed files with 28527 additions and 28646 deletions
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: HexStringConverter.cs *
|
||||
* *
|
||||
|
|
@ -15,119 +15,118 @@
|
|||
|
||||
using System;
|
||||
|
||||
namespace Server.Text
|
||||
{
|
||||
public static class HexStringConverter
|
||||
{
|
||||
public static readonly uint[] m_Lookup32Chars = CreateLookup32Chars();
|
||||
namespace Server.Text;
|
||||
|
||||
private static uint[] CreateLookup32Chars()
|
||||
public static class HexStringConverter
|
||||
{
|
||||
public static readonly uint[] m_Lookup32Chars = CreateLookup32Chars();
|
||||
|
||||
private static uint[] CreateLookup32Chars()
|
||||
{
|
||||
var result = new uint[256];
|
||||
for (var i = 0; i < 256; i++)
|
||||
{
|
||||
var result = new uint[256];
|
||||
for (var i = 0; i < 256; i++)
|
||||
var s = i.ToString("X2");
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
var s = i.ToString("X2");
|
||||
result[i] = s[0] + ((uint)s[1] << 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
result[i] = s[1] + ((uint)s[0] << 16);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ToHexString(this byte[] bytes) => new ReadOnlySpan<byte>(bytes).ToHexString();
|
||||
|
||||
public static string ToHexString(this Span<byte> bytes) => ((ReadOnlySpan<byte>)bytes).ToHexString();
|
||||
|
||||
public static unsafe string ToHexString(this ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var result = new string((char)0, bytes.Length * 2);
|
||||
fixed (char* resultP = result)
|
||||
{
|
||||
var resultP2 = (uint*)resultP;
|
||||
for (var i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
resultP2[i] = m_Lookup32Chars[bytes[i]];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static unsafe int ToSpacedHexString(this ReadOnlySpan<byte> bytes, Span<char> result)
|
||||
{
|
||||
var charsWritten = 0;
|
||||
fixed (char* resultP = result)
|
||||
{
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
var resultP2 = (uint*)(resultP + charsWritten);
|
||||
*resultP2 = m_Lookup32Chars[bytes[i]];
|
||||
charsWritten += 2;
|
||||
|
||||
if (i < bytes.Length - 1)
|
||||
{
|
||||
*(resultP + charsWritten++) = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return charsWritten;
|
||||
}
|
||||
|
||||
public static string ToDelimitedHexString(this byte[] bytes) => ((ReadOnlySpan<byte>)bytes).ToDelimitedHexString();
|
||||
|
||||
public static unsafe string ToDelimitedHexString(this ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
const uint delimiter = 0x20002C; // ", "
|
||||
const char openBracket = '[';
|
||||
const char closeBracket = ']';
|
||||
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)
|
||||
{
|
||||
resultP[0] = openBracket;
|
||||
resultP[length - 1] = closeBracket;
|
||||
|
||||
var resultP2 = (uint*)(resultP + 1);
|
||||
for (int a = 0, i = 0; a < bytes.Length; a++, i++)
|
||||
{
|
||||
if (a > 0)
|
||||
{
|
||||
resultP2[i++] = delimiter;
|
||||
}
|
||||
|
||||
resultP2[i] = m_Lookup32Chars[bytes[a]];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static unsafe void GetBytes(this string str, Span<byte> bytes)
|
||||
{
|
||||
fixed (char* strP = str)
|
||||
{
|
||||
var i = 0;
|
||||
var j = 0;
|
||||
while (i < str.Length)
|
||||
{
|
||||
int chr1 = strP[i++];
|
||||
int chr2 = strP[i++];
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
result[i] = s[0] + ((uint)s[1] << 16);
|
||||
bytes[j++] = (byte)(((chr1 - (chr1 >= 65 ? 55 : 48)) << 4) | (chr2 - (chr2 >= 65 ? 55 : 48)));
|
||||
}
|
||||
else
|
||||
{
|
||||
result[i] = s[1] + ((uint)s[0] << 16);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ToHexString(this byte[] bytes) => new ReadOnlySpan<byte>(bytes).ToHexString();
|
||||
|
||||
public static string ToHexString(this Span<byte> bytes) => ((ReadOnlySpan<byte>)bytes).ToHexString();
|
||||
|
||||
public static unsafe string ToHexString(this ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var result = new string((char)0, bytes.Length * 2);
|
||||
fixed (char* resultP = result)
|
||||
{
|
||||
var resultP2 = (uint*)resultP;
|
||||
for (var i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
resultP2[i] = m_Lookup32Chars[bytes[i]];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static unsafe int ToSpacedHexString(this ReadOnlySpan<byte> bytes, Span<char> result)
|
||||
{
|
||||
var charsWritten = 0;
|
||||
fixed (char* resultP = result)
|
||||
{
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
var resultP2 = (uint*)(resultP + charsWritten);
|
||||
*resultP2 = m_Lookup32Chars[bytes[i]];
|
||||
charsWritten += 2;
|
||||
|
||||
if (i < bytes.Length - 1)
|
||||
{
|
||||
*(resultP + charsWritten++) = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return charsWritten;
|
||||
}
|
||||
|
||||
public static string ToDelimitedHexString(this byte[] bytes) => ((ReadOnlySpan<byte>)bytes).ToDelimitedHexString();
|
||||
|
||||
public static unsafe string ToDelimitedHexString(this ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
const uint delimiter = 0x20002C; // ", "
|
||||
const char openBracket = '[';
|
||||
const char closeBracket = ']';
|
||||
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)
|
||||
{
|
||||
resultP[0] = openBracket;
|
||||
resultP[length - 1] = closeBracket;
|
||||
|
||||
var resultP2 = (uint*)(resultP + 1);
|
||||
for (int a = 0, i = 0; a < bytes.Length; a++, i++)
|
||||
{
|
||||
if (a > 0)
|
||||
{
|
||||
resultP2[i++] = delimiter;
|
||||
}
|
||||
|
||||
resultP2[i] = m_Lookup32Chars[bytes[a]];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static unsafe void GetBytes(this string str, Span<byte> bytes)
|
||||
{
|
||||
fixed (char* strP = str)
|
||||
{
|
||||
var i = 0;
|
||||
var j = 0;
|
||||
while (i < str.Length)
|
||||
{
|
||||
int chr1 = strP[i++];
|
||||
int chr2 = strP[i++];
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
bytes[j++] = (byte)(((chr1 - (chr1 >= 65 ? 55 : 48)) << 4) | (chr2 - (chr2 >= 65 ? 55 : 48)));
|
||||
}
|
||||
else
|
||||
{
|
||||
bytes[j++] = (byte)((chr1 - (chr1 >= 65 ? 55 : 48)) | ((chr2 - (chr2 >= 65 ? 55 : 48)) << 4));
|
||||
}
|
||||
bytes[j++] = (byte)((chr1 - (chr1 >= 65 ? 55 : 48)) | ((chr2 - (chr2 >= 65 ? 55 : 48)) << 4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: InsensitiveStringHelpers.cs *
|
||||
* *
|
||||
|
|
@ -16,87 +16,86 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
namespace Server;
|
||||
|
||||
public static class InsensitiveStringHelpers
|
||||
{
|
||||
public static class InsensitiveStringHelpers
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveCompare(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.CompareTo(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveCompare(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.CompareTo(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveCompare(this string a, string b) => StringComparer.Ordinal.Compare(a, b);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveCompare(this string a, string b) => StringComparer.Ordinal.Compare(a, b);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEquals(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Equals(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEquals(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Equals(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEquals(this string a, string b) =>
|
||||
a?.Equals(b, StringComparison.OrdinalIgnoreCase) ?? b == null;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEquals(this string a, string b) =>
|
||||
a?.Equals(b, StringComparison.OrdinalIgnoreCase) ?? b == null;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveStartsWith(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.StartsWith(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveStartsWith(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.StartsWith(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveStartsWith(this string a, string b) =>
|
||||
a?.StartsWith(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveStartsWith(this string a, string b) =>
|
||||
a?.StartsWith(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEndsWith(this string a, string b) =>
|
||||
a?.EndsWith(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEndsWith(this string a, string b) =>
|
||||
a?.EndsWith(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEndsWith(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.EndsWith(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveEndsWith(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.EndsWith(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Contains(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Contains(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this string a, string b) =>
|
||||
a?.Contains(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this string a, string b) =>
|
||||
a?.Contains(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Contains(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Contains(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this string a, char b) =>
|
||||
a?.Contains(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool InsensitiveContains(this string a, char b) =>
|
||||
a?.Contains(b, StringComparison.OrdinalIgnoreCase) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this string a, char b) =>
|
||||
a?.IndexOf(b, StringComparison.OrdinalIgnoreCase) ?? -1;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this string a, char b) =>
|
||||
a?.IndexOf(b, StringComparison.OrdinalIgnoreCase) ?? -1;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this string a, string b) =>
|
||||
a?.IndexOf(b, StringComparison.OrdinalIgnoreCase) ?? -1;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this string a, string b) =>
|
||||
a?.IndexOf(b, StringComparison.OrdinalIgnoreCase) ?? -1;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this string a, string b, int startIndex) =>
|
||||
a?.IndexOf(b, startIndex, StringComparison.OrdinalIgnoreCase) ?? -1;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this string a, string b, int startIndex) =>
|
||||
a?.IndexOf(b, startIndex, StringComparison.OrdinalIgnoreCase) ?? -1;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.IndexOf(b, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int InsensitiveIndexOf(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.IndexOf(b, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string InsensitiveReplace(this string a, string o, string n) =>
|
||||
a?.Replace(o, n, StringComparison.OrdinalIgnoreCase);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string InsensitiveReplace(this string a, string o, string n) =>
|
||||
a?.Replace(o, n, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void InsensitiveRemove(
|
||||
this ReadOnlySpan<char> a,
|
||||
ReadOnlySpan<char> b,
|
||||
Span<char> buffer,
|
||||
out int size
|
||||
) => a.Remove(b, StringComparison.OrdinalIgnoreCase, buffer, out size);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void InsensitiveRemove(
|
||||
this ReadOnlySpan<char> a,
|
||||
ReadOnlySpan<char> b,
|
||||
Span<char> buffer,
|
||||
out int size
|
||||
) => a.Remove(b, StringComparison.OrdinalIgnoreCase, buffer, out size);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string InsensitiveRemove(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Remove(b, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string InsensitiveRemove(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Remove(b, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2020 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: OrdinalStringHelpers.cs *
|
||||
* *
|
||||
|
|
@ -16,85 +16,84 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
namespace Server;
|
||||
|
||||
public static class OrdinalStringHelpers
|
||||
{
|
||||
public static class OrdinalStringHelpers
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CompareOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.CompareTo(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CompareOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.CompareTo(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CompareOrdinal(this string a, string b) => StringComparer.Ordinal.Compare(a, b);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CompareOrdinal(this string a, string b) => StringComparer.Ordinal.Compare(a, b);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsOrdinal(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Equals(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsOrdinal(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Equals(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsOrdinal(this string a, string b) =>
|
||||
a?.Equals(b, StringComparison.Ordinal) ?? b == null;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EqualsOrdinal(this string a, string b) =>
|
||||
a?.Equals(b, StringComparison.Ordinal) ?? b == null;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool StartsWithOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.StartsWith(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool StartsWithOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.StartsWith(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool StartsWithOrdinal(this string a, string b) =>
|
||||
a?.StartsWith(b, StringComparison.Ordinal) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool StartsWithOrdinal(this string a, string b) =>
|
||||
a?.StartsWith(b, StringComparison.Ordinal) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EndsWithOrdinal(this string a, string b) =>
|
||||
a?.EndsWith(b, StringComparison.Ordinal) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EndsWithOrdinal(this string a, string b) =>
|
||||
a?.EndsWith(b, StringComparison.Ordinal) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EndsWithOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.EndsWith(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EndsWithOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.EndsWith(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Contains(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this ReadOnlySpan<char> a, string b) =>
|
||||
a.Contains(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this string a, string b) =>
|
||||
a?.Contains(b, StringComparison.Ordinal) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this string a, string b) =>
|
||||
a?.Contains(b, StringComparison.Ordinal) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Contains(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Contains(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this string a, char b) =>
|
||||
a?.Contains(b, StringComparison.Ordinal) == true;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool ContainsOrdinal(this string a, char b) =>
|
||||
a?.Contains(b, StringComparison.Ordinal) == true;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this string a, char b) => a?.IndexOf(b, StringComparison.Ordinal) ?? -1;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this string a, char b) => a?.IndexOf(b, StringComparison.Ordinal) ?? -1;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this string a, string b) => a?.IndexOf(b, StringComparison.Ordinal) ?? -1;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this string a, string b) => a?.IndexOf(b, StringComparison.Ordinal) ?? -1;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this string a, string b, int startIndex) =>
|
||||
a?.IndexOf(b, startIndex, StringComparison.Ordinal) ?? -1;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this string a, string b, int startIndex) =>
|
||||
a?.IndexOf(b, startIndex, StringComparison.Ordinal) ?? -1;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.IndexOf(b, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.IndexOf(b, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string ReplaceOrdinal(this string a, string o, string n) =>
|
||||
a?.Replace(o, n, StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string ReplaceOrdinal(this string a, string o, string n) =>
|
||||
a?.Replace(o, n, StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string RemoveOrdinal(this string a, string b) =>
|
||||
a?.Replace(b, "", StringComparison.Ordinal);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string RemoveOrdinal(this string a, string b) =>
|
||||
a?.Replace(b, "", StringComparison.Ordinal);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void RemoveOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b, Span<char> buffer, out int size) =>
|
||||
a.Remove(b, StringComparison.Ordinal, buffer, out size);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void RemoveOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b, Span<char> buffer, out int size) =>
|
||||
a.Remove(b, StringComparison.Ordinal, buffer, out size);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string RemoveOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Remove(b, StringComparison.Ordinal);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string RemoveOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Remove(b, StringComparison.Ordinal);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: TextEncoding.cs *
|
||||
* *
|
||||
|
|
@ -18,131 +18,130 @@ using System.Runtime.CompilerServices;
|
|||
using System.Text;
|
||||
using Server.Buffers;
|
||||
|
||||
namespace Server.Text
|
||||
namespace Server.Text;
|
||||
|
||||
public static class TextEncoding
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (str.Length == 0)
|
||||
{
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
|
||||
encoding.GetBytes(str, bytes);
|
||||
|
||||
return bytes;
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
// Unlike the one built into the encoder, this avoids local init
|
||||
public static byte[] GetBytes(string str, Encoding encoding)
|
||||
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)
|
||||
{
|
||||
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;
|
||||
return Array.Empty<byte>();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesAscii(this string str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
|
||||
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
|
||||
encoding.GetBytes(str, 0, str.Length, bytes, 0);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesAscii(this ReadOnlySpan<char> str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesBigUni(this string str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesAscii(this string str, Span<byte> buffer) => Encoding.ASCII.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 GetBytesAscii(this ReadOnlySpan<char> str, Span<byte> buffer) => Encoding.ASCII.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 GetBytesBigUni(this string str, Span<byte> buffer) => Unicode.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 GetBytesBigUni(this ReadOnlySpan<char> str, Span<byte> buffer) => Unicode.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 GetBytesLittleUni(this string str, Span<byte> buffer) => UnicodeLE.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 GetBytesLittleUni(this ReadOnlySpan<char> str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetByteLengthForEncoding(this Encoding encoding) =>
|
||||
encoding.BodyName switch
|
||||
{
|
||||
"utf-16BE" => 2,
|
||||
"utf-16" => 2,
|
||||
"utf-32BE" => 3,
|
||||
"utf-32" => 3,
|
||||
_ => 1
|
||||
};
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesUtf8(this string str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsSafeChar(ushort c) => c >= 0x20 && c < 0xFFFE;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetBytesUtf8(this ReadOnlySpan<char> str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
|
||||
|
||||
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetByteLengthForEncoding(this Encoding encoding) =>
|
||||
encoding.BodyName switch
|
||||
{
|
||||
string s = encoding.GetString(span);
|
||||
"utf-16BE" => 2,
|
||||
"utf-16" => 2,
|
||||
"utf-32BE" => 3,
|
||||
"utf-32" => 3,
|
||||
_ => 1
|
||||
};
|
||||
|
||||
if (!safeString)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsSafeChar(ushort c) => c >= 0x20 && c < 0xFFFE;
|
||||
|
||||
ReadOnlySpan<char> chars = s.AsSpan();
|
||||
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
|
||||
{
|
||||
string s = encoding.GetString(span);
|
||||
|
||||
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();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue