fix(core): Optimizes strings / .NET 5 compatibility changes (#354)
- [X] Removes some string allocations (e.g. split) - [X] Optimizes some collections - [X] Converts insensitive to extension methods of built-ins. - [X] Adds ordinal (case sensitive) string helpers - [X] Fixes conditionals for in-game commands so they use Ordinal comparisons. - [X] Replaces ToLower.Contains with InsensitiveContains - [X] Adds ValueStringBuilder - [X] Implements ValueStringBuilder in a few places where it makes sense - [X] Removes the redundant Wrap function and replaces it with an optimized version - [X] Fixes list conversions in Utility Closes #351 Bumps release version
This commit is contained in:
parent
92aae8d482
commit
77ce2e1980
117 changed files with 2000 additions and 1226 deletions
102
Projects/Server/Utilities/InsensitiveStringHelpers.cs
Normal file
102
Projects/Server/Utilities/InsensitiveStringHelpers.cs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
/*************************************************************************
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
100
Projects/Server/Utilities/OrdinalStringHelpers.cs
Normal file
100
Projects/Server/Utilities/OrdinalStringHelpers.cs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/*************************************************************************
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
245
Projects/Server/Utilities/StringHelpers.cs
Normal file
245
Projects/Server/Utilities/StringHelpers.cs
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
/*************************************************************************
|
||||
* 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) => value?.Trim().Length > 0 ? value : def;
|
||||
|
||||
[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 - 1)
|
||||
{
|
||||
// Could be the end of the string
|
||||
var length = span[newLineLength] == ' ' ? newLineLength - 1 : newLineLength;
|
||||
list.Add(span.Slice(0, length).ToString());
|
||||
if (list.Count == maxLines)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
span = span.Slice(newLineLength);
|
||||
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(lineLength);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
|
|
@ -11,6 +12,7 @@ using System.Runtime.InteropServices;
|
|||
using System.Text;
|
||||
using System.Xml;
|
||||
using Microsoft.Toolkit.HighPerformance.Extensions;
|
||||
using Server.Buffers;
|
||||
using Server.Random;
|
||||
|
||||
namespace Server
|
||||
|
|
@ -144,18 +146,20 @@ namespace Server
|
|||
|
||||
ReadOnlySpan<char> chars = s.AsSpan();
|
||||
|
||||
StringBuilder stringBuilder = null;
|
||||
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]) || stringBuilder != null && i == chars.Length - 1)
|
||||
if (!IsSafeChar(chars[i]) && i == chars.Length - 1)
|
||||
{
|
||||
(stringBuilder ??= new StringBuilder()).Append(chars.Slice(last, i - last));
|
||||
hasDoneAnyReplacements = true;
|
||||
sb.Append(chars.Slice(last, i - last));
|
||||
last = i + 1; // Skip the unsafe char
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder?.ToString() ?? s;
|
||||
return !hasDoneAnyReplacements ? s : sb.ToString();
|
||||
}
|
||||
|
||||
public static void Separate(StringBuilder sb, string value, string separator)
|
||||
|
|
@ -175,9 +179,6 @@ namespace Server
|
|||
str = Intern(str);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string DefaultIfNullOrEmpty(this string value, string def) => value?.Trim().Length > 0 ? value : def;
|
||||
|
||||
public static IPAddress Intern(IPAddress ipAddress)
|
||||
{
|
||||
if (ipAddress == null)
|
||||
|
|
@ -301,7 +302,7 @@ namespace Server
|
|||
public static bool IPMatch(string val, IPAddress ip, out bool valid)
|
||||
{
|
||||
var family = ip.AddressFamily;
|
||||
var useIPv6 = family == AddressFamily.InterNetworkV6 || val.Contains(':', StringComparison.Ordinal);
|
||||
var useIPv6 = family == AddressFamily.InterNetworkV6 || val.ContainsOrdinal(':');
|
||||
|
||||
ip = useIPv6 ? ip.MapToIPv6() : ip.MapToIPv4();
|
||||
|
||||
|
|
@ -619,9 +620,9 @@ namespace Server
|
|||
return "";
|
||||
}
|
||||
|
||||
var hasOpen = str.Contains('<', StringComparison.Ordinal);
|
||||
var hasClose = str.Contains('>', StringComparison.Ordinal);
|
||||
var hasPound = str.Contains('#', StringComparison.Ordinal);
|
||||
var hasOpen = str.ContainsOrdinal('<');
|
||||
var hasClose = str.ContainsOrdinal('>');
|
||||
var hasPound = str.ContainsOrdinal('#');
|
||||
|
||||
if (!hasOpen && !hasClose && !hasPound)
|
||||
{
|
||||
|
|
@ -648,9 +649,9 @@ namespace Server
|
|||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static int InsensitiveCompare(string first, string second) => Insensitive.Compare(first, second);
|
||||
public static int InsensitiveCompare(string first, string second) => first.InsensitiveCompare(second);
|
||||
|
||||
public static bool InsensitiveStartsWith(string first, string second) => Insensitive.StartsWith(first, second);
|
||||
public static bool InsensitiveStartsWith(string first, string second) => first.InsensitiveStartsWith(second);
|
||||
|
||||
public static Direction GetDirection(IPoint2D from, IPoint2D to)
|
||||
{
|
||||
|
|
@ -990,33 +991,18 @@ namespace Server
|
|||
}
|
||||
}
|
||||
|
||||
public static List<TOutput> CastListContravariant<TInput, TOutput>(this IReadOnlyCollection<TInput> coll)
|
||||
where TInput : TOutput
|
||||
// Using this instead of Linq Cast<> means we can ditch the yield and enforce contravariance
|
||||
public static HashSet<TOutput> SafeConvertSet<TInput, TOutput>(this IEnumerable<TInput> coll)
|
||||
where TOutput : TInput => coll.SafeConvert<HashSet<TOutput>, TInput, TOutput>();
|
||||
|
||||
public static List<TOutput> SafeConvertList<TInput, TOutput>(this IEnumerable<TInput> coll)
|
||||
where TOutput : TInput => coll.SafeConvert<List<TOutput>, TInput, TOutput>();
|
||||
|
||||
public static TColl SafeConvert<TColl, TInput, TOutput>(this IEnumerable<TInput> coll)
|
||||
where TOutput : TInput where TColl : ICollection<TOutput>, new()
|
||||
{
|
||||
var outputList = new List<TOutput>();
|
||||
foreach (var entry in coll)
|
||||
{
|
||||
outputList.Add(entry);
|
||||
}
|
||||
var outputList = new TColl();
|
||||
|
||||
return outputList;
|
||||
}
|
||||
|
||||
public static List<TOutput> CastListCovariant<TInput, TOutput>(this IReadOnlyCollection<TInput> coll)
|
||||
where TOutput : TInput
|
||||
{
|
||||
var outputList = new List<TOutput>();
|
||||
foreach (var entry in coll)
|
||||
{
|
||||
outputList.Add((TOutput)entry);
|
||||
}
|
||||
|
||||
return outputList;
|
||||
}
|
||||
|
||||
public static List<TOutput> SafeConvertList<TInput, TOutput>(this IReadOnlyCollection<TInput> coll) where TOutput : class
|
||||
{
|
||||
var outputList = new List<TOutput>();
|
||||
foreach (var entry in coll)
|
||||
{
|
||||
if (entry is TOutput outEntry)
|
||||
|
|
@ -1055,14 +1041,14 @@ namespace Server
|
|||
return t;
|
||||
}
|
||||
|
||||
public static int ToInt32(string value)
|
||||
public static int ToInt32(ReadOnlySpan<char> value)
|
||||
{
|
||||
int i;
|
||||
|
||||
#pragma warning disable CA1806 // Do not ignore method results
|
||||
if (value.StartsWith("0x", StringComparison.Ordinal))
|
||||
if (value.StartsWithOrdinal("0x"))
|
||||
{
|
||||
int.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i);
|
||||
int.TryParse(value.Slice(2), NumberStyles.HexNumber, null, out i);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -1073,14 +1059,14 @@ namespace Server
|
|||
return i;
|
||||
}
|
||||
|
||||
public static uint ToUInt32(string value)
|
||||
public static uint ToUInt32(ReadOnlySpan<char> value)
|
||||
{
|
||||
uint i;
|
||||
|
||||
#pragma warning disable CA1806 // Do not ignore method results
|
||||
if (value.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
|
||||
if (value.InsensitiveStartsWith("0x"))
|
||||
{
|
||||
uint.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i);
|
||||
uint.TryParse(value.Slice(2), NumberStyles.HexNumber, null, out i);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -1091,14 +1077,14 @@ namespace Server
|
|||
return i;
|
||||
}
|
||||
|
||||
public static bool ToInt32(string value, out int i) =>
|
||||
value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||
? int.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i)
|
||||
public static bool ToInt32(ReadOnlySpan<char> value, out int i) =>
|
||||
value.InsensitiveStartsWith("0x")
|
||||
? int.TryParse(value.Slice(2), NumberStyles.HexNumber, null, out i)
|
||||
: int.TryParse(value, out i);
|
||||
|
||||
public static bool ToUInt32(string value, out uint i) =>
|
||||
value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||
? uint.TryParse(value.Substring(2), NumberStyles.HexNumber, null, out i)
|
||||
public static bool ToUInt32(ReadOnlySpan<char> value, out uint i) =>
|
||||
value.InsensitiveStartsWith("0x")
|
||||
? uint.TryParse(value.Slice(2), NumberStyles.HexNumber, null, out i)
|
||||
: uint.TryParse(value, out i);
|
||||
|
||||
public static int GetXMLInt32(string intString, int defaultValue)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue