-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuacUtils.py
63 lines (43 loc) · 1.36 KB
/
GuacUtils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import List, Optional
def guac_decode(string: str) -> Optional[List[str]]:
"""Implementation of guacamole decoder
Example: guac_decode(\"4.chat,5.hello\") -> [\"chat\", \"hello\"]"""
if not string:
return []
idx: int = 0
distance: int
result: List[str] = []
chars: List[str] = list(string)
while True:
dist_str: str = ""
while chars[idx].isdecimal():
dist_str += chars[idx]
idx = idx + 1
if idx >= 1:
idx -= 1
if not dist_str.isdigit():
return None
distance = int(dist_str)
idx += 1
if chars[idx] != ".":
return None
idx += 1
addition: str = ""
for num in range(idx, idx + distance):
addition += chars[num]
result.append(addition)
idx += distance
if idx >= len(chars):
return None
if chars[idx] == ",":
pass
elif chars[idx] == ";":
break
else:
return None
idx += 1
return result
def guac_encode(*args: str) -> str:
"""Implementation of guacamole encoder
Example: guac_encode(\"chat\", \"hello\") -> \"4.chat,5.hello;\" """
return f"{','.join(f'{len(arg)}.{arg}' for arg in args)};"