diff options
| author | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-06-14 17:07:38 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-06-15 13:38:02 +0200 |
| commit | 3402f5764f794a60f85a152a13c4eacaa2a719f6 (patch) | |
| tree | 9fafe13da4caee303313033e313461a556f9f495 /Sources/NorgKeyboardToolbar/LineScanner.swift | |
Extract from Norganizer
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 + } +} |