comparison bin/rot13 @ 11111:ea74d235ff72

<oerjan> ` mv bin/rot13{_stdin,}
author HackBot
date Sat, 22 Jul 2017 00:40:56 +0000
parents 0aef5f39601f
children 6412d84cf279
comparison
equal deleted inserted replaced
11110:a40e8e968f75 11111:ea74d235ff72
1 print_args_or_input "$@" | rot13_stdin 1 #!/usr/bin/env python
2
3 import re, sys, unicodedata as U
4 def tr(c):
5 m = re.match(r'(.*\bLATIN\b.*\bLETTER )([A-Z])\b(.*)', U.name(c, ''))
6 if m:
7 p, q, r = m.groups()
8 n = ord(q) - ord('A')
9 try:
10 return U.lookup(p + chr(ord('A') + (n + 13) % 26) + r)
11 except KeyError:
12 return c
13 return c
14
15 def tr2(c):
16 d = tr(c)
17 if d != c:
18 return d
19 else:
20 cs = [unichr(int(x, 16)) for x in U.decomposition(c).split()]
21 ds = [tr(x) for x in cs]
22 if cs != ds:
23 return u''.join(ds)
24 else:
25 return c
26
27 for c in unicode(sys.stdin.read(), 'utf8'):
28 sys.stdout.write(tr2(c).encode('utf8'))
29