forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString+LineEnding.swift
More file actions
27 lines (24 loc) · 815 Bytes
/
String+LineEnding.swift
File metadata and controls
27 lines (24 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Foundation
public extension String {
/// The line ending of the string.
var lineEnding: Character? {
last(where: \.isNewline)
}
/// Break a string into lines.
func breakLines(
proposedLineEnding: String? = nil,
appendLineBreakToLastLine: Bool = false
) -> [String] {
let lineEnding = proposedLineEnding ?? String(lineEnding ?? "\n")
let lines = split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
var all = [String]()
for (index, line) in lines.enumerated() {
if !appendLineBreakToLastLine, index == lines.endIndex - 1 {
all.append(String(line))
} else {
all.append(String(line) + String(lineEnding))
}
}
return all
}
}