Skip to content

Instantly share code, notes, and snippets.

@ymnk
Forked from kmizu/StringDump.scala
Created May 2, 2010 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ymnk/387135 to your computer and use it in GitHub Desktop.
Save ymnk/387135 to your computer and use it in GitHub Desktop.
object StringDump{
implicit def enrichString(str: String) = new {
def dump:String={
str.getBytes("UTF-8").map{ _.asInstanceOf[Char] match{
case n @ ('"' | '\\') => "\\" + n
case n if ' ' <= n && n <= '~' => n
case n => "\\%03o" format (n&0xff)
}}.mkString("\"","","\"")
}
}
def main(args: Array[String])={
println(args(0).dump)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment