-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext_en.py
73 lines (56 loc) · 2.4 KB
/
text_en.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
64
65
66
67
68
69
70
71
72
73
import re
from helper import snow_encode,snow_decode,emoji_encode,emoji_decode,subs_encode,subs_decode,zw_encode,zw_decode,zwc_4
def txt_encode(unencoded_string, msg, method="zw", binary=False, replacements=None, delimiter=None):
'''
Main encoding method
Dispatches to corresponding encoder based on specified method and handles
insertion/appending etc. of message into the string.
'''
if method == "zw":
code = zw_encode(msg, character_set=replacements, binary=binary)
chars = list(unencoded_string)
split_code = [code[i:i+4] for i in range(0, len(code), 4)]
if len(split_code) >= len(chars):
raise ValueError("String too short to encode message")
out = ''
for i in range(len(chars)):
out = out + chars[i]
if i < len(split_code):
out = out + split_code[i]
return out
elif method == "snow":
if not delimiter:
delimiter = '\t\t\t'
code = snow_encode(msg, character_set=replacements, binary=binary)
return unencoded_string + delimiter + code
elif method == "lookalike":
return subs_encode(unencoded_string, msg, substitution_table=replacements, binary=binary)
elif method == "emoji":
return emoji_encode(msg, binary=binary)
else:
raise Exception("Method: {}, is not supported".format(method))
def txt_decode(encoded_string, method="zw", binary=False, replacements=None, delimiter=None):
'''
Main decoding method
Dispatches to corresponding decoder based on specified method and handles
extraction of encoded message from the string.
'''
if method == "zw":
if not replacements:
replacements = zwc_4
code = ''
for c in encoded_string:
if c in replacements:
code = code + c
return zw_decode(code, character_set=replacements, binary=binary)
elif method == "snow":
if not delimiter:
delimiter = '\t\t\t'
regex = "{}(.+)$".format(delimiter)
m = re.search(regex, encoded_string)
code = m.groups()[0]
return snow_decode(code, character_set=replacements, binary=binary)
elif method == "lookalike":
return subs_decode(encoded_string, substitution_table=replacements, binary=binary)
elif method == "emoji":
return emoji_decode(encoded_string, binary=binary)