Created
December 7, 2012 15:26
-
-
Save audreyt/4233938 to your computer and use it in GitHub Desktop.
ROT encode/decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lsc | |
# 使用前請先 curl -O http://cpansearch.perl.org/src/DANKOGAI/Unicode-Unihan-0.04/srctxt/Mandarin.txt | |
require! fs | |
c2p = {}; p2c = {} | |
for l in "#{fs.read-file-sync 'Mandarin.txt'}" / /\n/ | |
[code, pin] = l / /\s+/ | |
continue unless code is /^....$/ # BMP | |
char = String.fromCharCode parseInt code, 16 | |
c2p[char] = pin | |
(p2c[pin] ?= [])push char | |
encode = -> (* '') <| for c in it | |
p = c2p[c] | |
if not p then c else | |
cs = p2c[p] | |
idx = (cs.indexOf c) + 1 | |
idx = 0 if idx is cs.length | |
cs[idx] | |
decode = -> (* '') <| for c in it | |
p = c2p[c] | |
if not p then c else | |
cs = p2c[p] | |
idx = (cs.indexOf c) - 1 | |
idx = cs.length - 1 if idx is -1 | |
cs[idx] | |
console.log encode "主席:黃委員還是堅持刪減200萬?但我們委員從原先的全刪,改為一成,後來又改為5%,現在又改為500萬,而經過本席折 衝,建議減列300萬。我再最後一次詢問各位,是否互相折衝一下,減列300萬?" | |
console.log decode "嘱棤:黄娓园鍰昰奸歭剼湕200蟃?僤捰们娓园悰厡嘕的啳剼,絠煟伊承,洉俫右絠煟5%,県扗右絠煟500蟃,耏经㳀楍棤摺 蹖,徤讛湕劣300蟃。捰在栬洉伊絘询妏嗰卫,昰殕冱箱摺蹖伊吓,湕劣300蟃?" |
Author
audreyt
commented
Dec 7, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment