diff options
author | Martin Odersky <odersky@gmail.com> | 2012-07-26 16:01:27 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2012-07-27 17:04:36 +0200 |
commit | b964bac5e295df92d304e97657d01481d63f2d62 (patch) | |
tree | 5ceba7e649c5916f0f04dfed11364639f4e29075 /src/library | |
parent | e896c0ab2d36740207f91086262e9feb4fc1c32d (diff) | |
download | scala-b964bac5e295df92d304e97657d01481d63f2d62.tar.gz scala-b964bac5e295df92d304e97657d01481d63f2d62.tar.bz2 scala-b964bac5e295df92d304e97657d01481d63f2d62.zip |
Raw string interpolator
Adds a raw string interpolator raw"..." which does not do any escape sequence processing.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/StringContext.scala | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index f11dfb72ae..7d37fa4aa1 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -26,7 +26,7 @@ case class StringContext(parts: String*) { * @param `args` The arguments to be checked. * @throws An `IllegalArgumentException` if this is not the case. */ - def checkLengths(args: Any*): Unit = + def checkLengths(args: Seq[Any]): Unit = if (parts.length != args.length + 1) throw new IllegalArgumentException("wrong number of arguments for interpolated string") @@ -42,11 +42,27 @@ case class StringContext(parts: String*) { * @throws A `StringContext.InvalidEscapeException` if if a `parts` string contains a backslash (`\`) character * that does not start a valid escape sequence. */ - def s(args: Any*): String = { - checkLengths(args: _*) + def s(args: Any*): String = standardInterpolator(treatEscapes, args) + + /** The raw string interpolator. + * + * It inserts its arguments between corresponding parts of the string context. + * As opposed to the simple string interpolator `s`, this one does not treat + * standard escape sequences as defined in the Scala specification. + * @param `args` The arguments to be inserted into the resulting string. + * @throws An `IllegalArgumentException` + * if the number of `parts` in the enclosing `StringContext` does not exceed + * the number of arguments `arg` by exactly 1. + * @throws A `StringContext.InvalidEscapeException` if if a `parts` string contains a backslash (`\`) character + * that does not start a valid escape sequence. + */ + def raw(args: Any*): String = standardInterpolator(identity, args) + + def standardInterpolator(process: String => String, args: Seq[Any]): String = { + checkLengths(args) val pi = parts.iterator val ai = args.iterator - val bldr = new java.lang.StringBuilder(treatEscapes(pi.next())) + val bldr = new java.lang.StringBuilder(process(pi.next())) while (ai.hasNext) { bldr append ai.next bldr append treatEscapes(pi.next()) |