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
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue