Skip to content

Commit

Permalink
Transform with Culture via ICulturedStringTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed May 18, 2021
1 parent 70e6746 commit 31c9dae
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\DictionaryExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\Internals\CollectionViewExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\Internals\EnumeratorWithIndex.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\Internals\Transformer\ICulturedStringTransformer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\MergedDictionariesExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\Internals\Transformer\IStringTransformer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\Internals\Transformer\To.cs" />
Expand Down Expand Up @@ -1108,7 +1109,6 @@
<Folder Include="$(MSBuildThisFileDirectory)Data\Info\" />
<Folder Include="$(MSBuildThisFileDirectory)HandyControls\Controls\Persian\" />
<Folder Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Command\Internals\DelegateCommand\" />
<Folder Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Extension\Internals\" />
<Folder Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Logging\Internals\" />
<Folder Include="$(MSBuildThisFileDirectory)HandyControls\Tools\Markup\Internals\" />
<Folder Include="$(MSBuildThisFileDirectory)Tools\Interop\Handle\" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Globalization;
namespace HandyControl.Tools.Extension
{
public interface ICulturedStringTransformer : IStringTransformer
{
/// <summary>
/// Transform the input
/// </summary>
/// <param name="input">String to be transformed</param>
/// <param name="culture">The culture</param>
/// <returns></returns>
string Transform(string input, CultureInfo culture);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
namespace HandyControl.Tools.Extension
using System.Globalization;
using System.Linq;

namespace HandyControl.Tools.Extension
{
/// <summary>
/// A portal to string transformation using IStringTransformer
/// </summary>
public static class To
{
/// <summary>
/// Transforms a string using the provided transformers. Transformations are applied in the provided order.
/// </summary>
/// <param name="input"></param>
/// <param name="transformers"></param>
/// <returns></returns>
public static string Transform(this string input, params IStringTransformer[] transformers)
{
return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current));
}

/// <summary>
/// Transforms a string using the provided transformers. Transformations are applied in the provided order.
/// </summary>
/// <param name="input"></param>
/// <param name="culture"></param>
/// <param name="transformers"></param>
/// <returns></returns>
public static string Transform(this string input, CultureInfo culture, params ICulturedStringTransformer[] transformers)
{
return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current, culture));
}

/// <summary>
/// Changes string to title case
/// </summary>
/// <example>
/// "INvalid caSEs arE corrected" -> "Invalid Cases Are Corrected"
/// </example>
public static IStringTransformer TitleCase
public static ICulturedStringTransformer TitleCase
{
get
{
Expand All @@ -25,7 +48,7 @@ public static IStringTransformer TitleCase
/// <example>
/// "Sentence casing" -> "sentence casing"
/// </example>
public static IStringTransformer LowerCase
public static ICulturedStringTransformer LowerCase
{
get
{
Expand All @@ -39,7 +62,7 @@ public static IStringTransformer LowerCase
/// <example>
/// "lower case statement" -> "LOWER CASE STATEMENT"
/// </example>
public static IStringTransformer UpperCase
public static ICulturedStringTransformer UpperCase
{
get
{
Expand All @@ -53,7 +76,7 @@ public static IStringTransformer UpperCase
/// <example>
/// "lower case statement" -> "Lower case statement"
/// </example>
public static IStringTransformer SentenceCase
public static ICulturedStringTransformer SentenceCase
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace HandyControl.Tools.Extension
{
internal class ToLowerCase : IStringTransformer
internal class ToLowerCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return CultureInfo.CurrentCulture.TextInfo.ToLower(input);
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

return culture.TextInfo.ToLower(input);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
namespace HandyControl.Tools.Extension
using System.Globalization;

namespace HandyControl.Tools.Extension
{
internal class ToSentenceCase : IStringTransformer
internal class ToSentenceCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

if (input.Length >= 1)
{
return string.Concat(input.Substring(0, 1).ToUpper(), input.Substring(1));
return culture.TextInfo.ToUpper(input[0]) + input.Substring(1);
}

return input.ToUpper();
return culture.TextInfo.ToUpper(input);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
using System.Linq;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

namespace HandyControl.Tools.Extension
{
internal class ToTitleCase : IStringTransformer
internal class ToTitleCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

var result = input;
var matches = Regex.Matches(input, @"(\w|[^\u0000-\u007F])+'?\w*");
foreach (Match word in matches)
{
if (!AllCapitals(word.Value))
{
result = ReplaceWithTitleCase(word, result);
result = ReplaceWithTitleCase(word, result, culture);
}
}

Expand All @@ -25,10 +33,10 @@ private static bool AllCapitals(string input)
return input.ToCharArray().All(char.IsUpper);
}

private static string ReplaceWithTitleCase(Match word, string source)
private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture)
{
var wordToConvert = word.Value;
var replacement = char.ToUpper(wordToConvert[0]) + wordToConvert.Remove(0, 1).ToLower();
var replacement = culture.TextInfo.ToUpper(wordToConvert[0]) + culture.TextInfo.ToLower(wordToConvert.Remove(0, 1));
return source.Substring(0, word.Index) + replacement + source.Substring(word.Index + word.Length);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
namespace HandyControl.Tools.Extension
using System.Globalization;

namespace HandyControl.Tools.Extension
{
internal class ToUpperCase : IStringTransformer
internal class ToUpperCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return input.ToUpper();
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

return culture.TextInfo.ToUpper(input);
}
}
}

0 comments on commit 31c9dae

Please sign in to comment.