about summary refs log tree commit diff
path: root/compiler/rustc_span
diff options
context:
space:
mode:
authorTyson Nottingham <tgnottingham@gmail.com>2020-09-20 17:40:48 -0700
committerTyson Nottingham <tgnottingham@gmail.com>2020-12-03 18:36:34 -0800
commit0987b841987fb7ffc09848c185d671ff3bea7d35 (patch)
tree33f6ee3829ea66d3997062af13ae57afb980e831 /compiler/rustc_span
parent8da2a5a27c9890100e3b56ec85879e312312b111 (diff)
downloadrust-0987b841987fb7ffc09848c185d671ff3bea7d35.tar.gz
rust-0987b841987fb7ffc09848c185d671ff3bea7d35.zip
rustc_span: refactor byte_pos_to_line_and_col
Diffstat (limited to 'compiler/rustc_span')
-rw-r--r--compiler/rustc_span/src/caching_source_map_view.rs59
1 files changed, 31 insertions, 28 deletions
diff --git a/compiler/rustc_span/src/caching_source_map_view.rs b/compiler/rustc_span/src/caching_source_map_view.rs
index 9360d5b5abf..b57da5448ab 100644
--- a/compiler/rustc_span/src/caching_source_map_view.rs
+++ b/compiler/rustc_span/src/caching_source_map_view.rs
@@ -70,38 +70,16 @@ impl<'sm> CachingSourceMapView<'sm> {
         }
 
         // No cache hit ...
-        let mut oldest = 0;
-        for index in 1..self.line_cache.len() {
-            if self.line_cache[index].time_stamp < self.line_cache[oldest].time_stamp {
-                oldest = index;
-            }
-        }
-
-        let cache_entry = &mut self.line_cache[oldest];
+        let oldest = self.oldest_cache_entry_index();
 
         // If the entry doesn't point to the correct file, fix it up
-        if !file_contains(&cache_entry.file, pos) {
-            let file_valid;
-            if self.source_map.files().len() > 0 {
-                let file_index = self.source_map.lookup_source_file_idx(pos);
-                let file = &self.source_map.files()[file_index];
-
-                if file_contains(&file, pos) {
-                    cache_entry.file = file.clone();
-                    cache_entry.file_index = file_index;
-                    file_valid = true;
-                } else {
-                    file_valid = false;
-                }
-            } else {
-                file_valid = false;
-            }
-
-            if !file_valid {
-                return None;
-            }
+        if !file_contains(&self.line_cache[oldest].file, pos) {
+            let (file, file_index) = self.file_for_position(pos)?;
+            self.line_cache[oldest].file = file;
+            self.line_cache[oldest].file_index = file_index;
         }
 
+        let cache_entry = &mut self.line_cache[oldest];
         let line_index = cache_entry.file.lookup_line(pos).unwrap();
         let line_bounds = cache_entry.file.line_bounds(line_index);
 
@@ -111,6 +89,31 @@ impl<'sm> CachingSourceMapView<'sm> {
 
         Some((cache_entry.file.clone(), cache_entry.line_number, pos - cache_entry.line.start))
     }
+
+    fn oldest_cache_entry_index(&self) -> usize {
+        let mut oldest = 0;
+
+        for idx in 1..self.line_cache.len() {
+            if self.line_cache[idx].time_stamp < self.line_cache[oldest].time_stamp {
+                oldest = idx;
+            }
+        }
+
+        oldest
+    }
+
+    fn file_for_position(&self, pos: BytePos) -> Option<(Lrc<SourceFile>, usize)> {
+        if !self.source_map.files().is_empty() {
+            let file_idx = self.source_map.lookup_source_file_idx(pos);
+            let file = &self.source_map.files()[file_idx];
+
+            if file_contains(file, pos) {
+                return Some((file.clone(), file_idx));
+            }
+        }
+
+        None
+    }
 }
 
 #[inline]