Skip to content

Commit

Permalink
Update common letter number confusions
Browse files Browse the repository at this point in the history
style code
  • Loading branch information
TheJoeFin committed Aug 25, 2024
1 parent 2f67b2a commit 8494fc2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 52 deletions.
12 changes: 6 additions & 6 deletions Tests/StringMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public void ReturnWordAtCursorWithNewLines(int cursorPosition, string expectedWo

[Theory]
[InlineData("", "")]
[InlineData("Hello, world! 0123456789", "Hello, world! ol23h5678g")]
[InlineData("Hello, world! 0123456789", "Hello, world! olz3hSb7Bg")]
[InlineData("Foo 4r b4r", "Foo hr bhr")]
[InlineData("B4zz 9zzl3", "Bhzz gzzl3")]
[InlineData("B4zz5 9zzl3", "BhzzS gzzl3")]
[InlineData("abcdefghijklmnop", "abcdefghijklmnop")]
public void TryFixToLetters_ReplacesDigitsWithLetters_AsExpected(string input, string expected)
{
Expand All @@ -106,10 +106,10 @@ public void TryFixNumOrLetters(string input, string expected)

[Theory]
[InlineData("", "")]
[InlineData("Hello, world! 0123456789", "He110, w0r1d! 0123456789")]
[InlineData("Foo 4r b4r", "F00 4r b4r")]
[InlineData("B4zz 9zzl3", "B4zz 9zz13")]
[InlineData("abcdefghijklmnop", "ab0def9h1jk1mn0p")]
[InlineData("Hello, world! 0123456789", "4e110, w0r1d! 0123456789")]
[InlineData("Foo 4r b4r", "F00 4r 64r")]
[InlineData("B4zzS 9zzl3", "84225 92213")]
[InlineData("abcdefghijklmnopqrs", "a60def941jk1mn0pqr5")]
public void TryFixToLetters_ReplacesLettersWithDigits_AsExpected(string input, string expected)
{
// Act
Expand Down
2 changes: 1 addition & 1 deletion Text-Grab/Extensions/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void RemoveTrailingNewlines(this StringBuilder text)

public static void ReplaceGreekOrCyrillicWithLatin(this StringBuilder stringBuilder)
{
stringBuilder.CharDictionaryReplace(StringMethods.greekCyrillicLatinMap);
stringBuilder.CharDictionaryReplace(StringMethods.GreekCyrillicLatinMap);
}

public static void TryFixToLetters(this StringBuilder stringBuilder)
Expand Down
92 changes: 47 additions & 45 deletions Text-Grab/Utilities/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ namespace Text_Grab.Utilities;

public static class StringMethods
{
public static readonly List<Char> specialCharList = new()
{ '\\', ' ', '.', ',', '$', '^', '{', '[', '(', '|', ')', '*', '+', '?', '=' };
public static readonly List<Char> specialCharList = [
'\\', ' ', '.', ',', '$', '^', '{', '[', '(', '|', ')',
'*', '+', '?', '=' ];

public static readonly List<Char> ReservedChars = new()
{ ' ', '"', '*', '/', ':', '<', '>', '?', '\\', '|', '+', ',', '.', ';', '=', '[', ']', '!', '@' };
public static readonly List<Char> ReservedChars = [
' ', '"', '*', '/', ':', '<', '>', '?', '\\', '|', '+',
',', '.', ';', '=', '[', ']', '!', '@' ];

public static readonly Dictionary<char, char> greekCyrillicLatinMap = new()
public static readonly Dictionary<char, char> GreekCyrillicLatinMap = new()
{
// Similar Looking Greek characters
{'Γ', 'r'}, {'Δ', 'A'}, {'Θ', 'O'}, {'Λ', 'A'}, {'Ξ', 'E'},
Expand Down Expand Up @@ -43,41 +45,42 @@ public static class StringMethods
{'ø', 'e'},
};

public static Dictionary<char, char> NumbersToLetters = new()
public static readonly Dictionary<char, char> NumbersToLetters = new()
{
{'0', 'o'}, {'4', 'h'}, {'9', 'g'}, {'1', 'l'}
{'0', 'o'}, {'4', 'h'}, {'9', 'g'}, {'1', 'l'}, {'8', 'B'},
{'5', 'S'}, {'6', 'b'}, {'2', 'z' }
};

public static Dictionary<char, char> LettersToNumbers = new()
public static readonly Dictionary<char, char> LettersToNumbers = new()
{
{'o', '0'}, {'O', '0'}, {'Q', '0'}, {'c', '0'}, {'C', '0'},
{'i', '1'}, {'I', '1'}, {'l', '1'}, {'g', '9'}
{'i', '1'}, {'I', '1'}, {'l', '1'}, {'g', '9'}, {'G', '9'},
{'h', '4'}, {'H', '4'}, {'s', '5'}, {'S', '5'}, {'B', '8'},
{'b', '6'}, {'z', '2'}, {'Z', '2'}
};

public static string ReplaceWithDictionary(this string str, Dictionary<char, char> dict)
{
var sb = new StringBuilder();
StringBuilder sb = new();

foreach (char c in str)
{
sb.Append(dict.ContainsKey(c) ? dict[c] : c);
}
sb.Append(dict.TryGetValue(c, out char value) ? value : c);

return sb.ToString();
}

public static string ReplaceGreekOrCyrillicWithLatin(this string str)
{
return str.ReplaceWithDictionary(greekCyrillicLatinMap);
return str.ReplaceWithDictionary(GreekCyrillicLatinMap);
}

public static IEnumerable<int> AllIndexesOf(this string str, string searchstring)
public static IEnumerable<int> AllIndexesOf(this string str, string searchString)
{
int minIndex = str.IndexOf(searchstring);
int minIndex = str.IndexOf(searchString);
while (minIndex != -1)
{
yield return minIndex;
minIndex = str.IndexOf(searchstring, minIndex + searchstring.Length);
minIndex = str.IndexOf(searchString, minIndex + searchString.Length);
}
}

Expand All @@ -102,7 +105,7 @@ public static (int, int) CursorWordBoundaries(this string input, int cursorPosit

// Check if the cursor is at a space
if (char.IsWhiteSpace(input[cursorPosition]))
cursorPosition = findNearestLetterIndex(input, cursorPosition);
cursorPosition = FindNearestLetterIndex(input, cursorPosition);

// Find the start and end of the word by moving the cursor
// backwards and forwards until we find a non-letter character.
Expand Down Expand Up @@ -132,7 +135,7 @@ public static string GetWordAtCursorPosition(this string input, int cursorPositi
return input.Substring(start, length);
}

private static int findNearestLetterIndex(string input, int cursorPosition)
private static int FindNearestLetterIndex(string input, int cursorPosition)
{
Math.Clamp(cursorPosition, 0, input.Length - 1);

Expand Down Expand Up @@ -242,14 +245,14 @@ public static string TryFixNumberLetterErrors(this string stringToFix)
public static string TryFixEveryWordLetterNumberErrors(this string stringToFix)
{
string[] listOfWords = stringToFix.Split(' ');
List<string> fixedWords = new();
List<string> fixedWords = [];

foreach (string word in listOfWords)
{
string newWord = word.TryFixNumberLetterErrors();
fixedWords.Add(newWord);
}
string joinedString = string.Join(' ', fixedWords.ToArray());
string joinedString = string.Join(' ', [.. fixedWords]);
joinedString = joinedString.Replace("\t ", "\t");
joinedString = joinedString.Replace("\r ", "\r");
joinedString = joinedString.Replace("\n ", "\n");
Expand All @@ -276,7 +279,7 @@ public static string MakeStringSingleLine(this string textToEdit)
if (workingString[0] == ' ')
workingString.Remove(0, 1);

if (workingString[workingString.Length - 1] == ' ')
if (workingString[^1] == ' ')
workingString.Remove(workingString.Length - 1, 1);

return workingString.ToString();
Expand Down Expand Up @@ -344,16 +347,16 @@ public enum CharType { Letter, Number, Space, Special, Other };
public class CharRun
{
public CharType TypeOfChar { get; set; }
public Char Character { get; set; }
public int numberOfRun { get; set; }
public char Character { get; set; }
public int NumberOfRun { get; set; }
}

public static string ReplaceReservedCharacters(this string stringToClean)
{
StringBuilder sb = new();
sb.Append(stringToClean);

foreach (Char reservedChar in ReservedChars)
foreach (char reservedChar in ReservedChars)
sb.Replace(reservedChar, '-');

return Regex.Replace(sb.ToString(), @"-+", "-");
Expand Down Expand Up @@ -382,41 +385,40 @@ public static string ExtractSimplePattern(this string stringToExtract)
foreach (char c in stringToExtract)
{
CharType thisCharType = CharType.Other;
if (Char.IsWhiteSpace(c))
if (char.IsWhiteSpace(c))
thisCharType = CharType.Space;
else if (specialCharList.Contains(c))
thisCharType = CharType.Special;
else if (Char.IsLetter(c))
else if (char.IsLetter(c))
thisCharType = CharType.Letter;
else if (Char.IsNumber(c))
else if (char.IsNumber(c))
thisCharType = CharType.Number;

if (thisCharType == charRunList.LastOrDefault()?.TypeOfChar)
{
if (thisCharType == CharType.Other)
{
if (c == charRunList.Last().Character)
charRunList.Last().numberOfRun++;
charRunList.Last().NumberOfRun++;
}
else
{
charRunList.Last().numberOfRun++;
charRunList.Last().NumberOfRun++;
}
}
else
{
CharRun newRun = new()
{
Character = c,
numberOfRun = 1,
NumberOfRun = 1,
TypeOfChar = thisCharType
};
charRunList.Add(newRun);
}
}

StringBuilder sb = new();
// sb.Append("(");

foreach (CharRun ct in charRunList)
{
Expand All @@ -441,9 +443,9 @@ public static string ExtractSimplePattern(this string stringToExtract)
break;
}

if (ct.numberOfRun > 1)
if (ct.NumberOfRun > 1)
{
sb.Append('{').Append(ct.numberOfRun).Append('}');
sb.Append('{').Append(ct.NumberOfRun).Append('}');
}
}

Expand Down Expand Up @@ -500,7 +502,7 @@ private static string ShortenRegexPattern(this string pattern)
sb.Clear();
}

possibleShortenedPatterns = possibleShortenedPatterns.OrderBy(p => p.Length).ToList();
possibleShortenedPatterns = [.. possibleShortenedPatterns.OrderBy(p => p.Length)];

return possibleShortenedPatterns.First();
}
Expand Down Expand Up @@ -574,14 +576,14 @@ public static string UnstackGroups(this string stringGroupedToUnstack, int numbe

public static string RemoveDuplicateLines(this string stringToDeduplicate)
{
string[] splitString = stringToDeduplicate.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.TrimEntries);
List<string> uniqueLines = new();
string[] splitString = stringToDeduplicate.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries);
List<string> uniqueLines = [];

foreach (string originalLine in splitString)
if (!uniqueLines.Contains(originalLine))
uniqueLines.Add(originalLine);

return string.Join(Environment.NewLine, uniqueLines.ToArray());
return string.Join(Environment.NewLine, [.. uniqueLines]);
}

public static string RemoveAllInstancesOf(this string stringToBeEdited, string stringToRemove)
Expand All @@ -592,7 +594,7 @@ public static string RemoveAllInstancesOf(this string stringToBeEdited, string s

public static string RemoveFromEachLine(this string stringToEdit, int numberOfChars, SpotInLine spotInLine)
{
string[] splitString = stringToEdit.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
string[] splitString = stringToEdit.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

StringBuilder sb = new();
foreach (string line in splitString)
Expand All @@ -607,10 +609,10 @@ public static string RemoveFromEachLine(this string stringToEdit, int numberOfCh
switch (spotInLine)
{
case SpotInLine.Beginning:
sb.AppendLine(line.Substring(numberOfChars));
sb.AppendLine(line[numberOfChars..]);
break;
case SpotInLine.End:
sb.AppendLine(line.Substring(0, lineLength - numberOfChars));
sb.AppendLine(line[..(lineLength - numberOfChars)]);
break;
default:
break;
Expand Down Expand Up @@ -661,7 +663,7 @@ public static string LimitCharactersPerLine(this string stringToEdit, int charac
}

if (spotInLine== SpotInLine.Beginning)
returnStringBuilder.AppendLine(line.Substring(0, lineLimit));
returnStringBuilder.AppendLine(line[..lineLimit]);
else
returnStringBuilder.AppendLine(line.Substring(line.Length - (lineLimit), lineLimit));
}
Expand Down Expand Up @@ -689,7 +691,7 @@ public static string GetCharactersToLeftOfNewLine(ref string mainString, int ind
int newLineIndex = GetNewLineIndexToLeft(ref mainString, index);

if (newLineIndex < 1)
return mainString.Substring(0, index);
return mainString[..index];

newLineIndex++;

Expand All @@ -709,13 +711,13 @@ public static string GetCharactersToRightOfNewLine(ref string mainString, int in
{
int newLineIndex = GetNewLineIndexToRight(ref mainString, index);
if (newLineIndex < 1)
return mainString.Substring(index);
return mainString[index..];

if (newLineIndex - index > numberOfCharacters)
return string.Concat(mainString.AsSpan(index, numberOfCharacters), "...");

if (newLineIndex == mainString.Length)
return mainString.Substring(index);
return mainString[index..];

return string.Concat(mainString.AsSpan(index, newLineIndex - index), "...");
}
Expand Down

0 comments on commit 8494fc2

Please sign in to comment.