Skip to content

Commit

Permalink
C#: Implement IFormattable for Variant structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Repiteo committed Mar 11, 2024
1 parent 0ace0a1 commit 435f698
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 138 deletions.
18 changes: 11 additions & 7 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Aabb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Aabb : IEquatable<Aabb>
public struct Aabb : IEquatable<Aabb>, IFormattable
{
private Vector3 _position;
private Vector3 _size;
Expand Down Expand Up @@ -733,18 +733,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Aabb"/> to a string.
/// </summary>
/// <returns>A string representation of this AABB.</returns>
public override readonly string ToString()
{
return $"{_position}, {_size}";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Aabb"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this AABB.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Aabb"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this AABB.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
return $"{_position.ToString(format)}, {_size.ToString(format)}";
string separator = formatProvider.GetListSeparator();
return $"{_position.ToString(format, formatProvider)}{separator} {_size.ToString(format, formatProvider)}";
}
}
}
20 changes: 12 additions & 8 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Basis.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.ComponentModel;

#nullable enable

Expand All @@ -23,7 +23,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Basis : IEquatable<Basis>
public struct Basis : IEquatable<Basis>, IFormattable
{
// NOTE: x, y and z are public-only. Use Column0, Column1 and Column2 internally.

Expand Down Expand Up @@ -1134,18 +1134,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Basis"/> to a string.
/// </summary>
/// <returns>A string representation of this basis.</returns>
public override readonly string ToString()
{
return $"[X: {X}, Y: {Y}, Z: {Z}]";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Basis"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this basis.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Basis"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this basis.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
return $"[X: {X.ToString(format)}, Y: {Y.ToString(format)}, Z: {Z.ToString(format)}]";
string separator = formatProvider.GetListSeparator();
return $"[X: {X.ToString(format, formatProvider)}{separator} Y: {Y.ToString(format, formatProvider)}{separator} Z: {Z.ToString(format, formatProvider)}]";
}
}
}
21 changes: 11 additions & 10 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using Godot.NativeInterop;

Expand All @@ -20,7 +19,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Color : IEquatable<Color>
public struct Color : IEquatable<Color>, IFormattable
{
/// <summary>
/// The color's red component, typically on the range of 0 to 1.
Expand Down Expand Up @@ -1329,20 +1328,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Color"/> to a string.
/// </summary>
/// <returns>A string representation of this color.</returns>
public override readonly string ToString()
{
return $"({R}, {G}, {B}, {A})";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Color"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this color.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Color"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this color.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
#pragma warning disable CA1305 // Disable warning: "Specify IFormatProvider"
return $"({R.ToString(format)}, {G.ToString(format)}, {B.ToString(format)}, {A.ToString(format)})";
#pragma warning restore CA1305
string separator = formatProvider.GetListSeparator();
return $"({R.ToString(format, formatProvider)}{separator} {G.ToString(format, formatProvider)}{separator} {B.ToString(format, formatProvider)}{separator} {A.ToString(format, formatProvider)})";
}
}
}
20 changes: 11 additions & 9 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Plane : IEquatable<Plane>
public struct Plane : IEquatable<Plane>, IFormattable
{
private Vector3 _normal;
private real_t _d;
Expand Down Expand Up @@ -424,20 +424,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Plane"/> to a string.
/// </summary>
/// <returns>A string representation of this plane.</returns>
public override readonly string ToString()
{
return $"{_normal}, {_d}";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Plane"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this plane.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Plane"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this plane.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
#pragma warning disable CA1305 // Disable warning: "Specify IFormatProvider"
return $"{_normal.ToString(format)}, {_d.ToString(format)}";
#pragma warning restore CA1305
string separator = formatProvider.GetListSeparator();
return $"{_normal.ToString(format, formatProvider)}{separator} {_d.ToString(format, formatProvider)}";
}
}
}
26 changes: 14 additions & 12 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Projection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Projection : IEquatable<Projection>
public struct Projection : IEquatable<Projection>, IFormattable
{
/// <summary>
/// Enumerated index values for the planes.
Expand Down Expand Up @@ -1012,23 +1012,25 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Projection"/> to a string.
/// </summary>
/// <returns>A string representation of this projection.</returns>
public override readonly string ToString()
{
return $"{X.X}, {X.Y}, {X.Z}, {X.W}\n{Y.X}, {Y.Y}, {Y.Z}, {Y.W}\n{Z.X}, {Z.Y}, {Z.Z}, {Z.W}\n{W.X}, {W.Y}, {W.Z}, {W.W}\n";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Projection"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this projection.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Projection"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this projection.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
#pragma warning disable CA1305 // Disable warning: "Specify IFormatProvider"
return $"{X.X.ToString(format)}, {X.Y.ToString(format)}, {X.Z.ToString(format)}, {X.W.ToString(format)}\n" +
$"{Y.X.ToString(format)}, {Y.Y.ToString(format)}, {Y.Z.ToString(format)}, {Y.W.ToString(format)}\n" +
$"{Z.X.ToString(format)}, {Z.Y.ToString(format)}, {Z.Z.ToString(format)}, {Z.W.ToString(format)}\n" +
$"{W.X.ToString(format)}, {W.Y.ToString(format)}, {W.Z.ToString(format)}, {W.W.ToString(format)}\n";
#pragma warning restore CA1305
string separator = formatProvider.GetListSeparator();
return $"{X.X.ToString(format, formatProvider)}{separator} {X.Y.ToString(format, formatProvider)}{separator} {X.Z.ToString(format, formatProvider)}{separator} {X.W.ToString(format, formatProvider)}\n" +
$"{Y.X.ToString(format, formatProvider)}{separator} {Y.Y.ToString(format, formatProvider)}{separator} {Y.Z.ToString(format, formatProvider)}{separator} {Y.W.ToString(format, formatProvider)}\n" +
$"{Z.X.ToString(format, formatProvider)}{separator} {Z.Y.ToString(format, formatProvider)}{separator} {Z.Z.ToString(format, formatProvider)}{separator} {Z.W.ToString(format, formatProvider)}\n" +
$"{W.X.ToString(format, formatProvider)}{separator} {W.Y.ToString(format, formatProvider)}{separator} {W.Z.ToString(format, formatProvider)}{separator} {W.W.ToString(format, formatProvider)}\n";
}
}
}
20 changes: 11 additions & 9 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Quaternion : IEquatable<Quaternion>
public struct Quaternion : IEquatable<Quaternion>, IFormattable
{
/// <summary>
/// X component of the quaternion (imaginary <c>i</c> axis part).
Expand Down Expand Up @@ -811,20 +811,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Quaternion"/> to a string.
/// </summary>
/// <returns>A string representation of this quaternion.</returns>
public override readonly string ToString()
{
return $"({X}, {Y}, {Z}, {W})";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Quaternion"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this quaternion.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Quaternion"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this quaternion.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
#pragma warning disable CA1305 // Disable warning: "Specify IFormatProvider"
return $"({X.ToString(format)}, {Y.ToString(format)}, {Z.ToString(format)}, {W.ToString(format)})";
#pragma warning restore CA1305
string separator = formatProvider.GetListSeparator();
return $"({X.ToString(format, formatProvider)}{separator} {Y.ToString(format, formatProvider)}{separator} {Z.ToString(format, formatProvider)}{separator} {W.ToString(format, formatProvider)})";
}
}
}
18 changes: 11 additions & 7 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Rect2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Rect2 : IEquatable<Rect2>
public struct Rect2 : IEquatable<Rect2>, IFormattable
{
private Vector2 _position;
private Vector2 _size;
Expand Down Expand Up @@ -469,18 +469,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Rect2"/> to a string.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public override readonly string ToString()
{
return $"{_position}, {_size}";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Rect2"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Rect2"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
return $"{_position.ToString(format)}, {_size.ToString(format)}";
string separator = formatProvider.GetListSeparator();
return $"{_position.ToString(format, formatProvider)}{separator} {_size.ToString(format, formatProvider)}";
}
}
}
18 changes: 11 additions & 7 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Rect2I.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Godot
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Rect2I : IEquatable<Rect2I>
public struct Rect2I : IEquatable<Rect2I>, IFormattable
{
private Vector2I _position;
private Vector2I _size;
Expand Down Expand Up @@ -429,18 +429,22 @@ public override readonly int GetHashCode()
/// Converts this <see cref="Rect2I"/> to a string.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public override readonly string ToString()
{
return $"{_position}, {_size}";
}
public override readonly string ToString() => ToString(null, null);

/// <summary>
/// Converts this <see cref="Rect2I"/> to a string with the given <paramref name="format"/>.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public readonly string ToString(string? format)
public readonly string ToString(string? format) => ToString(format, null);

/// <summary>
/// Converts this <see cref="Rect2I"/> to a string with the given <paramref name="format"/> and <paramref name="formatProvider"/>.
/// </summary>
/// <returns>A string representation of this rect.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
return $"{_position.ToString(format)}, {_size.ToString(format)}";
string separator = formatProvider.GetListSeparator();
return $"{_position.ToString(format, formatProvider)}{separator} {_size.ToString(format, formatProvider)}";
}
}
}
26 changes: 26 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,5 +1877,31 @@ public static string XMLEscape(this string instance)
{
return SecurityElement.FromString(instance)?.Text;
}

/// <summary>
/// Returns the list separator of this <see cref="IFormatProvider"/> as a string.
/// </summary>
/// <param name="provider">The format provider to pull from.</param>
/// <returns>The list separator of this IFormatProvider, or the current culture's if
/// <paramref name="provider"/> is <see langword="null"/> or cannot be converted.</returns>
public static string GetListSeparator(this IFormatProvider? provider)
{
if (provider is CultureInfo cultureInfo)
{
return cultureInfo.TextInfo.ListSeparator;
}

if (provider is NumberFormatInfo numberFormatInfo)
{
if (numberFormatInfo == NumberFormatInfo.InvariantInfo)
{
return CultureInfo.InvariantCulture.TextInfo.ListSeparator;
}
// TODO: Figure out if there's a way of backtracing CultureInfo from NumberFormatInfo
return CultureInfo.CurrentCulture.TextInfo.ListSeparator;
}

return CultureInfo.CurrentCulture.TextInfo.ListSeparator;
}
}
}
Loading

0 comments on commit 435f698

Please sign in to comment.