Skip to content

Commit

Permalink
Extend InvariantCultureExtensions and add CurrentCultureExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
skrysmanski committed Mar 24, 2024
1 parent a1af259 commit be47fa9
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion _ProjectCommons
77 changes: 77 additions & 0 deletions src/AppMotor.Core/Extensions/CurrentCultureExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
// Copyright AppMotor Framework (https://github.com/skrysmanski/AppMotor)

using System.Globalization;
using System.Text;

using AppMotor.Core.Globalization;

using JetBrains.Annotations;

namespace AppMotor.Core.Extensions;

/// <summary>
/// Extension methods that format data with the current UI culture (<see cref="UICulture.FormatsAndSorting"/>).
/// </summary>
/// <seealso cref="InvariantCultureExtensions"/>
/// <seealso cref="UICulture"/>
public static class CurrentCultureExtensions
{
/// <summary>
/// Converts this value into a string with <see cref="UICulture.FormatsAndSorting"/>.
/// </summary>
[PublicAPI, MustUseReturnValue]
public static string ToStringCC<T>(this T value) where T : IConvertible
{
return value.ToString(CultureInfo.InvariantCulture);
}

/// <summary>
/// Converts this value into a string with <see cref="UICulture.FormatsAndSorting"/>.
/// </summary>
[PublicAPI, MustUseReturnValue]
public static string ToStringCC<T>(this T value, string format) where T : IFormattable
{
return value.ToString(format, UICulture.FormatsAndSorting);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with <see cref="UICulture.FormatsAndSorting"/>.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatCC(this StringBuilder stringBuilder, string format, object? arg0)
{
return stringBuilder.AppendFormat(UICulture.FormatsAndSorting, format, arg0);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with <see cref="UICulture.FormatsAndSorting"/>.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatCC(this StringBuilder stringBuilder, string format, object? arg0, object? arg1)
{
return stringBuilder.AppendFormat(UICulture.FormatsAndSorting, format, arg0, arg1);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with <see cref="UICulture.FormatsAndSorting"/>.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatCC(this StringBuilder stringBuilder, string format, object? arg0, object? arg1, object? arg2)
{
return stringBuilder.AppendFormat(UICulture.FormatsAndSorting, format, arg0, arg1, arg2);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with <see cref="UICulture.FormatsAndSorting"/>.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatCC(this StringBuilder stringBuilder, string format, params object?[] args)
{
return stringBuilder.AppendFormat(UICulture.FormatsAndSorting, format, args);
}
}
50 changes: 46 additions & 4 deletions src/AppMotor.Core/Extensions/InvariantCultureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

using System.ComponentModel;
using System.Globalization;
using System.Text;

using JetBrains.Annotations;

namespace AppMotor.Core.Extensions;

/// <summary>
/// Provides methods that format things with <see cref="CultureInfo.InvariantCulture"/>.
/// Extension methods that format data with <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
/// <seealso cref="CurrentCultureExtensions"/>
public static class InvariantCultureExtensions
{
/// <summary>
/// Convenience method for calling <c>string.Format()</c> with <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
/// <seealso cref="StringExtensions.With"/>
[PublicAPI, MustUseReturnValue]
[StringFormatMethod("message")]
[MustUseReturnValue]
public static string WithIC([Localizable(false)] this string message, params object[] args)
{
return string.Format(CultureInfo.InvariantCulture, message, args);
Expand All @@ -27,7 +29,7 @@ public static string WithIC([Localizable(false)] this string message, params obj
/// <summary>
/// Returns this values as string with invariant culture formatting.
/// </summary>
[MustUseReturnValue]
[PublicAPI, MustUseReturnValue]
public static string ToStringIC<T>(this T value) where T : IConvertible
{
return value.ToString(CultureInfo.InvariantCulture);
Expand All @@ -36,9 +38,49 @@ public static string ToStringIC<T>(this T value) where T : IConvertible
/// <summary>
/// Returns this values as string with invariant culture formatting.
/// </summary>
[MustUseReturnValue]
[PublicAPI, MustUseReturnValue]
public static string ToStringIC<T>(this T value, string format) where T : IFormattable
{
return value.ToString(format, CultureInfo.InvariantCulture);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with invariant culture formatting.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatIC(this StringBuilder stringBuilder, string format, object? arg0)
{
return stringBuilder.AppendFormat(CultureInfo.InvariantCulture, format, arg0);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with invariant culture formatting.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatIC(this StringBuilder stringBuilder, string format, object? arg0, object? arg1)
{
return stringBuilder.AppendFormat(CultureInfo.InvariantCulture, format, arg0, arg1);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with invariant culture formatting.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatIC(this StringBuilder stringBuilder, string format, object? arg0, object? arg1, object? arg2)
{
return stringBuilder.AppendFormat(CultureInfo.InvariantCulture, format, arg0, arg1, arg2);
}

/// <summary>
/// Convenience method for calling <c>StringBuilder.AppendFormat()</c> with invariant culture formatting.
/// </summary>
[PublicAPI]
[StringFormatMethod("format")]
public static StringBuilder AppendFormatIC(this StringBuilder stringBuilder, string format, params object?[] args)
{
return stringBuilder.AppendFormat(CultureInfo.InvariantCulture, format, args);
}
}

0 comments on commit be47fa9

Please sign in to comment.