Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 2, 2010 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kmizu/387103 to your computer and use it in GitHub Desktop.
Save kmizu/387103 to your computer and use it in GitHub Desktop.
object StringDump{
class DumpableString(str: String) {
def dump = StringDump.dump(str)
}
implicit def enrichString(str: String) = new DumpableString(str)
def dump(str:String):String={
str.getBytes("UTF-8").map{
case 34 => "\\\""
case 92 => "\\"
case n if 32 <= n && n <= 126 => new String(Array[Byte](n))
case n if n < 0 => "\\%03o" format (256+n)
case n => "\\%03o" format n
}.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