Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random case/spongetext case converter implementation #1080

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/app/dev/DevToys.Tools/Helpers/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(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;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@
<data name="PascalCase" xml:space="preserve">
<value>PascalCase</value>
</data>
<data name="RandomCase" xml:space="preserve">
<value>raNdoM cASe</value>
</data>
<data name="Randomize" xml:space="preserve">
<value>Randomize</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private enum OperationType
TrainCase,
AlternatingCase,
InverseCase,
RandomCase,
SortLinesAlphabetically,
SortLinesAlphabeticallyDescending,
SortLinesByLastWord,
Expand Down Expand Up @@ -118,6 +119,8 @@ public async ValueTask<int> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down