-
Notifications
You must be signed in to change notification settings - Fork 0
/
hh_table.py
58 lines (41 loc) · 1.8 KB
/
hh_table.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
import json
from typing import List
class HHTable:
def __init__(self, name: str) -> None:
with open(f"tables/{name}.json") as f:
input_json = json.load(f)
self.conversion_table = {}
self.missing_symbol_id = None
for symbol_data in input_json["ConversionTable"]:
# WHYYYYYYYY
hex_string = symbol_data.get("hexstring")
if not hex_string:
hex_string = symbol_data["hexString"]
# hedgeturd why hex string... why not integer
# you can easily convert it to hex in c# :skull:
id = int.from_bytes(bytes.fromhex(hex_string), "big")
symbol = symbol_data["letter"]
if symbol == "newline": symbol = "\n"
elif symbol == "quote": symbol = '"'
elif symbol == "?": self.missing_symbol_id = id
self.conversion_table[id] = symbol
def get_symbol_by_id(self, id: int) -> str:
if id not in self.conversion_table.keys():
return "?"
return self.conversion_table[id]
def get_id_by_symbol(self, symbol: str) -> int:
if symbol not in self.conversion_table.values():
return self.missing_symbol_id
for id, char in self.conversion_table.items():
if symbol == char:
return id
def convert_symbols_to_string(self, symbols: List[int]) -> str:
result = ""
for id in symbols:
result += self.get_symbol_by_id(id)
return result
def convert_string_to_symbols(self, string: str) -> List[int]:
result = []
for char in string:
result.append(self.get_id_by_symbol(char))
return result