Skip to content

Commit 557d19f

Browse files
committed
PrettyPrinterPerformance Optimized the PrettyPrinter for #894
Worked to get the perfomance to be closer to where we were before the changes in #883. This code should be only about 1.5% slower rather than 7% slower.
1 parent a871b81 commit 557d19f

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Sources/SwiftFormat/PrettyPrint/PrettyPrintBuffer.swift

+8-9
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,15 @@ struct PrettyPrintBuffer {
122122
// In case of comments, we may get a multi-line string.
123123
// To account for that case, we need to correct the lineNumber count.
124124
// The new column is only the position within the last line.
125-
let lines = text.split(separator: "\n")
126-
lineNumber += lines.count - 1
127-
if lines.count > 1 {
128-
// in case we have inserted new lines, we need to reset the column
129-
column = lines.last?.count ?? 0
130-
} else {
131-
// in case it is an end of line comment or a single line comment,
132-
// we just add to the current column
133-
column += lines.last?.count ?? 0
125+
let lines = text.count { $0 == "\n" }
126+
lineNumber += lines
127+
guard lines > 1, let lastNewlineIndex = text.lastIndex(of: "\n") else {
128+
// Handle case where only one line exists.
129+
column += text.count
130+
return
134131
}
132+
let lastLine = text[text.index(after: lastNewlineIndex)...]
133+
column = lastLine.count
135134
}
136135

137136
/// Request that the given number of spaces be printed out before the next text token.

0 commit comments

Comments
 (0)