Skip to content

Commit

Permalink
Add GUID correction method
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Oct 28, 2024
1 parent 6c7831d commit d2b3f33
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
Binary file added Tests/Images/GUIDs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 34 additions & 1 deletion Tests/StringMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,37 @@ public void TestLimitEachLine(string inputString, string expected, int charLimit
{
Assert.Equal(expected, inputString.LimitCharactersPerLine(charLimit, spotInLine));
}
}

private string actualGuids = """

Check warning on line 415 in Tests/StringMethodTests.cs

View workflow job for this annotation

GitHub Actions / build

The field 'StringMethodTests.actualGuids' is assigned but its value is never used
97a56312-d8e8-4ca5-87fa-18e35266d31e
bdc5a5f2-d6ff-403d-a632-f9006387e149
aeef14aa-9aff-4f0d-8ca5-e5df1b399c20
c702f24c-e51b-4ebd-bb45-56df08266e80
a87f9201-a046-425d-b92b-b488667a5d92
bc656414-4f2a-4219-b763-810632a535e2
11c5ecc4-0c0a-4606-a54f-16df976637d1
5cec5cc9-782d-47aa-bff3-c84e13a81604
8501db7b-ee04-4fb2-8516-f5e2f0bc71bf
8da03c16-6d3f-4750-831b-c3866af85551
03d82c33-489c-41b2-8222-cc489d00b1bf
edf3b5ee-658e-41ea-8f7a-494a07beb322
4418874a-30c7-4a16-aba5-0f2a0c49b4f9
d4144186-4fad-40a0-bda9-7e3a2ea58a48
486d81d0-0d56-466b-856c-0bc37e897b7b
935155d5-1a96-4901-8b7d-23854fceb32d
ff826fac-d166-441e-8040-05218989e805
0a4ed755-f236-4e10-8b0b-592a527bb560
9be83ad8-5e2d-4e37-a9f5-9b728cd9b934
926ef504-264d-4762-b781-8813156eaa86
""";


[Theory]
[InlineData("g7a56312-d8e8-4ca5-87fa-18e3S266d3le", "97a56312-d8e8-4ca5-87fa-18e35266d31e")]
[InlineData("g7a56312-d8e 8-4ca5-87fa-18e3S2 66d3le", "97a56312-d8e8-4ca5-87fa-18e35266d31e")]
[InlineData("g7a56312-\r\nd8e8\r\n-4ca5-87fa-18e3S266d3le", "97a56312-d8e8-4ca5-87fa-18e35266d31e")]
public void TestGuidCorrections(string input, string expected)
{
Assert.Equal(expected, input.CorrectCommonGuidErrors());
}
}
19 changes: 19 additions & 0 deletions Text-Grab/Utilities/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public static class StringMethods
{'b', '6'}, {'z', '2'}, {'Z', '2'}
};

public static readonly Dictionary<char, char> GuidCorrections = new()
{
{'o', '0'}, {'O', '0'}, {'i', '1'}, {'l', '1'}, {'I', '1'},
{'h', '4'}, {'z', '2'}, {'Z', '2'}, {'g', '9'}, {'G', '9'},
{'s', '5'}, {'S', '5'}, {'Ø', '0'}, {'#', 'f'}, {'@', '0'},
{'Q', '0'}, {'¥', 'f'}, {'£', 'f'}, {'/', '7'}
};

public static string ReplaceWithDictionary(this string str, Dictionary<char, char> dict)
{
StringBuilder sb = new();
Expand All @@ -74,6 +82,17 @@ public static string ReplaceGreekOrCyrillicWithLatin(this string str)
return str.ReplaceWithDictionary(GreekCyrillicLatinMap);
}

public static string CorrectCommonGuidErrors(this string guid)
{
// remove all spaces
guid = guid.Replace(" ", "");
// if a line ends with a dash remove the newline after the dash
guid = guid.Replace("-\r\n", "-");
// if a line begins with a dash remove the newline before the dash
guid = guid.Replace("\r\n-", "-");
return guid.ReplaceWithDictionary(GuidCorrections);
}

public static IEnumerable<int> AllIndexesOf(this string str, string searchString)
{
int minIndex = str.IndexOf(searchString);
Expand Down
4 changes: 4 additions & 0 deletions Text-Grab/Views/EditTextWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@
x:Name="TryToAlphaMenuItem"
Click="TryToAlphaMenuItem_Click"
Header="Try To Make _Letters" />
<MenuItem
x:Name="CorrectGuid"
Click="CorrectGuid_Click"
Header="Correct Common _GUID/UUID Errors" />
<MenuItem
x:Name="ToggleCaseMenuItem"
Command="{x:Static local:EditTextWindow.ToggleCaseCmd}"
Expand Down
12 changes: 12 additions & 0 deletions Text-Grab/Views/EditTextWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,5 +2127,17 @@ private void WrapTextCHBOX_Checked(object sender, RoutedEventArgs e)

DefaultSettings.EditWindowIsWordWrapOn = WrapTextMenuItem.IsChecked;
}

private void CorrectGuid_Click(object sender, RoutedEventArgs e)
{
string workingString = GetSelectedTextOrAllText();

workingString = workingString.CorrectCommonGuidErrors();

if (PassedTextControl.SelectionLength == 0)
PassedTextControl.Text = workingString;
else
PassedTextControl.SelectedText = workingString;
}
#endregion Methods
}

0 comments on commit d2b3f33

Please sign in to comment.