@@ -312,13 +312,14 @@ public class WhitespaceLinter {
312
312
formattedRun: ArraySlice < UTF8 . CodeUnit >
313
313
) {
314
314
guard userRun != formattedRun else { return }
315
-
315
+ let userString = String ( decoding: userRun, as: UTF8 . self)
316
+ let formattedString = String ( decoding: formattedRun, as: UTF8 . self)
316
317
// This assumes tabs will always be forbidden for inter-token spacing (but not for leading
317
318
// indentation).
318
- if userRun . contains ( utf8Tab ) {
319
+ if userString . contains ( " \t " ) {
319
320
diagnose ( . spacingCharError, category: . spacingCharacter, utf8Offset: userIndex)
320
- } else if formattedRun . count != userRun . count {
321
- let delta = formattedRun . count - userRun . count
321
+ } else if formattedString . count != userString . count {
322
+ let delta = formattedString . count - userString . count
322
323
diagnose ( . spacingError( delta) , category: . spacing, utf8Offset: userIndex)
323
324
}
324
325
}
@@ -339,20 +340,15 @@ public class WhitespaceLinter {
339
340
startingAt offset: Int ,
340
341
in data: [ UTF8 . CodeUnit ]
341
342
) -> 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
- }
350
- guard
351
- let whitespaceEnd = data [ offset... ] . firstIndex ( where: { !isWhitespace( $0) } )
352
- else {
353
- return data [ offset..< data. endIndex]
343
+ // To ensure consistency with PrettyPrinter, the data is converted to a String
344
+ // before processing whitespace.
345
+ let substring = String ( decoding: data [ offset... ] , as: UTF8 . self)
346
+ var stringIndex = substring. startIndex
347
+ while stringIndex < substring. endIndex, substring [ stringIndex] . isWhitespace {
348
+ substring. formIndex ( after: & stringIndex)
354
349
}
355
- return data [ offset..< whitespaceEnd]
350
+ let utf8Count = substring. utf8. distance ( from: substring. startIndex, to: stringIndex)
351
+ return data [ offset..< offset + utf8Count]
356
352
}
357
353
358
354
/// Returns the code unit at the given index, or nil if the index is the end of the data.
0 commit comments