diff options
author | Som Snytt <som.snytt@gmail.com> | 2016-08-11 16:15:41 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2016-08-12 16:55:49 -0700 |
commit | d83a00aabaeabb1cd4c647a43cecd7bb4595dcf6 (patch) | |
tree | 3e7a1e813a569fc8f5525e22d0487de81485ea53 /src | |
parent | 2b172be8c83c3146d3fd5ab01546c171ab18fa46 (diff) | |
download | scala-d83a00aabaeabb1cd4c647a43cecd7bb4595dcf6.tar.gz scala-d83a00aabaeabb1cd4c647a43cecd7bb4595dcf6.tar.bz2 scala-d83a00aabaeabb1cd4c647a43cecd7bb4595dcf6.zip |
SI-9885 Don't return offset past EOF
On bad line number, `lineToOffset` should not return
an offset past EOF (which was sentinel, internally).
Diffstat (limited to 'src')
-rw-r--r-- | src/reflect/scala/reflect/internal/util/SourceFile.scala | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/reflect/scala/reflect/internal/util/SourceFile.scala b/src/reflect/scala/reflect/internal/util/SourceFile.scala index a2642628a4..64b6972298 100644 --- a/src/reflect/scala/reflect/internal/util/SourceFile.scala +++ b/src/reflect/scala/reflect/internal/util/SourceFile.scala @@ -154,18 +154,23 @@ class BatchSourceFile(val file : AbstractFile, content0: Array[Char]) extends So case _ => false } - def calculateLineIndices(cs: Array[Char]) = { - val buf = new ArrayBuffer[Int] - buf += 0 - for (i <- 0 until cs.length) if (isAtEndOfLine(i)) buf += i + 1 - buf += cs.length // sentinel, so that findLine below works smoother - buf.toArray + private lazy val lineIndices: Array[Int] = { + def calculateLineIndices(cs: Array[Char]) = { + val buf = new ArrayBuffer[Int] + buf += 0 + for (i <- 0 until cs.length) if (isAtEndOfLine(i)) buf += i + 1 + buf += cs.length // sentinel, so that findLine below works smoother + buf.toArray + } + calculateLineIndices(content) } - private lazy val lineIndices: Array[Int] = calculateLineIndices(content) - def lineToOffset(index : Int): Int = lineIndices(index) + def lineToOffset(index: Int): Int = { + val offset = lineIndices(index) + if (offset < length) offset else throw new IndexOutOfBoundsException(index.toString) + } - private var lastLine = 0 + private[this] var lastLine = 0 /** Convert offset to line in this source file. * Lines are numbered from 0. |