diff --git a/VpSharp/src/Coordinates.Serialization.cs b/VpSharp/src/Coordinates.Serialization.cs index 0f0db35..47f8f9b 100644 --- a/VpSharp/src/Coordinates.Serialization.cs +++ b/VpSharp/src/Coordinates.Serialization.cs @@ -10,15 +10,15 @@ public readonly partial struct Coordinates { 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.Empty); + int count = Serialize(coordinates, format, formatProvider, Span.Empty); Span chars = stackalloc char[count]; - Serialize(coordinates, format, chars); + Serialize(coordinates, format, formatProvider, chars); return chars.ToString(); } - public static int Serialize(in Coordinates coordinates, string format, Span destination) + public static int Serialize(in Coordinates coordinates, string format, IFormatProvider? formatProvider, Span destination) { using Utf8ValueStringBuilder builder = ZString.CreateUtf8StringBuilder(); @@ -40,7 +40,7 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Z)); + builder.Append(string.Format(formatProvider, format, coordinates.Z)); builder.Append(' '); if (west) @@ -48,7 +48,7 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.X)); + builder.Append(string.Format(formatProvider, format, coordinates.X)); builder.Append(' '); if (up) @@ -56,7 +56,7 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Y)); + builder.Append(string.Format(formatProvider, format, coordinates.Y)); builder.Append("a "); if (dir) @@ -64,25 +64,25 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Yaw)); + builder.Append(string.Format(formatProvider, format, coordinates.Yaw)); } else { char zChar = north ? 'n' : 's'; 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(' '); - 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(' '); - builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Y)); + builder.Append(string.Format(formatProvider, format, coordinates.Y)); builder.Append("a "); - builder.Append(string.Format(CultureInfo.InvariantCulture, format, coordinates.Yaw)); + builder.Append(string.Format(formatProvider, format, coordinates.Yaw)); } ReadOnlySpan bytes = builder.AsSpan(); @@ -339,7 +339,7 @@ public readonly partial struct Coordinates { 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; }*/ diff --git a/VpSharp/src/Coordinates.cs b/VpSharp/src/Coordinates.cs index 456c843..adf6300 100644 --- a/VpSharp/src/Coordinates.cs +++ b/VpSharp/src/Coordinates.cs @@ -3,7 +3,7 @@ /// /// Represents a set of coordinates. /// -public readonly partial struct Coordinates : IEquatable +public readonly partial struct Coordinates : IEquatable, IFormattable { /// /// Initializes a new instance of the struct. @@ -195,9 +195,11 @@ public readonly partial struct Coordinates : IEquatable /// Returns the string representation of these coordinates. /// /// The format to apply to each component. + /// The format provider. /// A representation of these coordinates. - 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); } }