From: Jean Privat Date: Thu, 9 Feb 2017 17:04:28 +0000 (-0500) Subject: nitc: add Location::get_line X-Git-Url: http://nitlanguage.org nitc: add Location::get_line Signed-off-by: Jean Privat --- diff --git a/src/location.nit b/src/location.nit index 99083ad..36157d3 100644 --- a/src/location.nit +++ b/src/location.nit @@ -47,6 +47,20 @@ class SourceFile # # 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 # A location inside a source file