From db355e8ade0d400b31b1819945d37d00e36d6d5c Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Mon, 30 Dec 2024 18:57:23 -0500 Subject: [PATCH] fix(commands/utils/tokenize): properly handle multiple quotes --- commands/utils.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/commands/utils.py b/commands/utils.py index 631c39f..53a3430 100644 --- a/commands/utils.py +++ b/commands/utils.py @@ -39,30 +39,30 @@ def match(command: str) -> None | list[Command]: def tokenize(string: str) -> list[str]: tokens = [] - current_token = [] + token = "" in_quotes = False - escape_next = False + quote_char = None + escape = False for char in string[len(constants.PREFIX) :]: - if escape_next: - current_token.append(char) - escape_next = False + if escape: + token += char + escape = False elif char == "\\": - escape_next = True - elif char in ['"', "'"]: - if in_quotes: - if current_token and current_token[0] == char: - in_quotes = False - else: - in_quotes = True + escape = True + elif char in ('"', "'") and not in_quotes: + in_quotes = True + quote_char = char + elif char == quote_char and in_quotes: + in_quotes = False + quote_char = None elif char.isspace() and not in_quotes: - if current_token: - tokens.append("".join(current_token)) - current_token = [] + if token: + tokens.append(token) + token = "" else: - current_token.append(char) - - if current_token: - tokens.append("".join(current_token)) + token += char + if token: + tokens.append(token) return tokens