Skip to content

Commit

Permalink
faster arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Feb 15, 2024
1 parent 99c57ec commit 74124e2
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions cat_win/util/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@


IS_FILE, IS_DIR, IS_PATTERN = range(0, 3)
RE_ENCODING = re.compile(r"\Aenc[\=\:].+\Z", re.IGNORECASE)
RE_MATCH = re.compile(r"\Amatch[\=\:].+\Z", re.IGNORECASE)
RE_FIND = re.compile(r"\Afind[\=\:].+\Z", re.IGNORECASE)
RE_TRUNC = re.compile(r"\Atrunc[\=\:][0-9\(\)\+\-\*\/]*"
r"\:[0-9\(\)\+\-\*\/]*\:?"
r"[0-9\(\)\+\-\*\/]*\Z")
RE_CUT = re.compile(r"\A\[[0-9\(\)\+\-\*\/]*\:"
r"[0-9\(\)\+\-\*\/]*\:?"
r"[0-9\(\)\+\-\*\/]*\]\Z")
RE_REPLACE = re.compile(r"\A\[(?:.*[^\\])?(?:\\\\)*,"
r"(?:.*[^\\])?(?:\\\\)*\]\Z")
# using simple if-statements (e.g. startwith()) would be faster, but arguably less readable


def levenshtein(str_a: str, str_b: str) -> float:
Expand Down Expand Up @@ -210,38 +222,37 @@ def _add_argument(self, param: str, delete: bool = False) -> bool:
should simply be printed to stdout.
"""
# 'enc' + ('=' or ':') + file_encoding
if re.match(r"\Aenc[\=\:].+\Z", param, re.IGNORECASE):
if RE_ENCODING.match(param):
self.file_encoding = param[4:]
return False
# 'match' + ('=' or ':') + file_match
if re.match(r"\Amatch[\=\:].+\Z", param, re.IGNORECASE):
if RE_MATCH.match(param):
if delete:
self.file_match.discard(param[6:])
return False
self.file_match.add(fr'{param[6:]}')
return False
# 'find' + ('=' or ':') + file_search
if re.match(r"\Afind[\=\:].+\Z", param, re.IGNORECASE):
if RE_FIND.match(param):
if delete:
self.file_search.discard(param[5:])
return False
self.file_search.add(param[5:])
return False
# 'trunc' + ('='/':') + file_truncate[0] +':'+ file_truncate[1] [+ ':' + file_truncate[2]]
if re.match(r"\Atrunc[\=\:][0-9\(\)\+\-\*\/]*\:[0-9\(\)\+\-\*\/]*\:?[0-9\(\)\+\-\*\/]*\Z",
param, re.IGNORECASE):
if RE_TRUNC.match(param):
for i, p_split in enumerate(param[6:].split(':')):
try:
self.file_truncate[i] = int(eval(p_split))
except (SyntaxError, NameError, ValueError, ArithmeticError):
self.file_truncate[i] = None
return False
# '[' + ARGS_CUT + ']'
if re.match(r"\A\[[0-9\(\)\+\-\*\/]*\:[0-9\(\)\+\-\*\/]*\:?[0-9\(\)\+\-\*\/]*\]\Z", param):
if RE_CUT.match(param):
self._args.append((ARGS_CUT, param))
return False
# '[' + ARGS_REPLACE_THIS + ',' + ARGS_REPLACE_WITH + ']' (escape chars with '\')
if re.match(r"\A\[(?:.*[^\\])?(?:\\\\)*,(?:.*[^\\])?(?:\\\\)*\]\Z", param):
if RE_REPLACE.match(param):
self._args.append((ARGS_REPLACE, param))
return False

Expand Down

0 comments on commit 74124e2

Please sign in to comment.