@@ -4,6 +4,7 @@ package coverage
44import java .nio .file .{Path , Paths , Files }
55import java .io .Writer
66import scala .language .unsafeNulls
7+ import scala .collection .mutable .StringBuilder
78
89/**
910 * Serializes scoverage data.
@@ -62,25 +63,49 @@ object Serializer:
6263 def writeStatement (stmt : Statement , writer : Writer ): Unit =
6364 // Note: we write 0 for the count because we have not measured the actual coverage at this point
6465 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 }
6869 | ${stmt.location.classType}
69- | ${stmt.location.fullClassName}
70- | ${stmt.location.methodName}
70+ | ${stmt.location.fullClassName.escaped }
71+ | ${stmt.location.methodName.escaped }
7172 | ${stmt.start}
7273 | ${stmt.end}
7374 | ${stmt.line}
74- | ${stmt.symbolName}
75+ | ${stmt.symbolName.escaped }
7576 | ${stmt.treeName}
7677 | ${stmt.branch}
7778 |0
7879 | ${stmt.ignored}
79- | ${stmt.desc}
80+ | ${stmt.desc.escaped }
8081 |\f
8182 | """ .stripMargin)
8283
8384 writeHeader(writer)
8485 coverage.statements.toSeq
8586 .sortBy(_.id)
8687 .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