Skip to content

Commit b87402f

Browse files
authored
Merge pull request #144 from PortfolioAI/main
Optimize Regex Performance in Patch Class
2 parents 3621d31 + 2d356d9 commit b87402f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

aicodebot/patch.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
class Patch:
88
"""Handle patches in unified diff format for making changes to the local file system."""
99

10+
# Compile the regular expression used in parse_line method
11+
CHUNK_HEADER_REGEX = re.compile(r"@@ -(\d+),(\d+) \+(\d+),(\d+) @@")
12+
1013
@staticmethod
1114
def apply_patch(patch_string, is_rebuilt=False):
1215
"""Applies a patch to the local file system using git apply."""
@@ -48,7 +51,7 @@ def parse_line(line): # noqa: PLR0911
4851
elif line.startswith("+++"):
4952
return SimpleNamespace(line=line, type="destination_file", parsed=line[6:])
5053
elif line.startswith("@@"):
51-
chunk_header_match = re.match(r"@@ -(\d+),(\d+) \+(\d+),(\d+) @@", line)
54+
chunk_header_match = Patch.CHUNK_HEADER_REGEX.match(line)
5255
if not chunk_header_match:
5356
raise ValueError(f"Invalid chunk header: {line}")
5457

0 commit comments

Comments
 (0)