Skip to content

Commit 9458460

Browse files
authored
Merge pull request swiftlang#16 from rintaro/linetable-limitedby
[SKSupport.LineTable] More efficient LineTable
2 parents 202826a + 8f90fa4 commit 9458460

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

Sources/SKSupport/LineTable.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ extension LineTable {
139139
return nil
140140
}
141141
let lineSlice = self[line]
142-
guard utf16Column <= content.utf16.distance(from: lineSlice.startIndex, to: lineSlice.endIndex) else {
143-
// Column out of range.
144-
return nil
145-
}
146-
return content.utf16.index(lineSlice.startIndex, offsetBy: utf16Column)
142+
return content.utf16.index(lineSlice.startIndex, offsetBy: utf16Column, limitedBy: lineSlice.endIndex)
147143
}
148144

149145
/// Returns UTF8 buffer offset of given logical position.
@@ -181,11 +177,10 @@ extension LineTable {
181177
return nil
182178
}
183179
let lineSlice = self[line]
184-
guard utf8Column <= content.utf8.distance(from: lineSlice.startIndex, to: lineSlice.endIndex) else {
180+
guard let targetIndex = content.utf8.index(lineSlice.startIndex, offsetBy: utf8Column, limitedBy: lineSlice.endIndex) else {
185181
// Column out of range
186182
return nil
187183
}
188-
let targetIndex = content.utf8.index(lineSlice.startIndex, offsetBy: utf8Column)
189184
return content.utf16.distance(from: lineSlice.startIndex, to: targetIndex)
190185
}
191186
}

0 commit comments

Comments
 (0)