fix(commands/utils/tokenize): properly handle multiple quotes
This commit is contained in:
parent
b579fc56ee
commit
db355e8ade
@ -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:
|
|
||||||
if current_token and current_token[0] == char:
|
|
||||||
in_quotes = False
|
|
||||||
else:
|
|
||||||
in_quotes = True
|
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:
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user