sealedcaseclass Position(line: Int = 0, column: Int = 0) { def line1: Int = line + 1 def column1: Int = column + 1 def print: String = if (column == 0) line1.toString else line1.toString + ":" + column1.toString
def compare(that: Position): Int =
line compare that.line match { case 0 => column compare that.column case i => i
}
def advance(text: String): Position = if (text.isEmpty) this else { val lines = logical_lines(text) val l = line + lines.length - 1 val c = (if (l == line) column else 0) + Library.trim_line(lines.last).length
Position(line = l, column = c)
}
}
/* range (right-open interval) */
object Range { def apply(start: Position): Range = Range(start, start) val zero: Range = Range(Position.zero)
}
overridedef equals(that: Any): Boolean =
that match { case other: Document => lines == other.lines case _ => false
} overridedef hashCode(): Int = lines.hashCode
def position(text_offset: Text.Offset): Position = {
@tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position = {
lines_rest match { case Nil =>
require(i == 0, "bad Line.position.move")
Position(line = lines_count) case line :: ls => val n = line.text.length if (ls.isEmpty || i <= n) {
Position(line = lines_count).advance(line.text.substring(n - i))
} else move(i - (n + 1), lines_count + 1, ls)
}
}
move(text_offset, 0, lines)
}
def range(text_range: Text.Range): Range =
Range(position(text_range.start), position(text_range.stop))
def offset(pos: Position): Option[Text.Offset] = { val l = pos.line val c = pos.column val n = lines.length if (0 <= l && l < n) { if (0 <= c && c <= lines(l).text.length) { val line_offset =
lines.iterator.take(l).foldLeft(0) { case (n, line) => n + line.text.length + 1 }
Some(line_offset + c)
} else None
} elseif (l == n && c == 0) Some(text_length) else None
}
val empty: Line = new Line("") def apply(text: String): Line = if (text == "") empty elsenew Line(text)
}
finalclass Line private(val text: String) {
require(text.forall(c => c != '\r' && c != '\n'), "bad line text")
overridedef equals(that: Any): Boolean =
that match { case other: Line => text == other.text case _ => false
} overridedef hashCode(): Int = text.hashCode overridedef toString: String = text
}
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.