File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
library/src/scala/tasty/util Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change 1
1
package scala .tasty
2
2
package util
3
3
4
+ import scala .annotation .switch
5
+
4
6
class ShowSourceCode [T <: Tasty with Singleton ](tasty0 : T ) extends Show [T ](tasty0) {
5
7
import tasty ._
6
8
@@ -591,8 +593,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
591
593
case Constant .Long (v) => this += v += " L"
592
594
case Constant .Float (v) => this += v
593
595
case Constant .Double (v) => this += v
594
- case Constant .Char (v) => this += '\' ' += v.toString += '\' ' // TODO escape char
595
- case Constant .String (v) => this += '"' += v.toString += '"' // TODO escape string
596
+ case Constant .Char (v) => this += '\' ' += escapedChar(v) += '\' '
597
+ case Constant .String (v) => this += '"' += escapedString(v) += '"'
596
598
}
597
599
598
600
def printTypeOrBoundsTree (tpt : TypeOrBoundsTree ): Buffer = tpt match {
@@ -770,6 +772,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
770
772
def += (x : Char ): this .type = { sb.append(x); this }
771
773
def += (x : String ): this .type = { sb.append(x); this }
772
774
775
+ @ switch private def escapedChar (ch : Char ): String = ch match {
776
+ case '\b ' => " \\ b"
777
+ case '\t ' => " \\ t"
778
+ case '\n ' => " \\ n"
779
+ case '\f ' => " \\ f"
780
+ case '\r ' => " \\ r"
781
+ case '"' => " \\\" "
782
+ case '\' ' => " \\\' "
783
+ case '\\ ' => " \\\\ "
784
+ case _ => if (ch.isControl) " \\ 0" + Integer .toOctalString(ch) else String .valueOf(ch)
785
+ }
786
+
787
+ private def escapedString (str : String ): String = str flatMap escapedChar
773
788
}
774
789
775
790
You can’t perform that action at this time.
0 commit comments