From 470b046702884d4ff6f2b719674d20c7c21dffa6 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Wed, 6 Jul 2022 13:49:13 +0200 Subject: [PATCH] compile regext outside of routes --- files/helpers/regex.py | 4 ++++ files/routes/admin.py | 2 +- files/routes/search.py | 3 +-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/files/helpers/regex.py b/files/helpers/regex.py index d1761593f..4e31dbf35 100644 --- a/files/helpers/regex.py +++ b/files/helpers/regex.py @@ -96,6 +96,10 @@ color_regex = re.compile("[a-z0-9]{6}", flags=re.A) showmore_regex = re.compile("(.{3000,40000}?

)(

.*)", flags=re.A) +search_token_regex = re.compile('"([^"]*)"|(\S+)', flags=re.A) + +git_regex = re.compile("ref: (refs/.+)", flags=re.A) + def sub_matcher(match, upper=False): if match.group(0).startswith('<'): return match.group(0) diff --git a/files/routes/admin.py b/files/routes/admin.py index f1015bbcb..402e5f305 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -493,7 +493,7 @@ def admin_git_head(): try: with open('.git/HEAD', encoding='utf_8') as head_f: head_txt = head_f.read() - head_path = re.match('ref: (refs/.+)', head_txt).group(1) + head_path = git_regex.match(head_txt).group(1) with open('.git/' + head_path, encoding='utf_8') as ref_f: gitref = ref_f.read()[0:short_len] except: diff --git a/files/routes/search.py b/files/routes/search.py index c7f409956..cbbdb47b8 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -24,10 +24,9 @@ def searchparse(text): text = text.replace(f"{x}:{criteria[x]}", "") text = text.strip() - re_search_token = re.compile('"([^"]*)"|(\S+)') if text: criteria['q'] = [] - for m in re_search_token.finditer(text): + for m in search_token_regex.finditer(text): token = m[1] if m[1] else m[2] # Escape SQL pattern matching special characters token = token.replace('\\', '').replace('_', '\_').replace('%', '\%')