From f584f5b4b86be552bcba47309b8e63363a9904d9 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 29 Jan 2014 11:16:39 -0800 Subject: SI-8205 Don't include CR in line More penance. Extend the unit test and don't include CR in the line text. This is obvious, which shows how dangerous it is to refactor without unit tests. My very favorite bugs are off-by-one and EOL handling, followed closely by off-by-Int.MaxValue. --- src/reflect/scala/reflect/internal/util/SourceFile.scala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/reflect/scala/reflect/internal/util/SourceFile.scala b/src/reflect/scala/reflect/internal/util/SourceFile.scala index add072aa71..8791d8eb23 100644 --- a/src/reflect/scala/reflect/internal/util/SourceFile.scala +++ b/src/reflect/scala/reflect/internal/util/SourceFile.scala @@ -143,7 +143,13 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend def isLineBreak(idx: Int) = charAtIsEOL(idx)(isLineBreakChar) - def isEndOfLine(idx: Int) = charAtIsEOL(idx) { + /** True if the index is included by an EOL sequence. */ + def isEndOfLine(idx: Int) = (content isDefinedAt idx) && PartialFunction.cond(content(idx)) { + case CR | LF => true + } + + /** True if the index is end of an EOL sequence. */ + def isAtEndOfLine(idx: Int) = charAtIsEOL(idx) { case CR | LF => true case _ => false } @@ -151,7 +157,7 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend def calculateLineIndices(cs: Array[Char]) = { val buf = new ArrayBuffer[Int] buf += 0 - for (i <- 0 until cs.length) if (isEndOfLine(i)) buf += i + 1 + for (i <- 0 until cs.length) if (isAtEndOfLine(i)) buf += i + 1 buf += cs.length // sentinel, so that findLine below works smoother buf.toArray } -- cgit v1.2.3