fix(commands/utils/tokenize): properly handle multiple quotes

This commit is contained in:
Ryan 2024-12-30 18:57:23 -05:00
parent b579fc56ee
commit db355e8ade
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3

View File

@ -39,30 +39,30 @@ def match(command: str) -> None | list[Command]:
def tokenize(string: str) -> list[str]: def tokenize(string: str) -> list[str]:
tokens = [] tokens = []
current_token = [] token = ""
in_quotes = False in_quotes = False
escape_next = False quote_char = None
escape = False
for char in string[len(constants.PREFIX) :]: for char in string[len(constants.PREFIX) :]:
if escape_next: if escape:
current_token.append(char) token += char
escape_next = False escape = False
elif char == "\\": elif char == "\\":
escape_next = True escape = True
elif char in ['"', "'"]: elif char in ('"', "'") and not in_quotes:
if in_quotes: in_quotes = True
if current_token and current_token[0] == char: quote_char = char
in_quotes = False elif char == quote_char and in_quotes:
else: in_quotes = False
in_quotes = True quote_char = None
elif char.isspace() and not in_quotes: elif char.isspace() and not in_quotes:
if current_token: if token:
tokens.append("".join(current_token)) tokens.append(token)
current_token = [] token = ""
else: else:
current_token.append(char) token += char
if current_token:
tokens.append("".join(current_token))
if token:
tokens.append(token)
return tokens return tokens