diff options
| author | Guanqun Lu <guanqun.lu@gmail.com> | 2019-10-12 12:45:28 +0800 |
|---|---|---|
| committer | Guanqun Lu <guanqun.lu@gmail.com> | 2019-10-12 15:50:26 +0800 |
| commit | e0395341f7e38e804646bce23809b407564e84db (patch) | |
| tree | be63fb2c68b37c2e17f0b5d3a082e7cb63adb39a /src/libsyntax | |
| parent | 4b42e919d640f7c714a7e87c91b4f3a9089552d0 (diff) | |
| download | rust-e0395341f7e38e804646bce23809b407564e84db.tar.gz rust-e0395341f7e38e804646bce23809b407564e84db.zip | |
replace the hand-written binary search with the library one
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/source_map.rs | 18 |
1 files changed, 5 insertions, 13 deletions
diff --git a/src/libsyntax/source_map.rs b/src/libsyntax/source_map.rs index 7d0d2392945..07c301f524e 100644 --- a/src/libsyntax/source_map.rs +++ b/src/libsyntax/source_map.rs @@ -882,21 +882,13 @@ impl SourceMap { 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; - } - } + // (p - 1) below will not underflow, this follows previous implementation's assumption. + assert!(count >= 1); + let ret = files.binary_search_by_key(&pos, |key| key.start_pos).unwrap_or_else(|p| p - 1); - assert!(a < count, "position {} does not resolve to a source location", pos.to_usize()); + assert!(ret < count, "position {} does not resolve to a source location", pos.to_usize()); - return a; + return ret; } pub fn count_lines(&self) -> usize { |
