From 01e330aa97dcd7455d6dd4624b3d836442a8bfde Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 22 Apr 2022 13:33:36 -0700 Subject: [PATCH] precompile regex patterns --- jc/parsers/git_log.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jc/parsers/git_log.py b/jc/parsers/git_log.py index 81bf3d9f..e72a5e88 100644 --- a/jc/parsers/git_log.py +++ b/jc/parsers/git_log.py @@ -148,6 +148,8 @@ import re from typing import List, Dict import jc.utils +hash_pattern = re.compile(r'([0-9]|[a-f])+') +changes_pattern = re.compile(r'\s(?P\d+)\s+(files? changed),\s+(?P\d+)\s(insertions?\(\+\))?(,\s+)?(?P\d+)?(\s+deletions?\(\-\))?') class info(): """Provides parser metadata (version, author, etc.)""" @@ -194,8 +196,7 @@ def _is_commit_hash(hash_string: str) -> bool: if len(hash_string) != 40: return False - hash_pattern = r'([0-9]|[a-f])+' - if re.match(hash_pattern, hash_string): + if hash_pattern.match(hash_string): return True return False @@ -304,8 +305,7 @@ def parse( if line.startswith(' ') and 'changed, ' in line: # this is the stat summary - changes_pattern = r'\s(?P\d+)\s+(files? changed),\s+(?P\d+)\s(insertions?\(\+\))?(,\s+)?(?P\d+)?(\s+deletions?\(\-\))?' - changes = re.match(changes_pattern, line) + changes = changes_pattern.match(line) if changes: files = changes['files'] insertions = changes['insertions']