Mercurial > repo
annotate bin/rot13_stdin @ 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 | |
children | a40e8e968f75 |
rev | line source |
---|---|
11109
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
1 #!/usr/bin/env python |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
2 |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
3 import re, sys, unicodedata as U |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
4 def tr(c): |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
5 m = re.match(r'(.*\bLATIN\b.*\bLETTER )([A-Z])\b(.*)', U.name(c, '')) |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
6 if m: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
7 p, q, r = m.groups() |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
8 n = ord(q) - ord('A') |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
9 try: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
10 return U.lookup(p + chr(ord('A') + (n + 13) % 26) + r) |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
11 except KeyError: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
12 return c |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
13 return c |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
14 |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
15 def tr2(c): |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
16 d = tr(c) |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
17 if d != c: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
18 return d |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
19 else: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
20 cs = [unichr(int(x, 16)) for x in U.decomposition(c).split()] |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
21 ds = [tr(x) for x in cs] |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
22 if cs != ds: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
23 return u''.join(ds) |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
24 else: |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
25 return c |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
26 |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
27 for c in unicode(sys.stdin.read(), 'utf8'): |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
28 sys.stdout.write(tr2(c).encode('utf8')) |
0aef5f39601f
<Jafet> ` mv bin/rot13{,_stdin} && echo \'print_args_or_input "$@" | rot13_stdin\' >bin/rot13 && chmod +x bin/rot13
HackBot
parents:
diff
changeset
|
29 |