Skip to content

replace the hand-written binary search with the library one #65327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions src/libsyntax/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,25 +878,8 @@ impl SourceMap {

// Returns the index of the `SourceFile` (in `self.files`) that contains `pos`.
pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
let files = self.files.borrow();
let files = &files.source_files;
let count = files.len();

// Binary search for the `SourceFile`.
let mut a = 0;
let mut b = count;
while b - a > 1 {
let m = (a + b) / 2;
if files[m].start_pos > pos {
b = m;
} else {
a = m;
}
}

assert!(a < count, "position {} does not resolve to a source location", pos.to_usize());

return a;
self.files.borrow().source_files.binary_search_by_key(&pos, |key| key.start_pos)
.unwrap_or_else(|p| p - 1)
}

pub fn count_lines(&self) -> usize {
Expand Down