nitc :: SourceFile :: defaultinit
# A raw text Nit source file
class SourceFile
# The path of the source
var filename: String
# The content of the source
var string: String is noinit
# The original stream used to initialize `string`
var stream: Reader
init
do
string = stream.read_all
line_starts[0] = 0
end
# Create a new sourcefile using a dummy filename and a given content
init from_string(filename: String, string: String) is
nosuper
do
self.filename = filename
self.string = string
line_starts[0] = 0
end
# Offset of each line start in the content `string`.
#
# Used for fast access to each line when rendering parts of the `string`.
var line_starts = new Array[Int]
# Extract a given line excluding the line-terminators characters.
#
# `line_number` starts at 1 for the first line.
fun get_line(line_number: Int): String do
if line_number > line_starts.length then return ""
var line_start = line_starts[line_number-1]
var line_end = line_start
var string = self.string
while line_end+1 < string.length and string.chars[line_end+1] != '\n' and string.chars[line_end+1] != '\r' do
line_end += 1
end
return string.substring(line_start, line_end-line_start+1)
end
end
src/location.nit:20,1--64,3