summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2012-07-26 16:01:27 +0200
committerMartin Odersky <odersky@gmail.com>2012-07-27 17:04:36 +0200
commitb964bac5e295df92d304e97657d01481d63f2d62 (patch)
tree5ceba7e649c5916f0f04dfed11364639f4e29075
parente896c0ab2d36740207f91086262e9feb4fc1c32d (diff)
downloadscala-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.
-rw-r--r--src/library/scala/StringContext.scala24
-rw-r--r--test/files/run/rawstrings.check1
-rw-r--r--test/files/run/rawstrings.scala3
3 files changed, 24 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())
diff --git a/test/files/run/rawstrings.check b/test/files/run/rawstrings.check
new file mode 100644
index 0000000000..36e63594df
--- /dev/null
+++ b/test/files/run/rawstrings.check
@@ -0,0 +1 @@
+[\n\t'"$]
diff --git a/test/files/run/rawstrings.scala b/test/files/run/rawstrings.scala
new file mode 100644
index 0000000000..9df64f6625
--- /dev/null
+++ b/test/files/run/rawstrings.scala
@@ -0,0 +1,3 @@
+object Test extends App {
+ println(raw"[\n\t'${'"'}$$]")
+}