From 90211f1647d01670f16956f50e94e806e179811d Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 8 Dec 2022 16:28:42 +0000 Subject: [PATCH] Fix formatting of Coordinates and Rotation --- VpSharp/src/Coordinates.Serialization.cs | 21 +++++++++++++-------- VpSharp/src/Coordinates.cs | 4 ++-- VpSharp/src/Rotation.cs | 8 ++++---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/VpSharp/src/Coordinates.Serialization.cs b/VpSharp/src/Coordinates.Serialization.cs index 47f8f9b..b0f4170 100644 --- a/VpSharp/src/Coordinates.Serialization.cs +++ b/VpSharp/src/Coordinates.Serialization.cs @@ -18,7 +18,12 @@ public readonly partial struct Coordinates return chars.ToString(); } - public static int Serialize(in Coordinates coordinates, string format, IFormatProvider? formatProvider, Span destination) + public static int Serialize( + in Coordinates coordinates, + string? format, + IFormatProvider? formatProvider, + Span destination + ) { using Utf8ValueStringBuilder builder = ZString.CreateUtf8StringBuilder(); @@ -40,7 +45,7 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(formatProvider, format, coordinates.Z)); + builder.Append(coordinates.Z.ToString(format, formatProvider)); builder.Append(' '); if (west) @@ -48,7 +53,7 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(formatProvider, format, coordinates.X)); + builder.Append(coordinates.X.ToString(format, formatProvider)); builder.Append(' '); if (up) @@ -56,7 +61,7 @@ public readonly partial struct Coordinates builder.Append('+'); } - builder.Append(string.Format(formatProvider, format, coordinates.Y)); + builder.Append(coordinates.Y.ToString(format, formatProvider)); builder.Append("a "); if (dir) @@ -71,18 +76,18 @@ public readonly partial struct Coordinates char zChar = north ? 'n' : 's'; char xChar = west ? 'w' : 'e'; - builder.Append(string.Format(formatProvider, format, Math.Abs(coordinates.Z))); + builder.Append(Math.Abs(coordinates.Z).ToString(format, formatProvider)); builder.Append(zChar); builder.Append(' '); - builder.Append(string.Format(formatProvider, format, Math.Abs(coordinates.X))); + builder.Append(Math.Abs(coordinates.X).ToString(format, formatProvider)); builder.Append(xChar); builder.Append(' '); - builder.Append(string.Format(formatProvider, format, coordinates.Y)); + builder.Append(Math.Abs(coordinates.Y).ToString(format, formatProvider)); builder.Append("a "); - builder.Append(string.Format(formatProvider, format, coordinates.Yaw)); + builder.Append(Math.Abs(coordinates.Yaw).ToString(format, formatProvider)); } ReadOnlySpan bytes = builder.AsSpan(); diff --git a/VpSharp/src/Coordinates.cs b/VpSharp/src/Coordinates.cs index 24e92d5..71daab6 100644 --- a/VpSharp/src/Coordinates.cs +++ b/VpSharp/src/Coordinates.cs @@ -190,7 +190,7 @@ public readonly partial struct Coordinates : IEquatable, IFormattab /// A representation of these coordinates. public override string ToString() { - return ToString("{0}", CultureInfo.InvariantCulture); + return ToString("F", CultureInfo.InvariantCulture); } /// @@ -202,6 +202,6 @@ public readonly partial struct Coordinates : IEquatable, IFormattab public string ToString(string? format, IFormatProvider? formatProvider = null) { format ??= "{0}"; - return Serializer.Serialize(this, format, formatProvider); + return Serializer.Serialize(this, format, formatProvider ?? CultureInfo.InvariantCulture); } } diff --git a/VpSharp/src/Rotation.cs b/VpSharp/src/Rotation.cs index 5584523..ca756f6 100644 --- a/VpSharp/src/Rotation.cs +++ b/VpSharp/src/Rotation.cs @@ -178,7 +178,7 @@ public readonly struct Rotation : IEquatable, IFormattable /// A representation of these coordinates. public override string ToString() { - return ToString("{0}", CultureInfo.CurrentCulture); + return ToString("F", CultureInfo.CurrentCulture); } /// @@ -194,13 +194,13 @@ public readonly struct Rotation : IEquatable, IFormattable using var builder = ZString.CreateUtf8StringBuilder(); builder.Append('<'); - builder.Append(string.Format(formatProvider, format, Tilt)); + builder.Append(Tilt.ToString(format, formatProvider)); builder.Append(separator); builder.Append(' '); - builder.Append(string.Format(formatProvider, format, Yaw)); + builder.Append(Yaw.ToString(format, formatProvider)); builder.Append(separator); builder.Append(' '); - builder.Append(string.Format(formatProvider, format, Roll)); + builder.Append(Roll.ToString(format, formatProvider)); builder.Append('>'); return builder.ToString();