Skip to content

Commit

Permalink
added ignore case to find and match
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Feb 19, 2024
1 parent 03cd2ab commit 6167750
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cat_win/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,10 @@ def print_file(content: list) -> bool:
return False

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

for line_prefix, line in content:
cleaned_line = remove_ansi_codes_from_line(line)
Expand Down
6 changes: 6 additions & 0 deletions cat_win/util/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ 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_truncate = [None, None, None]

def get_args(self) -> list:
Expand Down Expand Up @@ -230,13 +232,17 @@ def _add_argument(self, param: str, delete: bool = False) -> bool:
if delete:
self.file_match.discard(param[6:])
return False
if param[:5].isupper():
self.file_match_ignore_case = True
self.file_match.add(fr'{param[6:]}')
return False
# 'find' + ('=' or ':') + file_search
if RE_FIND.match(param):
if delete:
self.file_search.discard(param[5:])
return False
if param[:4].isupper():
self.file_search_ignore_case = True
self.file_search.add(param[5:])
return False
# 'trunc' + ('='/':') + file_truncate[0] +':'+ file_truncate[1] [+ ':' + file_truncate[2]]
Expand Down
9 changes: 7 additions & 2 deletions cat_win/util/stringfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class StringFinder:
"""
defines a stringfinder
"""
def __init__(self, literals: set = None, regex: set = None) -> None:
def __init__(self, literals: set = None, regex: set = None,
literals_ignore_case: bool = False, regex_ignore_case: bool = False) -> 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):
"""
Expand All @@ -29,6 +32,8 @@ def _findliterals(self, sub: str, _s: str):
(list):
containing the start and end indeces like [start, end]
"""
if self.literals_ignore_case:
sub, _s = sub.lower(), _s.lower()
_l = len(sub)
i = _s.find(sub)
while i != -1:
Expand All @@ -49,7 +54,7 @@ def _findregex(self, pattern: str, _s: str):
(list):
containing the start and end indeces like [start, end]
"""
for match in re.finditer(fr'{pattern}', _s):
for match in re.finditer(fr'{pattern}', _s, re.IGNORECASE if self.regex_ignore_case else 0):
yield list(match.span())

def _optimize_intervals(self, intervals: list) -> list:
Expand Down

0 comments on commit 6167750

Please sign in to comment.