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
|
|
@ -1,114 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: HexStringConverter.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;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
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 s = i.ToString("X2");
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
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 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 = 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(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,102 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: InsensitiveStringHelpers.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;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
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 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 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 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 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 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 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, 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 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 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,100 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: OrdinalStringHelpers.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;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
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 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 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 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 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 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 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, 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 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 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 string RemoveOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.Remove(b, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,244 +0,0 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2020 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: StringHelpers.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.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public static class StringHelpers
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string DefaultIfNullOrEmpty(this string value, string def) =>
|
||||
string.IsNullOrWhiteSpace(value) ? def : value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Remove(
|
||||
this ReadOnlySpan<char> a,
|
||||
ReadOnlySpan<char> b,
|
||||
StringComparison comparison,
|
||||
Span<char> buffer,
|
||||
out int size
|
||||
)
|
||||
{
|
||||
size = 0;
|
||||
if (a == null || a.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var sliced = a;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var indexOf = sliced.IndexOf(b, comparison);
|
||||
if (indexOf == -1)
|
||||
{
|
||||
indexOf = sliced.Length;
|
||||
}
|
||||
|
||||
if (size + indexOf > buffer.Length)
|
||||
{
|
||||
throw new OutOfMemoryException(nameof(buffer));
|
||||
}
|
||||
|
||||
sliced.Slice(0, indexOf).CopyTo(buffer.Slice(size));
|
||||
size += indexOf;
|
||||
|
||||
if (indexOf == sliced.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
sliced = sliced.Slice(indexOf + 1);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string Remove(this ReadOnlySpan<char> a, ReadOnlySpan<char> b, StringComparison comparison)
|
||||
{
|
||||
if (a == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (a.Length == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
Span<char> span = a.Length < 1024 ? stackalloc char[a.Length] : null;
|
||||
char[] chrs;
|
||||
if (span == null)
|
||||
{
|
||||
chrs = ArrayPool<char>.Shared.Rent(a.Length);
|
||||
span = chrs.AsSpan();
|
||||
}
|
||||
else
|
||||
{
|
||||
chrs = null;
|
||||
}
|
||||
|
||||
a.Remove(b, comparison, span, out var size);
|
||||
|
||||
var str = span.Slice(0, size).ToString();
|
||||
|
||||
if (chrs != null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(chrs);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string Capitalize(this string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
Span<char> span = value.Length < 1024 ? stackalloc char[value.Length] : null;
|
||||
char[] chrs;
|
||||
if (span == null)
|
||||
{
|
||||
chrs = ArrayPool<char>.Shared.Rent(value.Length);
|
||||
span = chrs.AsSpan();
|
||||
}
|
||||
else
|
||||
{
|
||||
chrs = null;
|
||||
}
|
||||
|
||||
var sliced = value.AsSpan();
|
||||
// Copy over the previous span
|
||||
sliced.CopyTo(span);
|
||||
|
||||
var index = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Special case for titles - words that don't get capitalized
|
||||
if (sliced.InsensitiveStartsWith("the "))
|
||||
{
|
||||
sliced = sliced.Slice(4);
|
||||
index += 4;
|
||||
continue;
|
||||
}
|
||||
|
||||
var indexOf = sliced.IndexOf(' ');
|
||||
span[index] = char.ToUpperInvariant(sliced[0]);
|
||||
|
||||
if (indexOf == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (indexOf == sliced.Length - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
sliced = sliced.Slice(indexOf + 1);
|
||||
index += indexOf + 1;
|
||||
}
|
||||
|
||||
var str = span.ToString();
|
||||
|
||||
if (chrs != null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(chrs);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static List<string> Wrap(this string value, int perLine, int maxLines)
|
||||
{
|
||||
if ((value = value?.Trim() ?? "").Length <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var span = value.AsSpan();
|
||||
var list = new List<string>(maxLines);
|
||||
var lineLength = 0;
|
||||
|
||||
while (span.Length > 0)
|
||||
{
|
||||
var spaceIndex = span.Slice(lineLength).IndexOf(' ');
|
||||
if (spaceIndex == -1)
|
||||
{
|
||||
spaceIndex = span.Length; // End of the string
|
||||
}
|
||||
|
||||
var newLineLength = lineLength + spaceIndex;
|
||||
|
||||
if (newLineLength == perLine || newLineLength == span.Length)
|
||||
{
|
||||
list.Add(span.Slice(0, newLineLength).ToString());
|
||||
if (list.Count == maxLines || newLineLength == span.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
span = span.Slice(newLineLength + 1);
|
||||
lineLength = 0;
|
||||
}
|
||||
else if (newLineLength < perLine)
|
||||
{
|
||||
lineLength = newLineLength + 1;
|
||||
}
|
||||
else if (lineLength > 0 && lineLength <= perLine)
|
||||
{
|
||||
list.Add(span.Slice(0, lineLength - 1).ToString());
|
||||
if (list.Count == maxLines)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
span = span.Slice(lineLength);
|
||||
lineLength = spaceIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
lineLength = newLineLength;
|
||||
var index = 0;
|
||||
|
||||
while (index < lineLength)
|
||||
{
|
||||
lineLength -= perLine;
|
||||
|
||||
var length = perLine - (span[index] == ' ' ? 1 : 0);
|
||||
list.Add(span.Slice(index, length).ToString());
|
||||
if (list.Count == maxLines)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
index += perLine;
|
||||
}
|
||||
|
||||
span = span.Slice(newLineLength - lineLength);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,19 +7,15 @@ using System.Linq;
|
|||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using Microsoft.Toolkit.HighPerformance.Extensions;
|
||||
using Server.Buffers;
|
||||
using Server.Random;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public static class Utility
|
||||
{
|
||||
private static Encoding m_UTF8, m_UTF8WithEncoding, m_Unicode, m_UnicodeLE;
|
||||
|
||||
private static Dictionary<IPAddress, IPAddress> _ipAddressTable;
|
||||
|
||||
private static readonly SkillName[] m_AllSkills =
|
||||
|
|
@ -105,62 +101,6 @@ namespace Server
|
|||
|
||||
private static readonly Stack<ConsoleColor> m_ConsoleColors = new();
|
||||
|
||||
public static Encoding UTF8 => m_UTF8 ??= new UTF8Encoding(false, false);
|
||||
public static Encoding UTF8WithEncoding => m_UTF8WithEncoding ??= new UTF8Encoding(true, false);
|
||||
public static Encoding Unicode => m_Unicode ??= new UnicodeEncoding(true, false, false);
|
||||
public static Encoding UnicodeLE => m_UnicodeLE ??= new UnicodeEncoding(false, false, false);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetByteLengthForEncoding(Encoding encoding) =>
|
||||
encoding.BodyName switch
|
||||
{
|
||||
"utf-16BE" => 2,
|
||||
"utf-16" => 2,
|
||||
"utf-32BE" => 4,
|
||||
"utf-32" => 4,
|
||||
_ => 1
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfTerminator(ReadOnlySpan<byte> buffer, int sizeT) =>
|
||||
sizeT switch
|
||||
{
|
||||
2 => MemoryMarshal.Cast<byte, char>(buffer).IndexOf((char)0) * 2,
|
||||
4 => MemoryMarshal.Cast<byte, uint>(buffer).IndexOf((uint)0) * 4,
|
||||
_ => buffer.IndexOf((byte)0)
|
||||
};
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsSafeChar(ushort c) => c >= 0x20 && c < 0xFFFE;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
|
||||
{
|
||||
string s = encoding.GetString(span);
|
||||
|
||||
if (!safeString)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> chars = s.AsSpan();
|
||||
|
||||
using ValueStringBuilder sb = new ValueStringBuilder(stackalloc char[256]);
|
||||
var hasDoneAnyReplacements = false;
|
||||
|
||||
for (int i = 0, last = 0; i < chars.Length; i++)
|
||||
{
|
||||
if (!IsSafeChar(chars[i]) && i == chars.Length - 1)
|
||||
{
|
||||
hasDoneAnyReplacements = true;
|
||||
sb.Append(chars.Slice(last, i - last));
|
||||
last = i + 1; // Skip the unsafe char
|
||||
}
|
||||
}
|
||||
|
||||
return !hasDoneAnyReplacements ? s : sb.ToString();
|
||||
}
|
||||
|
||||
public static void Separate(StringBuilder sb, string value, string separator)
|
||||
{
|
||||
if (sb.Length > 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue