From 0e4c318c19aa2a96ecd8f761d1ab04c900706909 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Fri, 22 Jul 2022 00:01:02 +0200 Subject: [PATCH] optimised regex used in bmg layer (100 seconds -> 1.7 seconds for Regex) (2 seconds -> 0.7 seconds for Original Track) --- .../Layer/FormatOriginalTrackLayer.py | 26 +++++++++------- .../BmgTxtEditor/Layer/RegexLayer.py | 30 ++++++++----------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py index e76f18a..6abf99f 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/FormatOriginalTrackLayer.py @@ -18,15 +18,14 @@ class FormatOriginalTrackLayer(AbstractLayer): self.template = template def patch_bmg(self, patch: "Patch", decoded_content: str) -> str: - originals_track = bmg.cat_data(decoded_content, filters=["TRACKS+ARENAS"]) - new_bmg_lines: list[str] = [] - - for line in originals_track.split("\n"): - if (match := re.match(r"^ {2}(?P.*?)\t= (?P.*)$", line, re.DOTALL)) is None: - # check if the line match a bmg definition, else ignore - # bmg definition is : 2 spaces, a bmg id, a tab, an equal sign, a space and the bmg text - continue + original_tracks = bmg.cat_data(decoded_content, filters=["TRACKS+ARENAS"]) + def replacement(match: re.Match) -> str: + """ + Get the replacement for the bmg line + :param match: the matched bmg line + :return: the patched bmg line + """ id = match.group("id") name = match.group("value") @@ -43,7 +42,12 @@ class FormatOriginalTrackLayer(AbstractLayer): self.template ) - new_bmg_lines.append(f" {id}\t={patched_name}") + return f" {id}\t= {patched_name}" - return decoded_content + "\n" + ("\n".join(new_bmg_lines)) + "\n" - # add every new line to the end of the decoded_bmg, old bmg_id will be overwritten. + patched_original_tracks = re.sub( + r" {2}(?P.*?)\t= (?P.*)", + replacement, + original_tracks + ) + + return f"{decoded_content}\n{patched_original_tracks}\n" diff --git a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py index fbf5f95..f9f8f34 100644 --- a/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py +++ b/source/mkw/Patch/PatchOperation/Operation/BmgTxtEditor/Layer/RegexLayer.py @@ -17,27 +17,23 @@ class RegexLayer(AbstractLayer): self.template = template def patch_bmg(self, patch: "Patch", decoded_content: str) -> str: - # TODO : use regex in a better way to optimise speed - - new_bmg_lines: list[str] = [] - for line in decoded_content.split("\n"): - if (match := re.match(r"^ {2}(?P.*?)\t= (?P.*)$", line, re.DOTALL)) is None: - # check if the line match a bmg definition, else ignore - # bmg definition is : 2 spaces, a bmg id, a tab, an equal sign, a space and the bmg text - continue - - new_bmg_id: str = match.group("id") - new_bmg_def: str = match.group("value") + def replacement(match: re.Match) -> str: + """ + Get the replacement for the bmg line + :param match: the matched bmg line + :return: the patched bmg line + """ + id: str = match.group("id") + value: str = match.group("value") for pattern, repl in self.template.items(): - new_bmg_def = re.sub( + value = re.sub( pattern, patch.safe_eval(repl, multiple=True), - new_bmg_def, + value, flags=re.DOTALL ) - # match a pattern from the template, and replace it with its repl - new_bmg_lines.append(f" {new_bmg_id}\t={new_bmg_def}") + return f" {id}\t={value}" + + return re.sub(r" {2}(?P.*?)\t= (?P.*)", replacement, decoded_content) - return decoded_content + "\n" + ("\n".join(new_bmg_lines)) + "\n" - # add every new line to the end of the decoded_bmg, old bmg_id will be overwritten.