diff options
author | Dominik Gruntz <dominik.gruntz@fhnw.ch> | 2012-03-28 16:48:41 +0200 |
---|---|---|
committer | Dominik Gruntz <dominik.gruntz@fhnw.ch> | 2012-03-28 16:48:41 +0200 |
commit | fdbca65fd1d50582f336e6ebed3466d6073f6f52 (patch) | |
tree | 7b862f0598a7be085a98103125111551aba6815f | |
parent | 017f48e00b863ecfc6a39c16c89a6ccb1dcde13d (diff) | |
download | scala-fdbca65fd1d50582f336e6ebed3466d6073f6f52.tar.gz scala-fdbca65fd1d50582f336e6ebed3466d6073f6f52.tar.bz2 scala-fdbca65fd1d50582f336e6ebed3466d6073f6f52.zip |
SIP-11 String Interpolation Simplification
Changed StringContext.f which implements the formatted
string interpolator.
Any '%' not in formatting position is left in the resulting string literally.
However, instead of adding %s format holders and extending the args with "%"
expressions, these '%' are replaced by '%%'. The formatter then converts '%%'
(percent formatter) to the literal '%' (\u0025). This also simplifies the spec.
The interpolation tests still pass.
-rw-r--r-- | src/library/scala/StringContext.scala | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index 882451680a..06620a82a1 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -79,15 +79,13 @@ case class StringContext(parts: String*) { * start a format specifier), then the string format specifier `%s` is inserted. * * 2. Any `%` characters not in formatting positions are left in the resulting - * string literally. This is achieved by replacing each such occurrence by a string - * format specifier `%s` and adding a corresponding argument string `"%"`. + * string literally. This is achieved by replacing each such occurrence by the string + * format specifier `%%`. */ def f(args: Any*) = { checkLengths(args: _*) val pi = parts.iterator - val ai = args.iterator val bldr = new java.lang.StringBuilder - val args1 = new ArrayBuffer[Any] def copyString(first: Boolean): Unit = { val str = treatEscapes(pi.next()) val strIsEmpty = str.length == 0 @@ -98,23 +96,23 @@ case class StringContext(parts: String*) { bldr append "%s" idx = 1 } - val len = str.length - while (idx < len) { - if (str(idx) == '%') { - bldr append (str substring (start, idx)) append "%s" - args1 += "%" - start = idx + 1 + if (!strIsEmpty) { + val len = str.length + while (idx < len) { + if (str(idx) == '%') { + bldr append (str substring (start, idx)) append "%%" + start = idx + 1 + } + idx += 1 } - idx += 1 + bldr append (str substring (start, idx)) } - if (!strIsEmpty) bldr append (str substring (start, idx)) } copyString(first = true) while (pi.hasNext) { - args1 += ai.next() copyString(first = false) } - bldr.toString format (args1: _*) + bldr.toString format (args: _*) } } |