PageRenderTime 11ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/dispatch/Util.scala

http://scouchdb.googlecode.com/
Scala | 48 lines | 46 code | 2 blank | 0 comment | 15 complexity | 045e903bfac8e8f540d52d09f6ca08f0 MD5 | raw file
 1package dispatch
 2
 3object Util {
 4  def quote(s: String): String = s match {
 5    case null => "null"
 6    case _ => {
 7      new StringBuilder(s.length + 2)
 8        .append('"')
 9        .append(s.foldLeft(new StringBuilder(""))((a, b) => a.append(escape(b, '"'))).toString)
10        .append('"')
11        .toString
12    }
13  }
14  
15  private def escape(c: Char, quoteChar: Char): String = c match {
16    case '"' if (c == quoteChar) => "\\" + c
17    case '"' => "" + c
18    case '\'' if (c == quoteChar) => "\\" + c
19    case '\'' => "" + c
20    case '/' => "\\/"
21    case '\\' => "\\\\"
22    case '\b' => "\\b"
23    case '\f' => "\\f"
24    case '\n' => "\\n"
25    case '\r' => "\\r"
26    case '\t' => "\\t"
27    case x if (x < 32 || x > 126) => {
28      val hex = Integer.toHexString(x)
29      val len = hex.length
30      "\\u" + (if (len < 4) "0000".substring(len) + hex else hex)
31    }
32    case _ => "" + c
33  }
34  
35  import java.io._
36  def readTillNl(in: InputStreamReader): String = {
37    var c = -1
38    var str = new StringBuffer
39    do {
40      c = in.read
41      if (c != '\n' && c != -1) {
42        str.append(c.toChar)
43      }
44    } 
45    while (c != '\n' && c != -1)
46    str.toString
47  }
48}