@@ -4,6 +4,7 @@ package coverage
4
4
import java .nio .file .{Path , Paths , Files }
5
5
import java .io .Writer
6
6
import scala .language .unsafeNulls
7
+ import scala .collection .mutable .StringBuilder
7
8
8
9
/**
9
10
* Serializes scoverage data.
@@ -62,25 +63,49 @@ object Serializer:
62
63
def writeStatement (stmt : Statement , writer : Writer ): Unit =
63
64
// Note: we write 0 for the count because we have not measured the actual coverage at this point
64
65
writer.write(s """ ${stmt.id}
65
- | ${getRelativePath(stmt.location.sourcePath)}
66
- | ${stmt.location.packageName}
67
- | ${stmt.location.className}
66
+ | ${getRelativePath(stmt.location.sourcePath).escaped }
67
+ | ${stmt.location.packageName.escaped }
68
+ | ${stmt.location.className.escaped }
68
69
| ${stmt.location.classType}
69
- | ${stmt.location.fullClassName}
70
- | ${stmt.location.methodName}
70
+ | ${stmt.location.fullClassName.escaped }
71
+ | ${stmt.location.methodName.escaped }
71
72
| ${stmt.start}
72
73
| ${stmt.end}
73
74
| ${stmt.line}
74
- | ${stmt.symbolName}
75
+ | ${stmt.symbolName.escaped }
75
76
| ${stmt.treeName}
76
77
| ${stmt.branch}
77
78
|0
78
79
| ${stmt.ignored}
79
- | ${stmt.desc}
80
+ | ${stmt.desc.escaped }
80
81
|\f
81
82
| """ .stripMargin)
82
83
83
84
writeHeader(writer)
84
85
coverage.statements.toSeq
85
86
.sortBy(_.id)
86
87
.foreach(stmt => writeStatement(stmt, writer))
88
+
89
+ /** Makes a String suitable for output in the coverage statement data as a single line.
90
+ * Escaped characters: '\\' (backslash), '\n', '\r', '\f'
91
+ */
92
+ extension (str : String ) def escaped : String =
93
+ val builder = StringBuilder (str.length)
94
+ var i = 0
95
+ while
96
+ i < str.length
97
+ do
98
+ str.charAt(i) match
99
+ case '\\ ' =>
100
+ builder ++= " \\\\ "
101
+ case '\n ' =>
102
+ builder ++= " \\ n"
103
+ case '\r ' =>
104
+ builder ++= " \\ r"
105
+ case '\f ' =>
106
+ builder ++= " \\ f"
107
+ case c =>
108
+ builder += c
109
+ i += 1
110
+ end while
111
+ builder.result()
0 commit comments