@@ -12,11 +12,42 @@ import config.Config
12
12
import config .Printers .lexical
13
13
import config .Settings .Setting
14
14
import Tokens ._
15
- import scala .annotation .{ switch , tailrec }
15
+ import scala .annotation .{switch , tailrec }
16
16
import scala .collection .mutable
17
17
import scala .collection .immutable .{SortedMap , BitSet }
18
18
import rewrites .Rewrites .patch
19
19
20
+ object Cbufs {
21
+ import java .lang .StringBuilder
22
+
23
+ private final val TargetCapacity = 256
24
+
25
+ opaque type Cbuf = StringBuilder
26
+ object Cbuf :
27
+ def apply (): Cbuf = new StringBuilder (TargetCapacity )
28
+
29
+ extension StringBuilderOps on (buf : Cbuf ):
30
+ def clear (): Unit = {
31
+ if buf.capacity() > TargetCapacity then
32
+ buf.setLength(TargetCapacity )
33
+ buf.trimToSize()
34
+ end if
35
+ buf.setLength(0 )
36
+ }
37
+ def toCharArray : Array [Char ] = {
38
+ val n = buf.length()
39
+ val res = new Array [Char ](n)
40
+ buf.getChars(0 , n, res, 0 )
41
+ res
42
+ }
43
+ def append (c : Char ): buf.type = { buf.append(c) ; buf }
44
+ def isEmpty : Boolean = buf.length() == 0
45
+ def length : Int = buf.length()
46
+ def last : Char = buf.charAt(buf.length() - 1 )
47
+ }
48
+
49
+ import Cbufs ._
50
+
20
51
object Scanners {
21
52
22
53
/** Offset into source character array */
@@ -99,14 +130,14 @@ object Scanners {
99
130
100
131
/** A character buffer for literals
101
132
*/
102
- protected val litBuf = new mutable. StringBuilder
133
+ protected val litBuf = Cbuf ()
103
134
104
135
/** append Unicode character to "litBuf" buffer
105
136
*/
106
137
protected def putChar (c : Char ): Unit = litBuf.append(c)
107
138
108
139
/** Return buffer contents and clear */
109
- def flushBuf (buf : StringBuilder ): String = {
140
+ def flushBuf (buf : Cbuf ): String = {
110
141
val str = buf.toString
111
142
buf.clear()
112
143
str
@@ -198,7 +229,7 @@ object Scanners {
198
229
def getDocComment (pos : Int ): Option [Comment ] = docstringMap.get(pos)
199
230
200
231
/** A buffer for comments */
201
- private val commentBuf = new mutable. StringBuilder
232
+ private val commentBuf = Cbuf ()
202
233
203
234
private def handleMigration (keyword : Token ): Token =
204
235
if (keyword == ERASED && ! ctx.settings.YerasedTerms .value) IDENTIFIER
0 commit comments