diff options
author | Martin Odersky <odersky@gmail.com> | 2009-07-14 19:25:02 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2009-07-14 19:25:02 +0000 |
commit | 4ec0c0ee2cb9615a84b9a584ebfd131f11a9c83d (patch) | |
tree | 2124ed56731b48efec779e631cc23ecd5686f01f /src | |
parent | d9418567e6a467f3ac47b70552476e2647be8784 (diff) | |
download | scala-4ec0c0ee2cb9615a84b9a584ebfd131f11a9c83d.tar.gz scala-4ec0c0ee2cb9615a84b9a584ebfd131f11a9c83d.tar.bz2 scala-4ec0c0ee2cb9615a84b9a584ebfd131f11a9c83d.zip |
Faster offsetToLine
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/util/SourceFile.scala | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/util/SourceFile.scala b/src/compiler/scala/tools/nsc/util/SourceFile.scala index 14405c15e6..483f533d32 100644 --- a/src/compiler/scala/tools/nsc/util/SourceFile.scala +++ b/src/compiler/scala/tools/nsc/util/SourceFile.scala @@ -7,6 +7,7 @@ package scala.tools.nsc.util import scala.tools.nsc.io.{AbstractFile, VirtualFile} +import scala.collection.mutable.ArrayBuffer import annotation.tailrec object SourceFile { @@ -98,8 +99,33 @@ class BatchSourceFile(val file : AbstractFile, val content: Array[Char]) extends case x => isLineBreakChar(x) } + private lazy val lineIndices: Array[Int] = { + val buf = new ArrayBuffer[Int] + buf += 0 + for (i <- 0 until content.length) if (isLineBreak(i)) buf += i + 1 + buf += content.length // sentinel, so that findLine below works smoother + buf.toArray + } + + def lineToOffset(index : Int): Int = lineIndices(index) + + private var lastLine = 0 + + def offsetToLine(offset: Int): Int = { + val lines = lineIndices + def findLine(lo: Int, hi: Int, mid: Int): Int = + if (offset < lines(mid)) findLine(lo, mid - 1, (lo + mid - 1) / 2) + else if (offset >= lines(mid + 1)) findLine(mid + 1, hi, (mid + 1 + hi) / 2) + else mid + lastLine = findLine(0, lines.length, lastLine) + lastLine + } + +/** + // An array which maps line numbers (counting from 0) to char offset into content private lazy val lineIndices: Array[Int] = { + val xs = content.indices filter isLineBreak map (_ + 1) toArray val arr = new Array[Int](xs.length + 1) arr(0) = 0 @@ -125,6 +151,8 @@ class BatchSourceFile(val file : AbstractFile, val content: Array[Char]) extends def lineToOffset(index : Int): Int = lineIndices(index) def offsetToLine(offset: Int): Int = lineIndicesRev(offset) + + */ } /** A source file composed of multiple other source files. |