fix: Adds ISpanFormattable to Geometry structs (#1231)

* Adds ISpanFormattable to geometry structs and makes ToString() near-zero-allocation.
* Adds IEquatable, and Parsable to Rectangle3D to get it in-line with the other structs.

Closes #1067
This commit is contained in:
Kamron Batman 2022-11-06 17:37:30 -08:00 committed by GitHub
parent c75bde55da
commit d7d914df6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 721 additions and 195 deletions

View file

@ -118,9 +118,22 @@ public readonly struct Serial : IComparable<Serial>, IComparable<uint>, IEquatab
public static Serial operator --(Serial l) => (Serial)(l.Value - 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => $"{this}";
public override string ToString()
{
// Maximum number of characters that are needed to represent this:
// 2 characters for 0x
// Up to 8 characters to represent the value in hex
Span<char> span = stackalloc char[10];
TryFormat(span, out var charsWritten, null, null);
return span[..charsWritten].ToString();
}
public string ToString(string format, IFormatProvider formatProvider) => ToString();
public string ToString(string format, IFormatProvider formatProvider)
{
// format and formatProvider are not doing anything right now, so use the
// default ToString implementation.
return ToString();
}
public bool TryFormat(
Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider