Skip to content

Commit

Permalink
s interpolator constantenates
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Apr 5, 2023
1 parent b7d4a61 commit 59f62b5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/compiler/scala/tools/reflect/FastStringInterpolator.scala
Expand Up @@ -12,6 +12,8 @@

package scala.tools.reflect

import scala.PartialFunction.cond

trait FastStringInterpolator extends FormatInterpolator {
import c.universe._

Expand Down Expand Up @@ -57,7 +59,20 @@ trait FastStringInterpolator extends FormatInterpolator {
case iue: StringContext.InvalidUnicodeEscapeException => c.abort(parts.head.pos.withShift(iue.index), iue.getMessage)
}

concatenate(treated, args)
if (args.forall(cond(_) { case Literal(c @ Constant(_)) => c.isSuitableLiteralType })) {
val it1 = treated.iterator
val it2 = args.iterator
val res = new StringBuilder
def add(t: Tree) = res.append(t.asInstanceOf[Literal].value.value)
add(it1.next())
while (it2.hasNext) {
add(it2.next())
add(it1.next())
}
val k = Constant(res.toString)
Literal(k).setType(ConstantType(k))
}
else concatenate(treated, args)

// Fallback -- inline the original implementation of the `s` or `raw` interpolator.
case t@Apply(Select(someStringContext, _interpol), args) =>
Expand Down

0 comments on commit 59f62b5

Please sign in to comment.