aboutsummaryrefslogtreecommitdiff
path: root/Sources/NorgKeyboardToolbar/LineScanner.swift
blob: 53d443d94be67217d572dcf7d9b8a80457eebf1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
  }
}