如何解释多行scala字符串中的转义序列?

时间:2013-07-25 21:00:46

标签: scala escaping multiline string-interpolation

简而言之:

"""I want to be able to
  |have the convenient formatting of a multiline string,
  |while using inline escape sequences\r\r\b\\
  |
  |How can this be done?""".stripMargin

3 个答案:

答案 0 :(得分:11)

我能想到两个选项:

  1. 您可以直接使用StringContext.treatEscapes

    StringContext.treatEscapes("""I want to be able to
            |have the convenient formatting of a multiline string,
            |while using inline escape sequences\r\r\b\\
            |
            |How can this be done?""".stripMargin)
    
  2. 如果“简单插补器”(s)的变量替换功能不会破坏您的需求,请尝试将string interpolation(转换转义字符)与{{1 - 引号(不会转义......):

    """

    输出

    println("""he\\lo\nworld""")
    println(s"""he\\lo\nworld""")
    

    有关详细信息,请参阅relevant SIPthis earlier question

答案 1 :(得分:3)

除了其他答案 - 这个想法怎么样?

s"""I want to be able to
  |have the convenient formatting of a multiline string,
  |while using inline escape sequences${"\r\r\b\"}\
  |
  |How can this be done?""".stripMargin

唯一不起作用的是行尾的\。 在我的示例中,您将转义的字符嵌入为普通字符串,然后使用字符串插值来插入它们。 (在打开三重引号之前注意s。)

答案 2 :(得分:2)

标准库中隐藏了一个方便的示例。可以通过几种方式轻松调整以添加标准处理。然而,嵌入式\r的意图并不明显,因此caveat interpolator

更新:对于记录,困难的部分是忘记了序列arg _*。因为它是Any *,所以没有类型错误;底层插补器只会抛出一个错误,即部件与args不匹配。

更新:修复了下划线星号,因此它没有斜体。

示例:

import reflect.internal.util.StripMarginInterpolator

object Test extends App {

  trait ZipMarginator extends StripMarginInterpolator {
    def zm(args: Any*): String = StringContext treatEscapes sm(args: _*)
  }
  implicit class ZipMarginOps(val stringContext: StringContext) extends ZipMarginator
  val sample =
zm"""I want to be able to
    |have the convenient formatting of a multiline string,
    |while using inline escape sequences
    |like\t\ttabs and \\Program Files\\backslashes.
    |
    |How can this be done?"""
  Console println sample

  implicit class ZipMarginOps2(val stringContext: StringContext) extends SStripMarginInterpolator {
    def sz(args: Any*): String = ssm(args: _*)
  }
  Console println sz"""
    |Another\t\texample."""
  Console println sz"""
    |Another\r\tex\nample.
    |Huh?"""

}

这是StripMargin ..或更改名称以保护一个人的理智,请注意关于raw的警告:

trait SStripMarginInterpolator {
  def stringContext: StringContext

  /**
   * A safe combination of [[scala.collection.immutable.StringLike#stripMargin]]
   * and [[scala.StringContext#raw]].
   *
   * The margin of each line is defined by whitespace leading up to a '|' character.
   * This margin is stripped '''before''' the arguments are interpolated into to string.
   *
   * String escape sequences are '''not''' processed; this interpolater is designed to
   * be used with triple quoted Strings.
   *
   * {{{
   * scala> val foo = "f|o|o"
   * foo: String = f|o|o
   * scala> sm"""|${foo}
   *             |"""
   * res0: String =
   * "f|o|o
   * "
   * }}}
   */
  final def ssm(args: Any*): String = {
    def isLineBreak(c: Char) = c == '\n' || c == '\f' // compatible with StringLike#isLineBreak
    def stripTrailingPart(s: String) = {
      val (pre, post) = s.span(c => !isLineBreak(c))
      pre + post.stripMargin
    }
    val stripped: List[String] = stringContext.parts.toList match {
      case head :: tail => head.stripMargin :: (tail map stripTrailingPart)
      case Nil => Nil
    }
    new StringContext(stripped: _*).s(args: _*)  // <= MODIFIED for s instead of raw
  }
}