refactor rot and zero_width stuff
parent
71b3fd1697
commit
9f8924929d
@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Functions that modify and return strings.
|
||||
"""
|
||||
|
||||
import string
|
||||
|
||||
def rot(in_str, n):
|
||||
'''
|
||||
return rot_n of in_str
|
||||
'''
|
||||
return in_str.translate(
|
||||
in_str.maketrans(
|
||||
string.ascii_lowercase,
|
||||
string.ascii_lowercase[n:] + string.ascii_lowercase[:n]
|
||||
)).translate(
|
||||
in_str.maketrans(
|
||||
string.ascii_uppercase,
|
||||
string.ascii_uppercase[n:] + string.ascii_uppercase[:n]
|
||||
))
|
||||
|
||||
def string2bin(in_str):
|
||||
return ''.join(format(ord(x), 'b').zfill(8) for x in in_str)
|
||||
|
||||
def bin2string(in_str):
|
||||
return ''.join(chr(int(in_str[_*8:_*8+8], 2)) for _ in range(len(in_str)//8))
|
||||
|
||||
def zero_width_string(in_str):
|
||||
'''
|
||||
return the ASCII string encoded in non printing spaces
|
||||
'''
|
||||
return ''.join(u'\u200b' if x == '1' else u'\u200d' for x in string2bin(in_str))
|
||||
|
||||
def zero_width_string_decode(in_str):
|
||||
return bin2string(''.join('1' if x == '\u200b' else '0' for x in in_str))
|
||||
|
||||
def main():
|
||||
pass
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue