Skip to content

Commit 83b5f01

Browse files
committed
[WhitespaceLinter] Use hand crafted "is whitespace" function
* `UnicodeScalar(_:)` on arbitrary UTF8 code point was wrong. It only works correctly if the code point is < 0x80 * `UnicodeScalar.Properties.isWhitespace` is slow. Profiling 'lint' shows it's taking 13.6 of the entire time * Whitespaces in Unicode "Basic Latin" block are well defined, there's no need to consult `UnicodeScalar.Properties`
1 parent 6e64b26 commit 83b5f01

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,16 @@ public class WhitespaceLinter {
339339
startingAt offset: Int,
340340
in data: [UTF8.CodeUnit]
341341
) -> ArraySlice<UTF8.CodeUnit> {
342+
func isWhitespace(_ char: UTF8.CodeUnit) -> Bool {
343+
switch char {
344+
case UInt8(ascii: " "), UInt8(ascii: "\n"), UInt8(ascii: "\t"), UInt8(ascii: "\r"), /*VT*/ 0x0B, /*FF*/ 0x0C:
345+
return true
346+
default:
347+
return false
348+
}
349+
}
342350
guard
343-
let whitespaceEnd =
344-
data[offset...].firstIndex(where: { !UnicodeScalar($0).properties.isWhitespace })
351+
let whitespaceEnd = data[offset...].firstIndex(where: { !isWhitespace($0) })
345352
else {
346353
return data[offset..<data.endIndex]
347354
}

0 commit comments

Comments
 (0)