Mercurial > repo
comparison bin/rot13 @ 11109:0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
author | HackBot |
---|---|
date | Fri, 21 Jul 2017 12:38:28 +0000 |
parents | 8f3c7e3a6f51 |
children | ea74d235ff72 |
comparison
equal
deleted
inserted
replaced
11108:8f3c7e3a6f51 | 11109:0aef5f39601f |
---|---|
1 #!/usr/bin/env python | 1 print_args_or_input "$@" | rot13_stdin |
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 |