From e3a278f70b1884a1a3ca87c669e9ddbf96ffb542 Mon Sep 17 00:00:00 2001 From: Patrick Strube Date: Mon, 11 Mar 2024 22:39:50 -0700 Subject: [PATCH] Random case/spongetext implementation --- .../dev/DevToys.Tools/Helpers/StringHelper.cs | 28 +++++++++++++++++++ .../AnalyzerAndUtilities.Designer.cs | 9 ++++++ .../AnalyzerAndUtilities.resx | 3 ++ .../AnalyzerAndUtilitiesCommandLineTool.cs | 3 ++ .../AnalyzerAndUtilitiesGuiTool.cs | 8 +++++- .../Tools/Helpers/StringHelperTests.cs | 9 ++++++ 6 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/app/dev/DevToys.Tools/Helpers/StringHelper.cs b/src/app/dev/DevToys.Tools/Helpers/StringHelper.cs index 48e0ac2981..cbc299c328 100644 --- a/src/app/dev/DevToys.Tools/Helpers/StringHelper.cs +++ b/src/app/dev/DevToys.Tools/Helpers/StringHelper.cs @@ -568,6 +568,34 @@ internal static string ConvertToInverseCase(string text, CancellationToken cance return new string(inverseCaseString.Span); } + internal static string ConvertToRandomCase(string text, CancellationToken cancellationToken) + { + Guard.IsNotNull(text); + if (text.Length == 0) + { + return text; + } + + var randomCaseString = new Memory(text.ToCharArray()); + + for (int i = 0; i < randomCaseString.Length; i++) + { + cancellationToken.ThrowIfCancellationRequested(); + bool isUpper = random.Next() > (int.MaxValue / 2); + + if (isUpper) + { + randomCaseString.Span[i] = char.ToUpperInvariant(randomCaseString.Span[i]); + } + else + { + randomCaseString.Span[i] = char.ToLowerInvariant(randomCaseString.Span[i]); + } + } + + return new string(randomCaseString.Span); + } + internal static EndOfLineSequence DetectLineBreakKind(string text, CancellationToken cancellationToken) { EndOfLineSequence overallLineBreakType = EndOfLineSequence.Unknown; diff --git a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.Designer.cs index a4425e1ab5..ea538ecc62 100644 --- a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.Designer.cs +++ b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.Designer.cs @@ -339,6 +339,15 @@ internal static string PascalCase { } } + /// + /// Looks up a localized string similar to raNdoM cASe. + /// + internal static string RandomCase { + get { + return ResourceManager.GetString("RandomCase", resourceCulture); + } + } + /// /// Looks up a localized string similar to Randomize. /// diff --git a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.resx b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.resx index 109d45f131..73ecae1eff 100644 --- a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.resx +++ b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilities.resx @@ -210,6 +210,9 @@ PascalCase + + raNdoM cASe + Randomize diff --git a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesCommandLineTool.cs index 97a7267ef6..f4fa16a16e 100644 --- a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesCommandLineTool.cs +++ b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesCommandLineTool.cs @@ -30,6 +30,7 @@ private enum OperationType TrainCase, AlternatingCase, InverseCase, + RandomCase, SortLinesAlphabetically, SortLinesAlphabeticallyDescending, SortLinesByLastWord, @@ -118,6 +119,8 @@ public async ValueTask InvokeAsync(ILogger logger, CancellationToken cancel => StringHelper.ConvertToAlternatingCase(newText, cancellationToken), OperationType.InverseCase => StringHelper.ConvertToInverseCase(newText, cancellationToken), + OperationType.RandomCase + => StringHelper.ConvertToRandomCase(newText, cancellationToken), OperationType.SortLinesAlphabetically => StringHelper.SortLinesAlphabetically(newText, StringHelper.DetectLineBreakKind(newText, cancellationToken)), OperationType.SortLinesAlphabeticallyDescending diff --git a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesGuiTool.cs index 8ba1b00bb0..348e7ba649 100644 --- a/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesGuiTool.cs +++ b/src/app/dev/DevToys.Tools/Tools/Text/AnalyzerAndUtilities/AnalyzerAndUtilitiesGuiTool.cs @@ -123,7 +123,8 @@ public UIToolView View Button().Text(AnalyzerAndUtilities.CobolCase).OnClick(OnCobolCaseButtonClick), Button().Text(AnalyzerAndUtilities.TrainCase).OnClick(OnTrainCaseButtonClick), Button().Text(AnalyzerAndUtilities.AlternatingCase).OnClick(OnAlternatingCaseButtonClick), - Button().Text(AnalyzerAndUtilities.InverseCase).OnClick(OnInverseCaseButtonClick))), + Button().Text(AnalyzerAndUtilities.InverseCase).OnClick(OnInverseCaseButtonClick), + Button().Text(AnalyzerAndUtilities.RandomCase).OnClick(OnRandomCaseButtonClick))), // Sort lines Stack() @@ -291,6 +292,11 @@ private void OnInverseCaseButtonClick() SetTextWithoutBackup(StringHelper.ConvertToInverseCase(_textInput.Text, CancellationToken.None)); } + private void OnRandomCaseButtonClick() + { + SetTextWithoutBackup(StringHelper.ConvertToRandomCase(_textInput.Text, CancellationToken.None)); + } + private void OnAlphabetizeButtonClick() { SetTextWithoutBackup(StringHelper.SortLinesAlphabetically(_textInput.Text, _statisticEndOfLineSequence)); diff --git a/src/app/tests/DevToys.UnitTests/Tools/Helpers/StringHelperTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Helpers/StringHelperTests.cs index c50c923241..e1b94ebb85 100644 --- a/src/app/tests/DevToys.UnitTests/Tools/Helpers/StringHelperTests.cs +++ b/src/app/tests/DevToys.UnitTests/Tools/Helpers/StringHelperTests.cs @@ -165,6 +165,15 @@ internal void InverseCase() .Be("LoReM IpSuM DoLoR SiT AmEt tAkImAtA SiT.\r\n pLaCeRaT SeD DuIs cLiTa nOnUmY TiNcIdUnT EsT FaCiLiSiS.\r CuM Ea eLiTr cOnSeCtEtUeR NoNuMy dIaM.\n EoS Ut dIaM LaOrEeT AmEt mInIm dOlOr dOlOrEs dOlOrE AmEt lOrEm cOnSeTeTuR DoLoR ClItA."); } + [Fact] + internal void RandomCase() + { + string result = StringHelper.ConvertToRandomCase(Text, CancellationToken.None); + result + .Should() + .NotBe(Text); + } + [Theory] [InlineData("", EndOfLineSequence.Unknown)] [InlineData(Text, EndOfLineSequence.Mixed)]