Skip to content

Commit 66967f9

Browse files
committed
Optimize calculateLineIndices
1 parent 26e49de commit 66967f9

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,18 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
103103
def positionInUltimateSource(position: SourcePosition): SourcePosition =
104104
SourcePosition(underlying, position.span shift start)
105105

106-
private def isLineBreak(idx: Int) =
107-
if (idx >= length) false else {
108-
val ch = content()(idx)
109-
// don't identify the CR in CR LF as a line break, since LF will do.
110-
if (ch == CR) (idx + 1 == length) || (content()(idx + 1) != LF)
111-
else isLineBreakChar(ch)
112-
}
113-
114106
private def calculateLineIndices(cs: Array[Char]) = {
115107
val buf = new ArrayBuffer[Int]
116108
buf += 0
117-
for (i <- 0 until cs.length) if (isLineBreak(i)) buf += i + 1
109+
var i = 0
110+
while i < cs.length do
111+
val isLineBreak =
112+
val ch = cs(i)
113+
// don't identify the CR in CR LF as a line break, since LF will do.
114+
if ch == CR then i + 1 == cs.length || cs(i + 1) != LF
115+
else isLineBreakChar(ch)
116+
if isLineBreak then buf += i + 1
117+
i += 1
118118
buf += cs.length // sentinel, so that findLine below works smoother
119119
buf.toArray
120120
}

0 commit comments

Comments
 (0)