Skip to content

Commit e039534

Browse files
author
Guanqun Lu
committed
replace the hand-written binary search with the library one
1 parent 4b42e91 commit e039534

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/libsyntax/source_map.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -882,21 +882,13 @@ impl SourceMap {
882882
let files = &files.source_files;
883883
let count = files.len();
884884

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-
}
885+
// (p - 1) below will not underflow, this follows previous implementation's assumption.
886+
assert!(count >= 1);
887+
let ret = files.binary_search_by_key(&pos, |key| key.start_pos).unwrap_or_else(|p| p - 1);
896888

897-
assert!(a < count, "position {} does not resolve to a source location", pos.to_usize());
889+
assert!(ret < count, "position {} does not resolve to a source location", pos.to_usize());
898890

899-
return a;
891+
return ret;
900892
}
901893

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

0 commit comments

Comments
 (0)