fix: Adds ISpanParsable and fixes command conditionals (#1241)
* Adds `ISpanParsable<T>` * Removes `[Parsable]` * Fixes querying by serial, body, and a few others. * Adds `Parse` to `Rectangle3D` * Fixes AutoArchive NPE Closes #1209
This commit is contained in:
parent
d494a9c78c
commit
03fb36c869
21 changed files with 1119 additions and 271 deletions
|
|
@ -35,6 +35,10 @@ public static class OrdinalStringHelpers
|
|||
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, char b) =>
|
||||
a.StartsWithOrdinal(new ReadOnlySpan<char>(b));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool StartsWithOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.StartsWith(b, StringComparison.Ordinal);
|
||||
|
|
@ -47,6 +51,9 @@ public static class OrdinalStringHelpers
|
|||
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, char b) => a.EndsWithOrdinal(new ReadOnlySpan<char>(b));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool EndsWithOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.EndsWith(b, StringComparison.Ordinal);
|
||||
|
|
@ -77,6 +84,10 @@ public static class OrdinalStringHelpers
|
|||
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, char b) =>
|
||||
a.IndexOfOrdinal(new ReadOnlySpan<char>(b));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int IndexOfOrdinal(this ReadOnlySpan<char> a, ReadOnlySpan<char> b) =>
|
||||
a.IndexOf(b, StringComparison.Ordinal);
|
||||
|
|
|
|||
|
|
@ -18,9 +18,8 @@ using System.Runtime.CompilerServices;
|
|||
|
||||
namespace Server;
|
||||
|
||||
[Parsable]
|
||||
[PropertyObject]
|
||||
public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
|
||||
public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>, ISpanParsable<TextDefinition>
|
||||
{
|
||||
public static readonly TextDefinition Empty = new();
|
||||
|
||||
|
|
@ -30,9 +29,15 @@ public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static TextDefinition Of(string text) => Of(0, text);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static TextDefinition Of(ReadOnlySpan<char> text) => Of(0, text);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static TextDefinition Of(int number, string text) => new(number, text);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static TextDefinition Of(int number, ReadOnlySpan<char> text) => new(number, text);
|
||||
|
||||
private TextDefinition()
|
||||
{
|
||||
}
|
||||
|
|
@ -43,6 +48,12 @@ public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
|
|||
String = text;
|
||||
}
|
||||
|
||||
private TextDefinition(int number, ReadOnlySpan<char> text)
|
||||
{
|
||||
Number = number;
|
||||
String = text.ToString();
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.GameMaster)]
|
||||
public int Number { get; }
|
||||
|
||||
|
|
@ -67,16 +78,6 @@ public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
|
|||
|
||||
public static implicit operator string(TextDefinition m) => m?.String;
|
||||
|
||||
public static TextDefinition Parse(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Utility.ToInt32(value, out var i) ? Of(i) : Of(value);
|
||||
}
|
||||
|
||||
public void Deconstruct(out int number, out string s)
|
||||
{
|
||||
if (Number > 0)
|
||||
|
|
@ -118,4 +119,38 @@ public class TextDefinition : IEquatable<object>, IEquatable<TextDefinition>
|
|||
public static bool operator ==(TextDefinition left, TextDefinition right) => Equals(left, right);
|
||||
|
||||
public static bool operator !=(TextDefinition left, TextDefinition right) => !Equals(left, right);
|
||||
|
||||
public static TextDefinition Parse(string value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Utility.ToInt32(value, out var i) ? Of(i) : Of(value);
|
||||
}
|
||||
|
||||
public static TextDefinition Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
public static bool TryParse(string s, IFormatProvider provider, out TextDefinition result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static TextDefinition Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
// We don't trim
|
||||
return int.TryParse(s, provider, out var label) ? Of(label) : Of(s);
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out TextDefinition result)
|
||||
{
|
||||
if (int.TryParse(s, provider, out var label))
|
||||
{
|
||||
result = Of(label);
|
||||
return true;
|
||||
}
|
||||
|
||||
// We don't trim
|
||||
result = Of(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue