Skip to content

Commit

Permalink
Add case_invariant option.
Browse files Browse the repository at this point in the history
Per: #1
  • Loading branch information
Stan James committed Aug 21, 2022
1 parent 0036893 commit abba281
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions confusables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ def __init__(self, confusables_filename):
confusables_dict[auth].append(fake)
self.confusables_dict = confusables_dict

def expand_char_to_confusables(self, c):
def expand_char_to_confusables(self, c, case_invariant=False):
if c in self.confusables_dict:
return '[{}{}]'.format(re.escape(c), re.escape("".join(self.confusables_dict[c])))
if case_invariant:
return '[{}{}{}]'.format(re.escape(c), re.escape("".join(self.confusables_dict[c.upper()])), ("".join(self.confusables_dict[c.lower()])))
else:
return '[{}{}]'.format(re.escape(c), re.escape("".join(self.confusables_dict[c])))
else:
return c

def confusables_regex(self, pattern, letter_test_function=None):
def confusables_regex(self, pattern, letter_test_function=None, case_invariant=False):
"""
Return string with each letter replaced with character class that
matches the letter and any character that might be confused
Expand All @@ -49,7 +52,10 @@ def confusables_regex(self, pattern, letter_test_function=None):
for c in pattern:
if ((not letter_test_function) or
(letter_test_function and letter_test_function(c))):
new += self.expand_char_to_confusables(c)
if case_invariant:
new += self.expand_char_to_confusables(c, case_invariant=case_invariant)
else:
new += self.expand_char_to_confusables(c)
else:
new += c
return new
Expand Down

0 comments on commit abba281

Please sign in to comment.