Fix yet another bug with case insensitive

This commit is contained in:
Nicolo P 2022-02-23 19:50:14 +01:00
parent d0dd2cff17
commit ba159e8021

View File

@ -6,17 +6,18 @@ import sys
def find_line(line: str, pattern: str, i_case: bool, is_regexp=False) -> bool: def find_line(line: str, pattern: str, i_case: bool, is_regexp=False) -> bool:
""" """
Distinguish between regexp or not (default is False) Check if line contains matches for pattern, with case and regexp flags.
Returns True if pattern is found in line.
""" """
found = False found = False
if i_case:
line = line.lower()
pattern = pattern.lower()
if is_regexp: if is_regexp:
matches = re.findall(pattern, line) matches = re.findall(pattern, line)
FIND_COND = len(matches) > 0 FIND_COND = len(matches) > 0
elif i_case:
lower = line.lower()
FIND_COND = lower.find(pattern.lower()) != -1
else: else:
FIND_COND = line.find(pattern) != -1 FIND_COND = line.find(pattern) != -1
@ -72,12 +73,12 @@ def parse_args(args: list) -> dict:
Parse cli arguments and return the tree as a dict Parse cli arguments and return the tree as a dict
""" """
# TODO move to class # TODO move to class
OPTS = [ OPTS = (
'i', # Case-insensitive search 'i', # Case-insensitive search
'E', # Pattern is a full regexp 'E', # Pattern is a full regexp
'r', # Search recursively in dir 'r', # Search recursively in dir
'n', # Print line numbers 'n', # Print line numbers
] )
# TODO Handle extended options? (e.g. --ignore-case) # TODO Handle extended options? (e.g. --ignore-case)
LONG_OPTS = [ LONG_OPTS = [
'--ignore-case', '--ignore-case',