-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.scala
More file actions
53 lines (43 loc) · 1.54 KB
/
StringUtils.scala
File metadata and controls
53 lines (43 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.encalmo.utils
import scala.quoted.*
object StringUtils {
/** Concatenate a list of terms to a string. */
def concat(using
cache: StatementsCache
)(term: cache.quotes.reflect.Term, terms: cache.quotes.reflect.Term*): cache.quotes.reflect.Term = {
import cache.quotes.reflect.*
given cache.quotes.type = cache.quotes
val stringPlusSym = defn.StringClass
.methodMember("+")
.find { sym =>
// We want the overload: def +(x: Any): String
sym.paramSymss match {
case List(List(param)) => true
case _ => false
}
}
.getOrElse(report.errorAndAbort("Could not find String.+ method"))
terms.foldLeft(term) { (acc, term) =>
term match {
case literal @ Literal(StringConstant(string)) =>
Apply(Select(acc, stringPlusSym), List(literal))
case _ =>
Apply(Select(acc, stringPlusSym), List(applyToString(term)))
}
}
}
/** Best effort to convert a term to a string. */
def applyToString(using cache: StatementsCache)(term: cache.quotes.reflect.Term): cache.quotes.reflect.Term = {
given cache.quotes.type = cache.quotes
import cache.quotes.reflect.*
term.tpe match {
case t if t <:< TypeRepr.of[String] => term
case _ =>
val sym = term.tpe.dealias.widen.typeSymbol
sym.methodMember("toString").match {
case toStringSym :: _ => Apply(Select(term, toStringSym), Nil)
case Nil => term
}
}
}
}