Mercurial > repo
annotate bin/rot13_stdin @ 11110:a40e8e968f75
<Jafet> ` sed -i \'s/\\r//\' bin/rot13_stdin
author | HackBot |
---|---|
date | Fri, 21 Jul 2017 12:48:55 +0000 |
parents | 0aef5f39601f |
children |
rev | line source |
---|---|
11110 | 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 | |
11109
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
14 |
11110 | 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 |