diff options
author | Som Snytt <som.snytt@gmail.com> | 2013-12-17 20:26:50 -0800 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2014-01-08 07:43:42 -0800 |
commit | bb2e99a69201060b88dd25602def42f31ba9ab65 (patch) | |
tree | 7a3e3a16a0a5e9ee7221f26a6ec50464856989a3 /src/reflect/scala/reflect/internal/util | |
parent | ada8d9156baad2d8a24c1a40e032eb4bc7154bac (diff) | |
download | scala-bb2e99a69201060b88dd25602def42f31ba9ab65.tar.gz scala-bb2e99a69201060b88dd25602def42f31ba9ab65.tar.bz2 scala-bb2e99a69201060b88dd25602def42f31ba9ab65.zip |
SI-8015 Count lines by EOLs
Source lines were counted by "line break chars", including FF.
Clients of `pos.line` seem to all expect the ordinary line num,
so that is what they get.
Unicode processing now precedes line ending processing.
Diffstat (limited to 'src/reflect/scala/reflect/internal/util')
-rw-r--r-- | src/reflect/scala/reflect/internal/util/SourceFile.scala | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/src/reflect/scala/reflect/internal/util/SourceFile.scala b/src/reflect/scala/reflect/internal/util/SourceFile.scala index 3b6c57e955..1da334d41f 100644 --- a/src/reflect/scala/reflect/internal/util/SourceFile.scala +++ b/src/reflect/scala/reflect/internal/util/SourceFile.scala @@ -16,12 +16,13 @@ import scala.reflect.internal.Chars._ /** abstract base class of a source file used in the compiler */ abstract class SourceFile { - def content : Array[Char] // normalized, must end in SU - def file : AbstractFile - def isLineBreak(idx : Int) : Boolean + def content: Array[Char] // normalized, must end in SU + def file : AbstractFile + def isLineBreak(idx: Int): Boolean + def isEndOfLine(idx: Int): Boolean def isSelfContained: Boolean def length : Int - def position(offset: Int) : Position = { + def position(offset: Int): Position = { assert(offset < length, file + ": " + offset + " >= " + length) Position.offset(this, offset) } @@ -52,6 +53,7 @@ object NoSourceFile extends SourceFile { def content = Array() def file = NoFile def isLineBreak(idx: Int) = false + def isEndOfLine(idx: Int) = false def isSelfContained = true def length = -1 def offsetToLine(offset: Int) = -1 @@ -129,17 +131,24 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend } def isLineBreak(idx: Int) = - if (idx >= length) false else { - val ch = content(idx) + (idx < length) && (content(idx) match { // don't identify the CR in CR LF as a line break, since LF will do. - if (ch == CR) (idx + 1 == length) || (content(idx + 1) != LF) - else isLineBreakChar(ch) - } + case CR => (idx + 1 == length) || (content(idx + 1) != LF) + case ch => isLineBreakChar(ch) + }) + + def isEndOfLine(idx: Int) = + (idx < length) && (content(idx) match { + // don't identify the CR in CR LF as a line break, since LF will do. + case CR => (idx + 1 == length) || (content(idx + 1) != LF) + case LF => true + case _ => false + }) def calculateLineIndices(cs: Array[Char]) = { val buf = new ArrayBuffer[Int] buf += 0 - for (i <- 0 until cs.length) if (isLineBreak(i)) buf += i + 1 + for (i <- 0 until cs.length) if (isEndOfLine(i)) buf += i + 1 buf += cs.length // sentinel, so that findLine below works smoother buf.toArray } @@ -149,8 +158,8 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend private var lastLine = 0 - /** Convert offset to line in this source file - * Lines are numbered from 0 + /** Convert offset to line in this source file. + * Lines are numbered from 0. */ def offsetToLine(offset: Int): Int = { val lines = lineIndices |