From cc5a8ada9182b4741f90279bd33618344a3b16d7 Mon Sep 17 00:00:00 2001 From: Giampiero <54741923+GLWine@users.noreply.github.com> Date: Tue, 19 Nov 2024 04:12:20 +0100 Subject: [PATCH 1/2] Fix regular expression syntax for ANSI color code filtering - Updated the regular expressions for detecting and filtering ANSI color codes. - Replaced the old string literals with raw string literals (prefix `r`) to avoid potential issues with escape sequences. - This change ensures the regular expressions are interpreted correctly by Python's `re` module, avoiding potential bugs caused by unescaped characters. - By applying this fix, we improve the robustness and readability of the regular expressions, ensuring they work reliably in all scenarios. Effects of not applying the modification: - Without this change, the regular expressions might not handle escape sequences properly, leading to incorrect matching or errors during pattern compilation. - This could cause failures in filtering out ANSI color codes, resulting in incorrect or garbled output in the application. --- bgstally/widgets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bgstally/widgets.py b/bgstally/widgets.py index 22bf3c6..f865043 100644 --- a/bgstally/widgets.py +++ b/bgstally/widgets.py @@ -60,8 +60,8 @@ class to convert text with a limited set of Discord-supported ansi color codes t # define some regexes which will come in handy in filtering # out the ansi color codes - color_pat = re.compile("\x01?\x1b(\[[\d;]*m?)\x02?") - inner_color_pat = re.compile("^\[([\d;]*)m$") + color_pat = re.compile(r'\x01?\x1b(\[[\d;]*m?)\x02?') + inner_color_pat = re.compile(r'^\[([\d;]*)m$') def __init__(self, *args, **kwargs): """ From 96c6549306cf12c553d4280dca234e267fea1b38 Mon Sep 17 00:00:00 2001 From: Giampiero <54741923+GLWine@users.noreply.github.com> Date: Tue, 19 Nov 2024 04:19:28 +0100 Subject: [PATCH 2/2] Fix regex pattern by using raw string literal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated the regular expression pattern in the `string_to_alphanumeric` function to use a raw string literal (prefix `r`). - This ensures that backslashes in the pattern are treated correctly, preventing potential issues with escape sequences. - By making this change, we ensure that the regular expression is interpreted as intended, avoiding any unexpected behavior when cleaning the string. Effects of not applying the modification: - Without this fix, the regular expression might not behave correctly due to escape sequences being misinterpreted by Python’s `re` module. - This could lead to errors in cleaning the string, potentially causing incorrect output or failure to properly remove non-alphanumeric characters. --- bgstally/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bgstally/utils.py b/bgstally/utils.py index 16d023c..3796dfe 100644 --- a/bgstally/utils.py +++ b/bgstally/utils.py @@ -153,5 +153,5 @@ def string_to_alphanumeric(s: str) -> str: Returns: str: The cleaned string """ - pattern: re.Pattern = re.compile('[\W_]+') + pattern: re.Pattern = re.compile(r'[\W_]+') return pattern.sub('', s)