Files
python-client-sdk/revolt/ext/commands/view.py
T
2023-05-20 03:04:52 +01:00

54 lines
1.3 KiB
Python
Executable File

from typing import Iterator
from .errors import NoClosingQuote
class StringView:
def __init__(self, string: str):
self.value: Iterator[str] = iter(string)
self.temp: str = ""
self.should_undo: bool = False
def undo(self) -> None:
self.should_undo = True
def next_char(self) -> str:
return next(self.value)
def get_rest(self) -> str:
if self.should_undo:
return f"{self.temp} {''.join(self.value)}"
return "".join(self.value)
def get_next_word(self) -> str:
if self.should_undo:
self.should_undo = False
return self.temp
char = self.next_char()
temp: list[str] = []
while char == " ":
char = self.next_char()
if char in ["\"", "'"]:
quote = char
try:
while (char := self.next_char()) != quote:
temp.append(char)
except StopIteration:
raise NoClosingQuote
else:
temp.append(char)
try:
while (char := self.next_char()) not in " \n":
temp.append(char)
except StopIteration:
pass
output = "".join(temp)
self.temp = output
return output