import Foundation /// Tools to find the start and end of a line. enum LineScanner { /// Given a current position in a character array, find the index of the /// start of the line. static func lineStart(in characters: [Character], at position: Int) -> Int { var start = position while start > 0, characters[start - 1] != "\n" { start -= 1 } return start } /// Given a current position in a character array, find the index right /// after the next newline or end of buffer. static func lineEnd(in characters: [Character], at position: Int) -> Int { var end = position while end < characters.count, characters[end] != "\n" { end += 1 } return end } }