Skip to content

Commit

Permalink
fix case sensitivity bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Mar 20, 2024
1 parent 644cd31 commit 2eac7e9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 47 deletions.
4 changes: 1 addition & 3 deletions cat_win/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,7 @@ def print_file(content: list) -> bool:

contains_queried = False
string_finder = StringFinder(arg_parser.file_search,
arg_parser.file_match,
arg_parser.file_search_ignore_case,
arg_parser.file_match_ignore_case)
arg_parser.file_match)

for line_prefix, line in content:
cleaned_line = remove_ansi_codes_from_line(line)
Expand Down
18 changes: 9 additions & 9 deletions cat_win/tests/test_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ def test_get_arguments_encoding(self):
def test_get_arguments_match(self):
arg_parser = ArgParser()
arg_parser.get_arguments(['CAT', 'match:\\Atest\\Z'])
self.assertCountEqual(arg_parser.file_match, set(['\\Atest\\Z']))
self.assertCountEqual(arg_parser.file_match, set([('\\Atest\\Z', False)]))
arg_parser.get_arguments(['CAT', 'match=\\Atest\\Z'])
self.assertCountEqual(arg_parser.file_match, set(['\\Atest\\Z']))
self.assertCountEqual(arg_parser.file_match, set([('\\Atest\\Z', False)]))

def test_get_arguments_find(self):
arg_parser = ArgParser()
arg_parser.get_arguments(['CAT', 'find=Test123'])
self.assertCountEqual(arg_parser.file_search, set(['Test123']))
arg_parser.get_arguments(['CAT', 'find:Test123'])
self.assertCountEqual(arg_parser.file_search, set(['Test123']))
self.assertCountEqual(arg_parser.file_search, set([('Test123', False)]))
arg_parser.get_arguments(['CAT', 'FIND:Test123'])
self.assertCountEqual(arg_parser.file_search, set([('Test123', False), ('Test123', True)]))

def test_get_arguments_trunc(self):
arg_parser = ArgParser()
Expand Down Expand Up @@ -200,15 +200,15 @@ def test_delete_queried(self):
arg_parser = ArgParser()
arg_parser._add_argument('find=hello')
arg_parser._add_argument('find=world')
self.assertSetEqual(arg_parser.file_search, set(['hello', 'world']))
self.assertSetEqual(arg_parser.file_search, set([('hello', False), ('world', False)]))
arg_parser._add_argument('find=hello', True)
self.assertSetEqual(arg_parser.file_search, set(['world']))
self.assertSetEqual(arg_parser.file_search, set([('world', False)]))

arg_parser._add_argument('match=[a-z]')
arg_parser._add_argument('match=[0-9]')
self.assertSetEqual(arg_parser.file_match, set(['[a-z]', '[0-9]']))
self.assertSetEqual(arg_parser.file_match, set([('[a-z]', False), ('[0-9]', False)]))
arg_parser._add_argument('match=[0-9]', True)
self.assertSetEqual(arg_parser.file_match, set(['[a-z]']))
self.assertSetEqual(arg_parser.file_match, set([('[a-z]', False)]))

def test_check_unknown_args(self):
arg_parser = ArgParser()
Expand Down
4 changes: 2 additions & 2 deletions cat_win/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_cat_shell_delete_param(self):
def test_cat_shell_see_param(self):
stdinhelpermock.set_content('!add -ln find=test match=[0-9]\n!see')
expected_output = ["successfully added ['-l', '-n'].", "Active Args: ['-l', '-n']",
"Literals: {'test'}", "Matches: {'[0-9]'}"]
"Literals: {('test', False)}", "Matches: {('[0-9]', False)}"]
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.shell_main()
fake_output = [line.lstrip('>>> ') for line in fake_out.getvalue().splitlines()[2:-1]]
Expand All @@ -81,7 +81,7 @@ def test_cat_shell_see_param(self):
def test_cat_shell_clear_param(self):
stdinhelpermock.set_content('!add -ln find=test match=[0-9]\n!see\n!clear\n!see')
expected_output = ["successfully added ['-l', '-n'].", "Active Args: ['-l', '-n']",
"Literals: {'test'}", "Matches: {'[0-9]'}",
"Literals: {('test', False)}", "Matches: {('[0-9]', False)}",
"successfully removed ['-l', '-n'].", 'Active Args: []']
with patch('sys.stdout', new=StdOutMock()) as fake_out:
cat.shell_main()
Expand Down
27 changes: 15 additions & 12 deletions cat_win/tests/test_stringfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@
class TestStringFinder(TestCase):
def test_find_literals_true(self):
string_finder = StringFinder(set(), set())
_x = list(string_finder._findliterals('test', 'abctestdef'))
_x = list(string_finder._findliterals('test', 'abctestdef', False))
self.assertEqual(_x, [[3, 7]])

_x = list(string_finder._findliterals('test', 'testabctestdetestf'))
_x = list(string_finder._findliterals('test', 'testabctestdetestf', False))
self.assertEqual(_x, [[0, 4], [7, 11], [13, 17]])

_x = list(string_finder._findliterals('', ''))
_x = list(string_finder._findliterals('', '', False))
self.assertEqual(_x, [[0, 0]])

_x = list(string_finder._findliterals('', 'x'))
_x = list(string_finder._findliterals('', 'x', False))
self.assertEqual(_x, [[0, 0], [1, 1]])

def test_find_literals_false(self):
string_finder = StringFinder(set(), set())
_x = list(string_finder._findliterals('a', ''))
_x = list(string_finder._findliterals('a', '', False))
self.assertEqual(_x, [])

_x = list(string_finder._findliterals('test', 'tsetabctesdeestf'))
_x = list(string_finder._findliterals('test', 'tsetabctesdeestf', False))
self.assertEqual(_x, [])

def test_find_regex_true(self):
string_finder = StringFinder(set(), set())
_x = list(string_finder._findregex(r"[0-9]{2}", '123'))
_x = list(string_finder._findregex(r"[0-9]{2}", '123', False))
self.assertEqual(_x, [[0, 2]])

_x = list(string_finder._findregex(r"[0-9]{2}", '1234'))
_x = list(string_finder._findregex(r"[0-9]{2}", '1234', False))
self.assertEqual(_x, [[0, 2], [2, 4]])

_x = list(string_finder._findregex(r"[A-Z]{1}[a-z]*\s?.*\.+\s", 'Silas A. Kraume'))
_x = list(string_finder._findregex(r"[A-Z]{1}[a-z]*\s?.*\.+\s", 'Silas A. Kraume', False))
self.assertEqual(_x, [[0, 9]])

_x = list(string_finder._findregex(r"[A-Z]{1}[a-z]*\s?.*\.+\s", 'silas A. Kraume'))
_x = list(string_finder._findregex(r"[A-Z]{1}[a-z]*\s?.*\.+\s", 'silas A. Kraume', False))
self.assertEqual(_x, [[6, 9]])

def test_find_regex_false(self):
string_finder = StringFinder(set(), set())
_x = list(string_finder._findregex(r"[A-Z]{1}[a-z]+\s?.*\.+\s", 'silas A. Kraume'))
_x = list(string_finder._findregex(r"[A-Z]{1}[a-z]+\s?.*\.+\s", 'silas A. Kraume', False))
self.assertEqual(_x, [])

def test_optimize_intervals(self):
Expand All @@ -67,7 +67,10 @@ def test_optimize_intervals_empty(self):
self.assertEqual(_x, [])

def test_find_keywords(self):
string_finder = StringFinder(set(['Is', 'Test', 'Not']), set([r"[0-9]\!"]))
string_finder = StringFinder(set([('Is', False),
('Test', False),
('Not', False)]),
set([(r"[0-9]\!", False)]))
line = 'ThisIsATest!1!'
intervals, f_keywords, m_keywords = string_finder.find_keywords(line)

Expand Down
17 changes: 8 additions & 9 deletions cat_win/util/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def reset_values(self) -> None:
"""
self.file_encoding = self.default_file_encoding
self.file_search = set()
self.file_search_ignore_case = False
self.file_match = set()
self.file_match_ignore_case = False
self.file_replace_mapping = {}
self.file_truncate = [None, None, None]

Expand Down Expand Up @@ -237,21 +235,22 @@ def _add_argument(self, param: str, delete: bool = False) -> bool:
# 'match' + ('=' or ':') + file_match
if RE_MATCH.match(param):
if delete:
self.file_match.discard(param[6:])
self.file_match.discard((param[6:], param[:5].isupper()))
return False
self.file_match_ignore_case = param[:5].isupper()
self.file_match.add(param[6:])
self.file_match.add((param[6:], param[:5].isupper()))
return False
# 'find' + ('=' or ':') + file_search
if RE_FIND.match(param):
if delete:
self.file_search.discard(param[5:])
self.file_search.discard((param[5:], param[:4].isupper()))
return False
self.file_search_ignore_case = param[:4].isupper()
try:
self.file_search.add(param[5:].encode().decode('unicode_escape').encode('latin-1').decode())
self.file_search.add((
param[5:].encode().decode('unicode_escape').encode('latin-1').decode(),
param[:4].isupper()
))
except UnicodeError:
self.file_search.add(param[5:])
self.file_search.add((param[5:], param[:4].isupper()))
return False
# 'trunc' + ('='/':') + file_truncate[0] +':'+ file_truncate[1] [+ ':' + file_truncate[2]]
if RE_TRUNC.match(param):
Expand Down
21 changes: 9 additions & 12 deletions cat_win/util/stringfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ class StringFinder:
"""
defines a stringfinder
"""
def __init__(self, literals: set = None, regex: set = None,
literals_ignore_case: bool = False, regex_ignore_case: bool = False) -> None:
def __init__(self, literals: set = None, regex: set = None) -> None:
self.kw_literals = literals if literals is not None else {}
self.kw_regex = regex if regex is not None else {}
self.literals_ignore_case = literals_ignore_case
self.regex_ignore_case = regex_ignore_case

def _findliterals(self, sub: str, _s: str):
def _findliterals(self, sub: str, _s: str, ignore_case: bool):
"""
Generate lists containing the position of sub in s.
Expand All @@ -32,15 +29,15 @@ def _findliterals(self, sub: str, _s: str):
(list):
containing the start and end indeces like [start, end]
"""
if self.literals_ignore_case:
if ignore_case:
sub, _s = sub.lower(), _s.lower()
_l = len(sub)
i = _s.find(sub)
while i != -1:
yield [i, i+_l]
i = _s.find(sub, i+1)

def _findregex(self, pattern: str, _s: str):
def _findregex(self, pattern: str, _s: str, ignore_case: bool):
"""
Generate lists containing the position of pattern in s.
Expand All @@ -55,7 +52,7 @@ def _findregex(self, pattern: str, _s: str):
containing the start and end indeces like [start, end]
"""
for _match in re.finditer(pattern, _s,
re.IGNORECASE if self.regex_ignore_case else 0 | re.DOTALL):
re.IGNORECASE if ignore_case else 0 | re.DOTALL):
yield list(_match.span())

def _optimize_intervals(self, intervals: list) -> list:
Expand Down Expand Up @@ -118,15 +115,15 @@ def find_keywords(self, line: str) -> tuple:
matched_list = []
matched_position = []

for keyword in self.kw_literals:
for _f in self._findliterals(keyword, line):
for keyword, ignore_case in self.kw_literals:
for _f in self._findliterals(keyword, line, ignore_case):
found_position.append(_f[:])
found_list.append((keyword, _f))
# sort by start position (necessary for a deterministic output)
found_list.sort(key = lambda x: x[1][0])

for keyword in self.kw_regex:
for _m in self._findregex(keyword, line):
for keyword, ignore_case in self.kw_regex:
for _m in self._findregex(keyword, line, ignore_case):
matched_position.append(_m[:])
matched_list.append((keyword, _m))
# sort by start position (necessary for a deterministic output)
Expand Down

0 comments on commit 2eac7e9

Please sign in to comment.