diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2016-11-18 15:08:14 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-18 15:08:14 +1000 |
commit | 4fe94a2ded7bf64b3d665e32f95978f5c0927805 (patch) | |
tree | 518a3e85b8cc95f8ea7262c0047a2123d6e874ac /src/reflect/scala/reflect | |
parent | 73678d4dafe250f0b38df2e953787af26b1a4ee3 (diff) | |
parent | d83a00aabaeabb1cd4c647a43cecd7bb4595dcf6 (diff) | |
download | scala-4fe94a2ded7bf64b3d665e32f95978f5c0927805.tar.gz scala-4fe94a2ded7bf64b3d665e32f95978f5c0927805.tar.bz2 scala-4fe94a2ded7bf64b3d665e32f95978f5c0927805.zip |
Merge pull request #5330 from som-snytt/issue/9885
SI-9885 Don't return offset past EOF
Diffstat (limited to 'src/reflect/scala/reflect')
-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. |