diff --git a/Sources/SwiftFormat/PrettyPrint/Comment.swift b/Sources/SwiftFormat/PrettyPrint/Comment.swift index 43616a5b..76279694 100644 --- a/Sources/SwiftFormat/PrettyPrint/Comment.swift +++ b/Sources/SwiftFormat/PrettyPrint/Comment.swift @@ -21,13 +21,27 @@ extension StringProtocol { /// - Returns: The string with trailing whitespace removed. func trimmingTrailingWhitespace() -> String { if isEmpty { return String() } - let scalars = unicodeScalars - var idx = scalars.index(before: scalars.endIndex) - while scalars[idx].properties.isWhitespace { - if idx == scalars.startIndex { return String() } - idx = scalars.index(before: idx) + let utf8Array = Array(utf8) + var idx = utf8Array.endIndex - 1 + while utf8Array[idx].isWhitespace { + if idx == utf8Array.startIndex { return String() } + idx -= 1 + } + return String(decoding: utf8Array[...idx], as: UTF8.self) + } +} + +extension UTF8.CodeUnit { + /// Checks if the UTF-8 code unit represents a whitespace character. + /// + /// - Returns: `true` if the code unit represents a whitespace character, otherwise `false`. + var isWhitespace: Bool { + switch self { + case UInt8(ascii: " "), UInt8(ascii: "\n"), UInt8(ascii: "\t"), UInt8(ascii: "\r"), /*VT*/ 0x0B, /*FF*/ 0x0C: + return true + default: + return false } - return String(String.UnicodeScalarView(scalars[...idx])) } } diff --git a/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift b/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift index 30f73395..d9d5178a 100644 --- a/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift +++ b/Sources/SwiftFormat/PrettyPrint/WhitespaceLinter.swift @@ -339,16 +339,8 @@ public class WhitespaceLinter { startingAt offset: Int, in data: [UTF8.CodeUnit] ) -> ArraySlice { - func isWhitespace(_ char: UTF8.CodeUnit) -> Bool { - switch char { - case UInt8(ascii: " "), UInt8(ascii: "\n"), UInt8(ascii: "\t"), UInt8(ascii: "\r"), /*VT*/ 0x0B, /*FF*/ 0x0C: - return true - default: - return false - } - } guard - let whitespaceEnd = data[offset...].firstIndex(where: { !isWhitespace($0) }) + let whitespaceEnd = data[offset...].firstIndex(where: { !$0.isWhitespace }) else { return data[offset..