diff options
Diffstat (limited to 'Sources/NorgKeyboardToolbar/LineScanner.swift')
| -rw-r--r-- | Sources/NorgKeyboardToolbar/LineScanner.swift | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Sources/NorgKeyboardToolbar/LineScanner.swift b/Sources/NorgKeyboardToolbar/LineScanner.swift new file mode 100644 index 0000000..53d443d --- /dev/null +++ b/Sources/NorgKeyboardToolbar/LineScanner.swift @@ -0,0 +1,21 @@ +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 + } +} |