1
0
mirror of https://github.com/oliverbooth/VpSharp synced 2024-11-22 19:18:47 +00:00

Accept IFormatProvider in Coordinates.ToString

This commit is contained in:
Oliver Booth 2022-12-08 15:52:22 +00:00
parent 342820b444
commit 6804959f93
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 18 additions and 16 deletions

View File

@ -10,15 +10,15 @@ public readonly partial struct Coordinates
{ {
private static class Serializer private static class Serializer
{ {
public static string Serialize(in Coordinates coordinates, string format) public static string Serialize(in Coordinates coordinates, string format, IFormatProvider? formatProvider)
{ {
int count = Serialize(coordinates, format, Span<char>.Empty); int count = Serialize(coordinates, format, formatProvider, Span<char>.Empty);
Span<char> chars = stackalloc char[count]; Span<char> chars = stackalloc char[count];
Serialize(coordinates, format, chars); Serialize(coordinates, format, formatProvider, chars);
return chars.ToString(); return chars.ToString();
} }
public static int Serialize(in Coordinates coordinates, string format, Span<char> destination) public static int Serialize(in Coordinates coordinates, string format, IFormatProvider? formatProvider, Span<char> destination)
{ {
using Utf8ValueStringBuilder builder = ZString.CreateUtf8StringBuilder(); using Utf8ValueStringBuilder builder = ZString.CreateUtf8StringBuilder();
@ -40,7 +40,7 @@ public readonly partial struct Coordinates
builder.Append('+'); builder.Append('+');
} }
builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Z)); builder.Append(string.Format(formatProvider, format, coordinates.Z));
builder.Append(' '); builder.Append(' ');
if (west) if (west)
@ -48,7 +48,7 @@ public readonly partial struct Coordinates
builder.Append('+'); builder.Append('+');
} }
builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.X)); builder.Append(string.Format(formatProvider, format, coordinates.X));
builder.Append(' '); builder.Append(' ');
if (up) if (up)
@ -56,7 +56,7 @@ public readonly partial struct Coordinates
builder.Append('+'); builder.Append('+');
} }
builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Y)); builder.Append(string.Format(formatProvider, format, coordinates.Y));
builder.Append("a "); builder.Append("a ");
if (dir) if (dir)
@ -64,25 +64,25 @@ public readonly partial struct Coordinates
builder.Append('+'); builder.Append('+');
} }
builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Yaw)); builder.Append(string.Format(formatProvider, format, coordinates.Yaw));
} }
else else
{ {
char zChar = north ? 'n' : 's'; char zChar = north ? 'n' : 's';
char xChar = west ? 'w' : 'e'; char xChar = west ? 'w' : 'e';
builder.Append(string.Format(CultureInfo.InvariantCulture, format, Math.Abs(coordinates.Z))); builder.Append(string.Format(formatProvider, format, Math.Abs(coordinates.Z)));
builder.Append(zChar); builder.Append(zChar);
builder.Append(' '); builder.Append(' ');
builder.Append(string.Format(CultureInfo.InvariantCulture, format, Math.Abs(coordinates.X))); builder.Append(string.Format(formatProvider, format, Math.Abs(coordinates.X)));
builder.Append(xChar); builder.Append(xChar);
builder.Append(' '); builder.Append(' ');
builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Y)); builder.Append(string.Format(formatProvider, format, coordinates.Y));
builder.Append("a "); builder.Append("a ");
builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Yaw)); builder.Append(string.Format(formatProvider, format, coordinates.Yaw));
} }
ReadOnlySpan<byte> bytes = builder.AsSpan(); ReadOnlySpan<byte> bytes = builder.AsSpan();
@ -339,7 +339,7 @@ public readonly partial struct Coordinates
{ {
y = value; y = value;
} }
else if (isAt3 && double.TryParse(chars, NumberStyles.Float, CultureInfo.InvariantCulture, out value)) else if (isAt3 && double.TryParse(chars, NumberStyles.Float, formatProvider, out value))
{ {
yaw = value; yaw = value;
}*/ }*/

View File

@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// Represents a set of coordinates. /// Represents a set of coordinates.
/// </summary> /// </summary>
public readonly partial struct Coordinates : IEquatable<Coordinates> public readonly partial struct Coordinates : IEquatable<Coordinates>, IFormattable
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Coordinates" /> struct. /// Initializes a new instance of the <see cref="Coordinates" /> struct.
@ -195,9 +195,11 @@ public readonly partial struct Coordinates : IEquatable<Coordinates>
/// Returns the string representation of these coordinates. /// Returns the string representation of these coordinates.
/// </summary> /// </summary>
/// <param name="format">The format to apply to each component.</param> /// <param name="format">The format to apply to each component.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>A <see cref="string" /> representation of these coordinates.</returns> /// <returns>A <see cref="string" /> representation of these coordinates.</returns>
public string ToString(string format) public string ToString(string? format, IFormatProvider? formatProvider = null)
{ {
return Serializer.Serialize(this, format); format ??= "{0}";
return Serializer.Serialize(this, format, formatProvider);
} }
} }