Skip to content

Commit 0316649

Browse files
authored
Base corrections on bytes to avoid range conversions (#6570)
1 parent eac464e commit 0316649

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

Source/SwiftLintCore/Protocols/SwiftSyntaxCorrectableRule.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ public extension SwiftSyntaxCorrectableRule {
4040
.filter { $0.areRulesDisabled(ruleIDs: Self.description.allIdentifiers) }
4141
.compactMap { $0.toSourceRange(locationConverter: locationConverter) }
4242

43-
typealias CorrectionRange = (range: NSRange, correction: String)
4443
let correctionRanges = violations
4544
.filter { !$0.position.isContainedIn(regions: disabledRegions, locationConverter: locationConverter) }
4645
.compactMap(\.correction)
4746
.compactMap { correction in
48-
file.stringView.NSRange(start: correction.start, end: correction.end).map { range in
49-
CorrectionRange(range: range, correction: correction.replacement)
50-
}
51-
}
52-
.sorted { (lhs: CorrectionRange, rhs: CorrectionRange) -> Bool in
53-
lhs.range.location > rhs.range.location
47+
(
48+
startByte: correction.start.utf8Offset,
49+
endByte: correction.end.utf8Offset,
50+
replacementBytes: correction.replacement.utf8)
5451
}
52+
.sorted { $0.startByte > $1.startByte }
5553
guard correctionRanges.isNotEmpty else {
5654
return 0
5755
}
5856

59-
var contents = file.contents
57+
var bytes = Array(file.contents.utf8)
6058
for range in correctionRanges {
61-
let contentsNSString = contents.bridge()
62-
contents = contentsNSString.replacingCharacters(in: range.range, with: range.correction)
59+
bytes.replaceSubrange(range.startByte..<range.endByte, with: range.replacementBytes)
60+
}
61+
guard let contents = String(bytes: bytes, encoding: .utf8) else {
62+
return 0
6363
}
6464
file.write(contents)
6565
return correctionRanges.count

0 commit comments

Comments
 (0)