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

@ -314,7 +314,7 @@ public static class PooledEnumeration
}
[Parsable]
public sealed class Map : IComparable<Map>
public sealed class Map : IComparable<Map>, ISpanFormattable
{
public const int SectorSize = 16;
public const int SectorShift = 4;
@ -525,8 +525,29 @@ public sealed class Map : IComparable<Map>
return null;
}
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
{
if (destination.Length >= Name.Length)
{
Name.CopyTo(destination);
charsWritten = Name.Length;
return true;
}
charsWritten = 0;
return false;
}
public override string ToString() => Name;
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 int GetAverageZ(int x, int y)
{
GetAverageZ(x, y, out _, out var avg, out _);