def escape()

in finagle-http/src/main/scala/com/twitter/finagle/http/filter/LoggingFilter.scala [24:63]


  def escape(s: String): String = {
    var builder: StringBuilder = null // only create if escaping is needed
    var index = 0
    s.foreach { c =>
      val i = c.toInt
      if (i >= 0x20 && i <= 0x7e && i != 0x22 && i != 0x5c) {
        if (builder == null) {
          index += 1 // common case
        } else {
          builder.append(c)
        }
      } else {
        if (builder == null) {
          builder = new StringBuilder(s.substring(0, index))
        }
        c match {
          case '\b' => builder.append("\\b")
          case '\n' => builder.append("\\n")
          case '\r' => builder.append("\\r")
          case '\t' => builder.append("\\t")
          case BackslashV => builder.append("\\v")
          case '\\' => builder.append("\\\\")
          case '"' => builder.append("\\\"")
          case _ =>
            c.toString().getBytes("UTF-8").foreach { byte =>
              builder.append("\\x")
              val s = java.lang.Integer.toHexString(byte & 0xff)
              if (s.length == 1)
                builder.append("0")
              builder.append(s)
            }
        }
      }
    }
    if (builder == null) {
      s // common case: nothing needed escaping
    } else {
      builder.toString
    }
  }