Skip to content

Commit af8a6e5

Browse files
authored
Rollup merge of rust-lang#65327 - guanqun:remove-hand-binary-search, r=petrochenkov
replace the hand-written binary search with the library one
2 parents 643261a + 63cb2fa commit af8a6e5

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

src/libsyntax/source_map.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -878,25 +878,8 @@ impl SourceMap {
878878

879879
// Returns the index of the `SourceFile` (in `self.files`) that contains `pos`.
880880
pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
881-
let files = self.files.borrow();
882-
let files = &files.source_files;
883-
let count = files.len();
884-
885-
// Binary search for the `SourceFile`.
886-
let mut a = 0;
887-
let mut b = count;
888-
while b - a > 1 {
889-
let m = (a + b) / 2;
890-
if files[m].start_pos > pos {
891-
b = m;
892-
} else {
893-
a = m;
894-
}
895-
}
896-
897-
assert!(a < count, "position {} does not resolve to a source location", pos.to_usize());
898-
899-
return a;
881+
self.files.borrow().source_files.binary_search_by_key(&pos, |key| key.start_pos)
882+
.unwrap_or_else(|p| p - 1)
900883
}
901884

902885
pub fn count_lines(&self) -> usize {

0 commit comments

Comments
 (0)