-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstringutil.py
More file actions
36 lines (29 loc) · 752 Bytes
/
Copy pathstringutil.py
File metadata and controls
36 lines (29 loc) · 752 Bytes
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
import re
import ast
def clean_str(s):
c=re.sub("[\x00-\x1F]", lambda x: "\\x%02X"%ord(x.group(0)), s.replace("\0", "").replace("\\","\\\\"))
return c
RE_special = re.compile("(\\\\\\\\|\\\\x..)")
def decode(x):
x=x.group(0)
if x=="\\\\":
return "\\"
else:
return chr(eval("0"+x[1:]))
def unclean_str(s):
return RE_special.sub(decode, s)
if __name__ == "__main__":
test="test\\test \n\1\2\3\26\32 demo\tdemo"
print (test)
test2=clean_str(test)
print (test2)
test3=unclean_str(test2)
print (test3)
print (test==test3)
test='Re: \x1a\x1a\x1a\x1a'
print (test)
test2=clean_str(test)
print (test2)
test3=unclean_str(test2)
print (test3)
print (test==test3)