From 2c3d31135e499321034f65b157f7b3f2db3c9f4a Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Sat, 27 Jan 2024 14:54:30 +0100 Subject: [PATCH 1/2] feat: Add static Format method --- src/Atc/Formatters/StringCaseFormatter.cs | 21 +++++++++++ .../Formatters/StringCaseFormatterTests.cs | 35 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Atc/Formatters/StringCaseFormatter.cs b/src/Atc/Formatters/StringCaseFormatter.cs index 03a62861..eabb2eb5 100644 --- a/src/Atc/Formatters/StringCaseFormatter.cs +++ b/src/Atc/Formatters/StringCaseFormatter.cs @@ -89,4 +89,25 @@ public string Format( _ => str, }; } + + /// + /// Formats the given arguments using the specified format string and the custom case formatting rules defined in StringCaseFormatter. + /// Each format item in the format string is replaced by the string representation of the corresponding object argument, formatted according to the custom case formatting rules. + /// + /// A composite format string that includes one or more format items, each of which corresponds to an object in the array. + /// An object array that contains zero or more objects to format and insert in the format string. + /// + /// A copy of in which the format items have been replaced by the string representation of the corresponding + /// objects in , formatted according to the custom case formatting rules. + /// + /// + /// + /// var result = StringCaseFormatter.Format("{0:U} {1:u} {2:L} {3:l}", "john", "dove", "HALLO", "WORLD"); + /// // Result: "JOHN Dove hallo wORLD" + /// + /// + public static string Format( + string format, + params object[] args) + => string.Format(Default, format, args); } \ No newline at end of file diff --git a/test/Atc.Tests/Formatters/StringCaseFormatterTests.cs b/test/Atc.Tests/Formatters/StringCaseFormatterTests.cs index d717e4d4..1f377d64 100644 --- a/test/Atc.Tests/Formatters/StringCaseFormatterTests.cs +++ b/test/Atc.Tests/Formatters/StringCaseFormatterTests.cs @@ -107,4 +107,39 @@ public void FormatStringForTwoParameters( => Assert.Equal( expected, string.Format(formatter, $"{{0:{formatSpecifier1}}} {{1:{formatSpecifier2}}}", input1, input2)); + + [Theory] + [InlineData("hallo world", "Hallo", "World", "L", "L")] + [InlineData("hallo world", "Hallo", "World", "L", "l")] + [InlineData("hallo world", "Hallo", "World", "l", "l")] + [InlineData("hallo world", "Hallo", "World", "l", "L")] + [InlineData("hallo world", "HALLO", "WORLD", "L", "L")] + [InlineData("hallo wORLD", "HALLO", "WORLD", "L", "l")] + [InlineData("hALLO wORLD", "HALLO", "WORLD", "l", "l")] + [InlineData("hALLO world", "HALLO", "WORLD", "l", "L")] + [InlineData("hallo world", "hallo", "world", "L", "L")] + [InlineData("hallo world", "hallo", "world", "L", "l")] + [InlineData("hallo world", "hallo", "world", "l", "l")] + [InlineData("hallo world", "hallo", "world", "l", "L")] + [InlineData("HALLO WORLD", "Hallo", "World", "U", "U")] + [InlineData("HALLO World", "Hallo", "World", "U", "u")] + [InlineData("Hallo World", "Hallo", "World", "u", "u")] + [InlineData("Hallo WORLD", "Hallo", "World", "u", "U")] + [InlineData("HALLO WORLD", "HALLO", "WORLD", "U", "U")] + [InlineData("HALLO WORLD", "HALLO", "WORLD", "U", "u")] + [InlineData("HALLO WORLD", "HALLO", "WORLD", "u", "u")] + [InlineData("HALLO WORLD", "HALLO", "WORLD", "u", "U")] + [InlineData("HALLO WORLD", "hallo", "world", "U", "U")] + [InlineData("HALLO World", "hallo", "world", "U", "u")] + [InlineData("Hallo World", "hallo", "world", "u", "u")] + [InlineData("Hallo WORLD", "hallo", "world", "u", "U")] + public void StaticFormatStringForTwoParameters( + string expected, + string input1, + string input2, + string formatSpecifier1, + string formatSpecifier2) + => Assert.Equal( + expected, + StringCaseFormatter.Format($"{{0:{formatSpecifier1}}} {{1:{formatSpecifier2}}}", input1, input2)); } \ No newline at end of file From 9a1d4240d9eb052406875965777a4b559853283a Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Sat, 27 Jan 2024 14:56:02 +0100 Subject: [PATCH 2/2] docs: Update MD --- docs/CodeDoc/Atc/Atc.md | 30 +++++++++++++++++++++++ docs/CodeDoc/Atc/IndexExtended.md | 2 ++ src/Atc/Formatters/StringCaseFormatter.cs | 21 ++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/docs/CodeDoc/Atc/Atc.md b/docs/CodeDoc/Atc/Atc.md index 77376c07..bb167a31 100644 --- a/docs/CodeDoc/Atc/Atc.md +++ b/docs/CodeDoc/Atc/Atc.md @@ -1160,16 +1160,46 @@ Provides custom string formatting based on specified case formatting options.
StringCaseFormatter Default >``` >Summary: Static `Atc.StringCaseFormatter` using `System.Globalization.CultureInfo.CurrentCulture`. +### Static Methods + +#### Format +>```csharp +>string Format(string format, object[] args) +>``` +>Summary: Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information. +> +>Parameters:
+>     `format`  -  A format string containing formatting specifications.
+>     `arg`  -  The object to format.
+>     `formatProvider`  -  An object that supplies format information about the current instance. + This parameter is ignored in this implementation.
+> +>Returns: The string representation of the value of `arg`, formatted as specified by `format` and `formatProvider`. ### Methods #### Format >```csharp >string Format(string format, object arg, IFormatProvider formatProvider) >``` +>Summary: Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information. +> +>Parameters:
+>     `format`  -  A format string containing formatting specifications.
+>     `arg`  -  The object to format.
+>     `formatProvider`  -  An object that supplies format information about the current instance. + This parameter is ignored in this implementation.
+> +>Returns: The string representation of the value of `arg`, formatted as specified by `format` and `formatProvider`. #### GetFormat >```csharp >object GetFormat(Type formatType) >``` +>Summary: Returns an object that provides formatting services for the specified type. +> +>Parameters:
+>     `formatType`  -  An object that specifies the type of format object to return.
+> +>Returns: An instance of the object specified by `formatType`, if the `System.ICustomFormatter` interface is requested; otherwise, null.
diff --git a/docs/CodeDoc/Atc/IndexExtended.md b/docs/CodeDoc/Atc/IndexExtended.md index 841a1910..0e0fd6e3 100644 --- a/docs/CodeDoc/Atc/IndexExtended.md +++ b/docs/CodeDoc/Atc/IndexExtended.md @@ -121,6 +121,8 @@ - [StringCaseFormatter](Atc.md#stringcaseformatter) - Static Fields - StringCaseFormatter Default + - Static Methods + - Format(string format, object[] args) - Methods - Format(string format, object arg, IFormatProvider formatProvider) - GetFormat(Type formatType) diff --git a/src/Atc/Formatters/StringCaseFormatter.cs b/src/Atc/Formatters/StringCaseFormatter.cs index eabb2eb5..66717c00 100644 --- a/src/Atc/Formatters/StringCaseFormatter.cs +++ b/src/Atc/Formatters/StringCaseFormatter.cs @@ -1,4 +1,5 @@ // ReSharper disable once CheckNamespace +// ReSharper disable CommentTypo namespace Atc; /// @@ -51,11 +52,31 @@ public sealed class StringCaseFormatter : IFormatProvider, ICustomFormatter /// public static readonly StringCaseFormatter Default = new(); + /// + /// Returns an object that provides formatting services for the specified type. + /// + /// An object that specifies the type of format object to return. + /// + /// An instance of the object specified by , + /// if the interface is requested; otherwise, null. + /// public object? GetFormat(Type? formatType) => formatType == typeof(ICustomFormatter) ? this : null; + /// + /// Converts the value of a specified object to an equivalent string representation + /// using specified format and culture-specific formatting information. + /// + /// A format string containing formatting specifications. + /// The object to format. + /// An object that supplies format information about the current instance. + /// This parameter is ignored in this implementation. + /// + /// The string representation of the value of , formatted as + /// specified by and . + /// public string Format( string? format, object? arg,