Created
July 13, 2011 07:16
-
-
Save yuroyoro/1079871 to your computer and use it in GitHub Desktop.
謎言語ウキャス
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
/** | |
* 謎言語ウキャス | |
* こんにちわ → ᓦᐓᔦᐓᓾᐓᓴᐓᔢᐓ | |
* 吾輩は猫である。名前はまだ無い。 → ᗟᐡᓢᐹᔂᐓᑙᐮᓺᐓᓕᐓᔞᐓ。ᖮᐡ᙭ᐠᔂᐓᔑᐓᓳᐓᓎᐭᓗᐓ。 | |
* 謎言語ウキャスは、@yuroyoroがScalaでテキトーに10分くらいで実装しました。 | |
* → ᗅᐷᒷᐷᕕᐷᔹᐓᕀᐓᕶᐓᕌᐓᔂᐓ、@yuroyoroᓟᐓScalaᓺᐓᕙᐓᕀᐓᕛᐓᖏᐓᓾᐓ10ᘦᐠᓢᐓᔜᐓᓗᐓᓺᐓᗃᐤᗻᐶᓪᐓᔑᐓᓪᐓᓲᐓ。 | |
* | |
*/ | |
object UCAS extends App{ | |
import java.text.Normalizer | |
import java.lang.Character.UnicodeBlock | |
def toUcasChar(c:Char) = { | |
def g(n:Int):Char = (n + 5120).toChar | |
def worker(xs:Seq[Char], n:Int):Seq[Char] = if( n < 639 ) xs :+ g(n) else worker( xs :+ g(n % 639), n / 639) | |
if( isHKC(c) ) worker(Seq.empty[Char], c.toInt) else Seq(c) | |
} | |
def fromUcasChar(a:Char, b:Char) = { | |
(( (b.toInt - 5120) * 639 ) + a.toInt - 5120 ).toChar | |
} | |
def isHKC(c:Char) = UnicodeBlock.of(c) match { | |
case UnicodeBlock.HIRAGANA | UnicodeBlock.KATAKANA | UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS => true | |
case _ => false | |
} | |
def isUcas(c:Char) = UnicodeBlock.of(c) == UnicodeBlock.UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS | |
def encode(s:String) = { | |
Normalizer.normalize(s, Normalizer.Form.NFKC).map{ toUcasChar }.flatten.mkString | |
} | |
def decode(s:String) = { | |
def worker( xs:List[Char], res:List[Char]):List[Char] = xs match { | |
case a::b::t if isUcas(a) && isUcas(b) => worker( t, res :+ fromUcasChar(a, b)) | |
case h::t => worker(t, res :+ h) | |
case _ => res | |
} | |
worker( s.toList, List.empty[Char] ).mkString | |
} | |
def rand(n:Int = 140) = { | |
scala.io.Codec.toUTF8 ((1 to n).map{_ => (util.Random.nextInt(639) + 5120).toChar}) | |
} | |
args.headOption.foreach{ cmd => cmd match { | |
case "rand" => | |
val n = args.tail.headOption.map{ _.toInt}.getOrElse(140) | |
rand(n).foreach{b => java.lang.System.out.write(b)} | |
case "decode" => | |
args.tail.foreach{ s => println(decode(s)) } | |
case "encode" => | |
args.tail.foreach{ s => println(encode(s)) } | |
case _ => | |
println("""usage: scala UCAS (rand|decode|encode) <strings...>""") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment