rewrite (and vastly simplify) message format tokenising

This commit is contained in:
jesopo 2020-04-08 14:18:42 +01:00
parent 2b0dfcc155
commit f16526c60d

View file

@ -75,48 +75,34 @@ FORMAT_TOKENS = [
FORMAT_STRIP = [ FORMAT_STRIP = [
"\x08" # backspace "\x08" # backspace
] ]
def _format_tokens(s: str) -> typing.List[str]: def _format_tokens(s: str) -> typing.List[str]:
is_color = False is_color = False
foreground: typing.List[str] = [] foreground: typing.List[str] = []
background: typing.List[str] = [] background: typing.List[str] = []
is_background = False is_background = False
matches = [] # type: typing.List[str] tokens: typing.List[str] = []
for i, char in enumerate(s): s_copy = list(s)
last_char = i == len(s)-1 while s_copy:
if is_color: token = s_copy.pop(0)
current_color = background if is_background else foreground if token == "\x03":
color_finished = True for i in range(2):
if s_copy and s_copy[0].isdigit():
token += s_copy.pop(0)
if (len(s_copy) > 1 and
s_copy[0] == "," and
s_copy[1].isdigit()):
token += s_copy.pop(0)
token += s_copy.pop(0)
if s_copy and s_copy[0].isdigit():
token += s_copy.pop(0)
if char == "," and not is_background: tokens.append(token)
is_background = True elif (token in FORMAT_TOKENS or
color_finished = False token in FORMAT_STRIP):
tokens.append(token)
elif char.isdigit() and len(current_color) < 2: return tokens
current_color.append(char)
color_finished = len(current_color) == 2 and is_background
if color_finished or last_char:
color = "".join(foreground)
if background:
color += "".join([","]+background)
matches.append("\x03%s" % color)
is_color = False
foreground = []
background = []
is_background = False
if char == consts.COLOR:
if is_color:
matches.append(char)
else:
is_color = True
elif char in FORMAT_TOKENS:
matches.append(char)
elif char in FORMAT_STRIP:
matches.append(char)
return matches
def _color_match(code: typing.Optional[str], foreground: bool) -> str: def _color_match(code: typing.Optional[str], foreground: bool) -> str:
if not code: if not code: