From 3402f5764f794a60f85a152a13c4eacaa2a719f6 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sun, 14 Jun 2026 17:07:38 +0200 Subject: Extract from Norganizer --- Sources/NorgKeyboardToolbar/LineScanner.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Sources/NorgKeyboardToolbar/LineScanner.swift (limited to 'Sources/NorgKeyboardToolbar/LineScanner.swift') 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 + } +} -- cgit