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
|
|
@ -88,11 +88,6 @@ public class TypeAliasAttribute : Attribute
|
|||
public string[] Aliases { get; }
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
|
||||
public class ParsableAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum)]
|
||||
public class CustomEnumAttribute : Attribute
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,13 +14,13 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server;
|
||||
|
||||
[Parsable]
|
||||
public struct Point2D
|
||||
: IPoint2D, IComparable<Point2D>, IComparable<IPoint2D>, IEquatable<object>, IEquatable<Point2D>,
|
||||
IEquatable<IPoint2D>, ISpanFormattable
|
||||
IEquatable<IPoint2D>, ISpanFormattable, ISpanParsable<Point2D>
|
||||
{
|
||||
internal int m_X;
|
||||
internal int m_Y;
|
||||
|
|
@ -51,21 +51,6 @@ public struct Point2D
|
|||
{
|
||||
}
|
||||
|
||||
public static Point2D Parse(string value)
|
||||
{
|
||||
var start = value.IndexOfOrdinal('(');
|
||||
var end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(')', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
|
||||
|
||||
return new Point2D(x, y);
|
||||
}
|
||||
|
||||
public bool Equals(Point2D other) => m_X == other.m_X && m_Y == other.m_Y;
|
||||
|
||||
public bool Equals(IPoint2D other) => m_X == other?.X && m_Y == other.Y;
|
||||
|
|
@ -130,4 +115,79 @@ public struct Point2D
|
|||
// default ToString implementation.
|
||||
return ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Point2D Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Point2D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Point2D result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Point2D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var comma = s.IndexOfOrdinal(',');
|
||||
if (comma == -1)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var first = s.Slice(1, comma - 1).Trim();
|
||||
if (!Utility.ToInt32(first, out var x))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var second = s.Slice(comma + 1, s.Length - comma - 2).Trim();
|
||||
if (!Utility.ToInt32(second, out var y))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
return new Point2D(x, y);
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Point2D result)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var comma = s.IndexOfOrdinal(',');
|
||||
if (comma == -1)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var first = s.Slice(1, comma - 1).Trim();
|
||||
if (!Utility.ToInt32(first, out var x))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var second = s.Slice(comma + 1, s.Length - comma - 2).Trim();
|
||||
if (!Utility.ToInt32(second, out var y))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = new Point2D(x, y);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,10 +18,9 @@ using System.Runtime.CompilerServices;
|
|||
|
||||
namespace Server;
|
||||
|
||||
[Parsable]
|
||||
public struct Point3D
|
||||
: IPoint3D, IComparable<Point3D>, IComparable<IPoint3D>, IEquatable<object>, IEquatable<Point3D>,
|
||||
IEquatable<IPoint3D>, ISpanFormattable
|
||||
IEquatable<IPoint3D>, ISpanFormattable, ISpanParsable<Point3D>
|
||||
{
|
||||
internal int m_X;
|
||||
internal int m_Y;
|
||||
|
|
@ -79,26 +78,6 @@ public struct Point3D
|
|||
|
||||
public override int GetHashCode() => HashCode.Combine(m_X, m_Y, m_Z);
|
||||
|
||||
public static Point3D Parse(string value)
|
||||
{
|
||||
var start = value.IndexOfOrdinal('(');
|
||||
var end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(')', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var z);
|
||||
|
||||
return new Point3D(x, y, z);
|
||||
}
|
||||
|
||||
public static bool operator ==(Point3D l, Point3D r) => l.m_X == r.m_X && l.m_Y == r.m_Y && l.m_Z == r.m_Z;
|
||||
|
||||
public static bool operator ==(Point3D l, IPoint3D r) =>
|
||||
|
|
@ -183,4 +162,115 @@ public struct Point3D
|
|||
// default ToString implementation.
|
||||
return ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Point3D Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Point3D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Point3D result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Point3D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var firstComma = s.IndexOfOrdinal(',');
|
||||
if (firstComma == -1)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var first = s.Slice(1, firstComma - 1).Trim();
|
||||
|
||||
if (!Utility.ToInt32(first, out var x))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var offset = firstComma + 1;
|
||||
|
||||
var secondComma = s[offset..].IndexOfOrdinal(',');
|
||||
if (secondComma == -1 || offset == secondComma)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var second = s.Slice(firstComma + 1, secondComma).Trim();
|
||||
|
||||
if (!Utility.ToInt32(second, out var y))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
offset += secondComma + 1;
|
||||
|
||||
var third = s.Slice(offset, s.Length - offset - 1).Trim();
|
||||
if (!Utility.ToInt32(third, out var z))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
return new Point3D(x, y, z);
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Point3D result)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (!s.StartsWithOrdinal('(') || !s.EndsWithOrdinal(')'))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var firstComma = s.IndexOfOrdinal(',');
|
||||
if (firstComma == -1)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var first = s.Slice(1, firstComma - 1).Trim();
|
||||
if (!Utility.ToInt32(first, out var x))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var offset = firstComma + 1;
|
||||
|
||||
var secondComma = s[offset..].IndexOfOrdinal(',');
|
||||
if (secondComma == -1 || offset == secondComma)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var second = s.Slice(firstComma + 1, secondComma).Trim();
|
||||
if (!Utility.ToInt32(second, out var y))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
offset += secondComma + 1;
|
||||
|
||||
var third = s.Slice(offset, s.Length - offset - 1).Trim();
|
||||
if (!Utility.ToInt32(third, out var z))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = new Point3D(x, y, z);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,13 +14,13 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server;
|
||||
|
||||
[NoSort]
|
||||
[Parsable]
|
||||
[PropertyObject]
|
||||
public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
|
||||
public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable, ISpanParsable<Rectangle2D>
|
||||
{
|
||||
private Point2D _start;
|
||||
private Point2D _end;
|
||||
|
|
@ -118,31 +118,6 @@ public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
|
|||
|
||||
public static bool operator !=(Rectangle2D l, Rectangle2D r) => l._start != r._start || l._end != r._end;
|
||||
|
||||
public static Rectangle2D Parse(string value)
|
||||
{
|
||||
var start = value.IndexOfOrdinal('(');
|
||||
var end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var w);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(')', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var h);
|
||||
|
||||
return new Rectangle2D(x, y, w, h);
|
||||
}
|
||||
|
||||
public bool Contains(Point3D p) =>
|
||||
_start.m_X <= p.m_X && _start.m_Y <= p.m_Y && _end.m_X > p.m_X && _end.m_Y > p.m_Y;
|
||||
|
||||
|
|
@ -172,4 +147,64 @@ public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
|
|||
// default ToString implementation.
|
||||
return ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Rectangle2D Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Rectangle2D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Rectangle2D result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Rectangle2D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
var delimiter = s.IndexOfOrdinal('+');
|
||||
if (delimiter == -1)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
if (!Point2D.TryParse(s[..delimiter], provider, out var start))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
if (!Point2D.TryParse(s[(delimiter + 1)..], provider, out var end))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
return new Rectangle2D(start, end);
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Rectangle2D result)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
var delimiter = s.IndexOfOrdinal('+');
|
||||
if (delimiter == -1)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Point2D.TryParse(s[..delimiter], provider, out var start))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Point2D.TryParse(s[(delimiter + 1)..], provider, out var end))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = new Rectangle2D(start, end);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,11 @@
|
|||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server;
|
||||
|
||||
[NoSort]
|
||||
[Parsable]
|
||||
[PropertyObject]
|
||||
public struct Rectangle3D : IEquatable<Rectangle3D>, ISpanFormattable
|
||||
{
|
||||
|
|
@ -172,4 +172,64 @@ public struct Rectangle3D : IEquatable<Rectangle3D>, ISpanFormattable
|
|||
// default ToString implementation.
|
||||
return ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Rectangle3D Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Rectangle3D Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Rectangle3D result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Rectangle3D Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
var delimiter = s.IndexOfOrdinal('+');
|
||||
if (delimiter == -1)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
if (!Point3D.TryParse(s[..delimiter], provider, out var start))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
if (!Point3D.TryParse(s[(delimiter + 1)..], provider, out var end))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
return new Rectangle3D(start, end);
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Rectangle3D result)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
var delimiter = s.IndexOfOrdinal('+');
|
||||
if (delimiter == -1)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Point3D.TryParse(s[..delimiter], provider, out var start))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Point3D.TryParse(s[(delimiter + 1)..], provider, out var end))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = new Rectangle3D(start, end);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,12 +16,12 @@
|
|||
using System;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server;
|
||||
|
||||
[Parsable]
|
||||
public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<WorldLocation>, IEquatable<IEntity>,
|
||||
ISpanFormattable
|
||||
ISpanFormattable, ISpanParsable<WorldLocation>
|
||||
{
|
||||
internal Point3D _loc;
|
||||
internal Map _map;
|
||||
|
|
@ -63,7 +63,7 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
|
|||
set => _map = value;
|
||||
}
|
||||
|
||||
public WorldLocation(IEntity e) : this(e.Location.X, e.Location.Y, e.Location.Z, e.Map)
|
||||
public WorldLocation(IEntity e) : this(e.Location, e.Map)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -75,8 +75,10 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
|
|||
{
|
||||
}
|
||||
|
||||
public WorldLocation(Point3D p, Map map) : this(p.X, p.Y, p.Z, map)
|
||||
public WorldLocation(Point3D p, Map map)
|
||||
{
|
||||
_loc = p;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public WorldLocation(int x, int y, int z, Map map)
|
||||
|
|
@ -88,11 +90,11 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
|
|||
}
|
||||
|
||||
public bool Equals(WorldLocation other) =>
|
||||
_loc.Equals(other._loc) && _map.MapID == other._map.MapID;
|
||||
_loc.Equals(other._loc) && _map?.MapID == other._map?.MapID;
|
||||
|
||||
public bool Equals(IEntity other) =>
|
||||
!ReferenceEquals(other, null) && _loc == other.Location &&
|
||||
_map.MapID == other.Map.MapID;
|
||||
_map?.MapID == other.Map?.MapID;
|
||||
|
||||
public override bool Equals(object obj) =>
|
||||
obj is WorldLocation other && Equals(other);
|
||||
|
|
@ -138,31 +140,6 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
|
|||
public static bool operator <=(WorldLocation l, IEntity r) =>
|
||||
!ReferenceEquals(r, null) && l._loc <= r.Location && l._map == r.Map;
|
||||
|
||||
public static WorldLocation Parse(string value)
|
||||
{
|
||||
var start = value.IndexOfOrdinal('(');
|
||||
var end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var x);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var y);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(',', start + 1);
|
||||
|
||||
Utility.ToInt32(value.AsSpan(start + 1, end - (start + 1)).Trim(), out var z);
|
||||
|
||||
start = end;
|
||||
end = value.IndexOf(')', start + 1);
|
||||
|
||||
var map = Map.Parse(value.AsSpan(start + 1, end - (start + 1)).Trim());
|
||||
|
||||
return new WorldLocation(x, y, z, map);
|
||||
}
|
||||
|
||||
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
|
||||
{
|
||||
if (_map == null)
|
||||
|
|
@ -208,4 +185,77 @@ public struct WorldLocation : IPoint3D, IComparable<WorldLocation>, IEquatable<W
|
|||
// default ToString implementation.
|
||||
return ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static WorldLocation Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static WorldLocation Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out WorldLocation result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static WorldLocation Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (!s.EndsWithOrdinal(']'))
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var mapStartBracket = s.IndexOf('[');
|
||||
if (mapStartBracket == -1)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
var loc = Point3D.Parse(s[..(mapStartBracket - 1)], provider);
|
||||
|
||||
var mapSlice = s.Slice(mapStartBracket + 1, s.Length - mapStartBracket - 2);
|
||||
return mapSlice.EqualsOrdinal("(-null-)")
|
||||
? new WorldLocation(loc, null)
|
||||
: new WorldLocation(loc, Map.Parse(mapSlice, provider));
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out WorldLocation result)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (!s.EndsWithOrdinal(']'))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var mapStartBracket = s.IndexOf('[');
|
||||
if (mapStartBracket == -1)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Point3D.TryParse(s[..(mapStartBracket - 1)], provider, out var loc))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var mapSlice = s.Slice(mapStartBracket + 1, s.Length - mapStartBracket - 2);
|
||||
if (mapSlice.EqualsOrdinal("(-null-)"))
|
||||
{
|
||||
result = new WorldLocation(loc, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!Map.TryParse(mapSlice, provider, out var map))
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = new WorldLocation(loc, map);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -313,8 +313,7 @@ public static class PooledEnumeration
|
|||
}
|
||||
}
|
||||
|
||||
[Parsable]
|
||||
public sealed class Map : IComparable<Map>, ISpanFormattable
|
||||
public sealed class Map : IComparable<Map>, ISpanFormattable, ISpanParsable<Map>
|
||||
{
|
||||
public const int SectorSize = 16;
|
||||
public const int SectorShift = 4;
|
||||
|
|
@ -482,49 +481,6 @@ public sealed class Map : IComparable<Map>, ISpanFormattable
|
|||
return mapValues;
|
||||
}
|
||||
|
||||
// Handles null checks
|
||||
public static Map Parse(string value) => Parse(value ?? ReadOnlySpan<char>.Empty);
|
||||
|
||||
public static Map Parse(ReadOnlySpan<char> value)
|
||||
{
|
||||
value = value.Trim();
|
||||
|
||||
if (value.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value.InsensitiveEquals("Internal"))
|
||||
{
|
||||
return Internal;
|
||||
}
|
||||
|
||||
if (!int.TryParse(value, out var index))
|
||||
{
|
||||
index = -1;
|
||||
}
|
||||
else if (index == 127)
|
||||
{
|
||||
return Internal;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Maps.Length; i++)
|
||||
{
|
||||
var map = Maps[i];
|
||||
if (map == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (index >= 0 && map.MapIndex == index || value.InsensitiveEquals(map.Name))
|
||||
{
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
|
||||
{
|
||||
if (destination.Length >= Name.Length)
|
||||
|
|
@ -1709,4 +1665,98 @@ public sealed class Map : IComparable<Map>, ISpanFormattable
|
|||
}
|
||||
}
|
||||
#pragma warning restore CA1000 // Do not declare static members on generic types
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Map Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Map Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Map result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Map Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (s.Length == 0)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
if (s.InsensitiveEquals("Internal"))
|
||||
{
|
||||
return Internal;
|
||||
}
|
||||
|
||||
if (!int.TryParse(s, provider, out var index))
|
||||
{
|
||||
index = -1;
|
||||
}
|
||||
else if (index == 127)
|
||||
{
|
||||
return Internal;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Maps.Length; i++)
|
||||
{
|
||||
var map = Maps[i];
|
||||
if (map == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (index >= 0 && map.MapIndex == index || s.InsensitiveEquals(map.Name))
|
||||
{
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Map result)
|
||||
{
|
||||
s = s.Trim();
|
||||
|
||||
if (s.Length == 0)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (s.InsensitiveEquals("Internal"))
|
||||
{
|
||||
result = Internal;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!int.TryParse(s, provider, out var index))
|
||||
{
|
||||
index = -1;
|
||||
}
|
||||
else if (index == 127)
|
||||
{
|
||||
result = Internal;
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Maps.Length; i++)
|
||||
{
|
||||
var map = Maps[i];
|
||||
if (map == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (index >= 0 && map.MapIndex == index || s.InsensitiveEquals(map.Name))
|
||||
{
|
||||
result = map;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ public enum BodyType : byte
|
|||
Equipment
|
||||
}
|
||||
|
||||
[Parsable]
|
||||
public readonly struct Body : IEquatable<object>, IEquatable<Body>, IEquatable<int>, IComparable<int>, IComparable<Body>
|
||||
public readonly struct Body : IEquatable<object>, IEquatable<Body>, IEquatable<int>,
|
||||
IComparable<int>, IComparable<Body>, ISpanParsable<Body>
|
||||
{
|
||||
private static readonly ILogger logger = LogFactory.GetLogger(typeof(Body));
|
||||
|
||||
|
|
@ -148,5 +148,28 @@ public readonly struct Body : IEquatable<object>, IEquatable<Body>, IEquatable<i
|
|||
|
||||
public int CompareTo(int other) => BodyID.CompareTo(other);
|
||||
|
||||
public static Body Parse(string value) => Utility.ToInt32(value);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Body Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Body Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Body result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Body Parse(ReadOnlySpan<char> s, IFormatProvider provider) => Utility.ToInt32(s);
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Body result)
|
||||
{
|
||||
if (Utility.ToInt32(s, out var value))
|
||||
{
|
||||
result = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Server;
|
||||
|
||||
[Parsable]
|
||||
public abstract class Poison
|
||||
public abstract class Poison : ISpanParsable<Poison>
|
||||
{
|
||||
/*public abstract TimeSpan Interval{ get; }
|
||||
public abstract TimeSpan Duration{ get; }*/
|
||||
|
|
@ -44,9 +44,6 @@ public abstract class Poison
|
|||
Poisons.Add(reg);
|
||||
}
|
||||
|
||||
public static Poison Parse(string value) =>
|
||||
(int.TryParse(value, out var plevel) ? GetPoison(plevel) : null) ?? GetPoison(value);
|
||||
|
||||
public static Poison GetPoison(int level)
|
||||
{
|
||||
for (var i = 0; i < Poisons.Count; ++i)
|
||||
|
|
@ -62,13 +59,13 @@ public abstract class Poison
|
|||
return null;
|
||||
}
|
||||
|
||||
public static Poison GetPoison(string name)
|
||||
public static Poison GetPoison(ReadOnlySpan<char> name)
|
||||
{
|
||||
for (var i = 0; i < Poisons.Count; ++i)
|
||||
{
|
||||
var p = Poisons[i];
|
||||
|
||||
if (Utility.InsensitiveCompare(p.Name, name) == 0)
|
||||
if (name.InsensitiveEquals(p.Name))
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
|
@ -76,4 +73,49 @@ public abstract class Poison
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Poison Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Poison Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Poison result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Poison Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
if (int.TryParse(s, provider, out var pLevel))
|
||||
{
|
||||
var result = GetPoison(pLevel);
|
||||
if (result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
var poison = GetPoison(s.Trim());
|
||||
if (poison == null)
|
||||
{
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
return poison;
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Poison result)
|
||||
{
|
||||
if (int.TryParse(s, provider, out var pLevel))
|
||||
{
|
||||
result = GetPoison(pLevel);
|
||||
if (result != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
result = GetPoison(s.Trim());
|
||||
return result != null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,8 @@ using System.Runtime.CompilerServices;
|
|||
|
||||
namespace Server;
|
||||
|
||||
[Parsable]
|
||||
public abstract class Race
|
||||
public abstract class Race : ISpanParsable<Race>
|
||||
{
|
||||
private static string[] m_RaceNames;
|
||||
private static Race[] m_RaceValues;
|
||||
|
||||
protected Race(
|
||||
int raceID, int raceIndex, string name, string pluralName, int maleBody, int femaleBody,
|
||||
int maleGhostBody, int femaleGhostBody, Expansion requiredExpansion
|
||||
|
|
@ -68,58 +64,6 @@ public abstract class Race
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool IsAllowedRace(Race race, int allowedRaceFlags) => (allowedRaceFlags & race.RaceFlag) != 0;
|
||||
|
||||
public static string[] GetRaceNames()
|
||||
{
|
||||
CheckNamesAndValues();
|
||||
return m_RaceNames;
|
||||
}
|
||||
|
||||
public static Race[] GetRaceValues()
|
||||
{
|
||||
CheckNamesAndValues();
|
||||
return m_RaceValues;
|
||||
}
|
||||
|
||||
public static Race Parse(string value)
|
||||
{
|
||||
CheckNamesAndValues();
|
||||
|
||||
for (var i = 0; i < m_RaceNames.Length; ++i)
|
||||
{
|
||||
if (m_RaceNames[i].InsensitiveEquals(value))
|
||||
{
|
||||
return m_RaceValues[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (int.TryParse(value, out var index) && index >= 0 && index < Races.Length &&
|
||||
Races[index] != null)
|
||||
{
|
||||
return Races[index];
|
||||
}
|
||||
|
||||
throw new ArgumentException("Invalid race name");
|
||||
}
|
||||
|
||||
private static void CheckNamesAndValues()
|
||||
{
|
||||
if (m_RaceNames?.Length == AllRaces.Count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_RaceNames = new string[AllRaces.Count];
|
||||
m_RaceValues = new Race[AllRaces.Count];
|
||||
|
||||
for (var i = 0; i < AllRaces.Count; ++i)
|
||||
{
|
||||
var race = AllRaces[i];
|
||||
|
||||
m_RaceNames[i] = race.Name;
|
||||
m_RaceValues[i] = race;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString() => Name;
|
||||
|
||||
public virtual bool ValidateHair(Mobile m, int itemID) => ValidateHair(m.Female, itemID);
|
||||
|
|
@ -153,4 +97,67 @@ public abstract class Race
|
|||
public virtual int GhostBody(Mobile m) => GhostBody(m.Female);
|
||||
|
||||
public virtual int GhostBody(bool female) => female ? FemaleGhostBody : MaleGhostBody;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Race Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Race Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Race result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Race Parse(ReadOnlySpan<char> s, IFormatProvider provider)
|
||||
{
|
||||
if (int.TryParse(s, out var index) && index >= 0 && index < Races.Length)
|
||||
{
|
||||
var race = Races[index];
|
||||
if (race != null)
|
||||
{
|
||||
return race;
|
||||
}
|
||||
}
|
||||
|
||||
s = s.Trim();
|
||||
|
||||
for (var i = 0; i < Races.Length; ++i)
|
||||
{
|
||||
var race = Races[i];
|
||||
if (s.InsensitiveEquals(race.Name) || s.InsensitiveEquals(race.PluralName))
|
||||
{
|
||||
return race;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FormatException($"The input string '{s}' was not in a correct format.");
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Race result)
|
||||
{
|
||||
if (int.TryParse(s, out var index) && index >= 0 && index < Races.Length)
|
||||
{
|
||||
var race = Races[index];
|
||||
if (race != null)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
s = s.Trim();
|
||||
|
||||
for (var i = 0; i < Races.Length; ++i)
|
||||
{
|
||||
var race = Races[i];
|
||||
if (s.InsensitiveEquals(race.Name) || s.InsensitiveEquals(race.PluralName))
|
||||
{
|
||||
result = race;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ using System.Runtime.CompilerServices;
|
|||
|
||||
namespace Server;
|
||||
|
||||
public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatable<Serial>, ISpanFormattable
|
||||
public readonly struct Serial : IComparable<Serial>, IComparable<uint>,
|
||||
IEquatable<Serial>, ISpanFormattable, ISpanParsable<Serial>
|
||||
{
|
||||
public static readonly Serial MinusOne = new(0xFFFFFFFF);
|
||||
public static readonly Serial Zero = new(0);
|
||||
|
|
@ -152,4 +153,28 @@ public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatab
|
|||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int ToInt32() => (int)Value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial Parse(string s) => Parse(s, null);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Serial Parse(string s, IFormatProvider provider) => Parse(s.AsSpan(), provider);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool TryParse(string s, IFormatProvider provider, out Serial result) =>
|
||||
TryParse(s.AsSpan(), provider, out result);
|
||||
|
||||
public static Serial Parse(ReadOnlySpan<char> s, IFormatProvider provider) => new(Utility.ToUInt32(s));
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out Serial result)
|
||||
{
|
||||
if (Utility.ToUInt32(s, out var value))
|
||||
{
|
||||
result = new Serial(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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